blob: e1afcdb8767ee042003d7804b6b35da9eaa0dcd1 [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 */
perkja49cbd32016-09-16 07:53:41 -070010#include "webrtc/video/video_quality_test.h"
perkj9fdbda62016-09-15 09:19:20 -070011
perkja49cbd32016-09-16 07:53:41 -070012#include <stdio.h>
ivica5d6a06c2015-09-17 05:30:24 -070013#include <algorithm>
14#include <deque>
15#include <map>
sprangce4aef12015-11-02 07:23:20 -080016#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080017#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070018#include <vector>
19
ivica5d6a06c2015-09-17 05:30:24 -070020#include "webrtc/base/checks.h"
Peter Boström5811a392015-12-10 13:02:50 +010021#include "webrtc/base/event.h"
ivica5d6a06c2015-09-17 05:30:24 -070022#include "webrtc/base/format_macros.h"
nissec7fe3c22016-04-20 03:25:36 -070023#include "webrtc/base/optional.h"
palmkviste75f2042016-09-28 06:19:48 -070024#include "webrtc/base/platform_file.h"
sprange1f2f1f2016-02-01 02:04:52 -080025#include "webrtc/base/timeutils.h"
ossuf515ab82016-12-07 04:52:58 -080026#include "webrtc/call/call.h"
ivica5d6a06c2015-09-17 05:30:24 -070027#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 11:53:05 -070028#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
aleloi10111bc2016-11-17 06:48:48 -080029#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprangce4aef12015-11-02 07:23:20 -080031#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
magjed509e4fe2016-11-18 01:34:11 -080032#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
33#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
34#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/cpu_info.h"
kwibergac9f8762016-09-30 22:29:43 -070036#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070037#include "webrtc/test/layer_filtering_transport.h"
38#include "webrtc/test/run_loop.h"
39#include "webrtc/test/statistics.h"
40#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 07:53:41 -070041#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070042#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070043#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070044
45namespace {
46
47constexpr int kSendStatsPollingIntervalMs = 1000;
48constexpr int kPayloadTypeH264 = 122;
49constexpr int kPayloadTypeVP8 = 123;
50constexpr int kPayloadTypeVP9 = 124;
51constexpr size_t kMaxComparisons = 10;
52constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080053constexpr int kOpusMinBitrateBps = 6000;
54constexpr int kOpusBitrateFbBps = 32000;
minyue73208662016-08-18 06:28:55 -070055
56struct VoiceEngineState {
57 VoiceEngineState()
58 : voice_engine(nullptr),
59 base(nullptr),
minyue73208662016-08-18 06:28:55 -070060 send_channel_id(-1),
61 receive_channel_id(-1) {}
62
63 webrtc::VoiceEngine* voice_engine;
64 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070065 int send_channel_id;
66 int receive_channel_id;
67};
68
69void CreateVoiceEngine(VoiceEngineState* voe,
70 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
71 decoder_factory) {
72 voe->voice_engine = webrtc::VoiceEngine::Create();
73 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
minyue73208662016-08-18 06:28:55 -070074 EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070075 webrtc::VoEBase::ChannelConfig config;
76 config.enable_voice_pacing = true;
77 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070078 EXPECT_GE(voe->send_channel_id, 0);
79 voe->receive_channel_id = voe->base->CreateChannel();
80 EXPECT_GE(voe->receive_channel_id, 0);
81}
82
83void DestroyVoiceEngine(VoiceEngineState* voe) {
84 voe->base->DeleteChannel(voe->send_channel_id);
85 voe->send_channel_id = -1;
86 voe->base->DeleteChannel(voe->receive_channel_id);
87 voe->receive_channel_id = -1;
88 voe->base->Release();
89 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -070090
91 webrtc::VoiceEngine::Delete(voe->voice_engine);
92 voe->voice_engine = nullptr;
93}
94
perkjfa10b552016-10-02 23:45:26 -070095class VideoStreamFactory
96 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
97 public:
98 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
99 : streams_(streams) {}
100
101 private:
102 std::vector<webrtc::VideoStream> CreateEncoderStreams(
103 int width,
104 int height,
105 const webrtc::VideoEncoderConfig& encoder_config) override {
106 return streams_;
107 }
108
109 std::vector<webrtc::VideoStream> streams_;
110};
111
minyue73208662016-08-18 06:28:55 -0700112} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700113
114namespace webrtc {
115
ivica5d6a06c2015-09-17 05:30:24 -0700116class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700117 public Transport,
nisse7ade7b32016-03-23 04:48:10 -0700118 public rtc::VideoSinkInterface<VideoFrame>,
Peter Boströme4499152016-02-05 11:13:28 +0100119 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 05:30:24 -0700120 public:
sprangce4aef12015-11-02 07:23:20 -0800121 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700122 const std::string& test_label,
123 double avg_psnr_threshold,
124 double avg_ssim_threshold,
125 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800126 FILE* graph_data_output_file,
127 const std::string& graph_title,
128 uint32_t ssrc_to_analyze)
perkja49cbd32016-09-16 07:53:41 -0700129 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700130 receiver_(nullptr),
131 send_stream_(nullptr),
perkja49cbd32016-09-16 07:53:41 -0700132 captured_frame_forwarder_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700133 test_label_(test_label),
134 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800135 graph_title_(graph_title),
136 ssrc_to_analyze_(ssrc_to_analyze),
pbos14fe7082016-04-20 06:35:56 -0700137 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100138 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700139 frames_to_process_(duration_frames),
140 frames_recorded_(0),
141 frames_processed_(0),
142 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700143 dropped_frames_before_first_encode_(0),
144 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700145 last_render_time_(0),
146 rtp_timestamp_delta_(0),
147 avg_psnr_threshold_(avg_psnr_threshold),
148 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100149 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100150 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100151 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700152 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
153
154 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
155 // so that we don't accidentally starve "real" worker threads (codec etc).
156 // Also, don't allocate more than kMaxComparisonThreads, even if there are
157 // spare cores.
158
159 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800160 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700161 static const uint32_t kMinCoresLeft = 4;
162 static const uint32_t kMaxComparisonThreads = 8;
163
164 if (num_cores <= kMinCoresLeft) {
165 num_cores = 1;
166 } else {
167 num_cores -= kMinCoresLeft;
168 num_cores = std::min(num_cores, kMaxComparisonThreads);
169 }
170
171 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100172 rtc::PlatformThread* thread =
173 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
174 thread->Start();
175 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700176 }
ivica5d6a06c2015-09-17 05:30:24 -0700177 }
178
179 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100180 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
181 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700182 delete thread;
183 }
184 }
185
186 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
187
perkja49cbd32016-09-16 07:53:41 -0700188 void SetSendStream(VideoSendStream* stream) {
189 rtc::CritScope lock(&crit_);
190 RTC_DCHECK(!send_stream_);
191 send_stream_ = stream;
192 }
193
194 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
195 return &captured_frame_forwarder_;
196 }
197 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
198 return &captured_frame_forwarder_;
199 }
200
ivica5d6a06c2015-09-17 05:30:24 -0700201 DeliveryStatus DeliverPacket(MediaType media_type,
202 const uint8_t* packet,
203 size_t length,
204 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700205 // Ignore timestamps of RTCP packets. They're not synchronized with
206 // RTP packet timestamps and so they would confuse wrap_handler_.
207 if (RtpHeaderParser::IsRtcp(packet, length)) {
208 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
209 }
210
sprangce4aef12015-11-02 07:23:20 -0800211 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700212 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800213 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700214 {
215 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800216 int64_t timestamp =
217 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
218 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700219 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
220 }
221
222 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
223 }
224
Peter Boströme4499152016-02-05 11:13:28 +0100225 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700226 rtc::CritScope crit(&comparison_lock_);
227 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
228 }
229
pbos14fe7082016-04-20 06:35:56 -0700230 void PreEncodeOnFrame(const VideoFrame& video_frame) {
231 rtc::CritScope lock(&crit_);
232 if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0) {
233 while (frames_.front().timestamp() != video_frame.timestamp()) {
234 ++dropped_frames_before_first_encode_;
235 frames_.pop_front();
236 RTC_CHECK(!frames_.empty());
237 }
238 first_send_timestamp_ = rtc::Optional<uint32_t>(video_frame.timestamp());
239 }
240 }
241
stefan1d8a5062015-10-02 03:39:33 -0700242 bool SendRtp(const uint8_t* packet,
243 size_t length,
244 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800245 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700246 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800247 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700248
sprangce4aef12015-11-02 07:23:20 -0800249 int64_t current_time =
250 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
251 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700252 {
253 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800254
Peter Boström81cbd922016-03-22 12:19:07 +0100255 if (rtp_timestamp_delta_ == 0) {
nissec7fe3c22016-04-20 03:25:36 -0700256 rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_;
257 first_send_timestamp_ = rtc::Optional<uint32_t>();
ivica5d6a06c2015-09-17 05:30:24 -0700258 }
sprang1b3530b2016-03-10 01:32:53 -0800259 int64_t timestamp =
260 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 07:23:20 -0800261 send_times_[timestamp] = current_time;
262 if (!transport_->DiscardedLastPacket() &&
263 header.ssrc == ssrc_to_analyze_) {
264 encoded_frame_sizes_[timestamp] +=
265 length - (header.headerLength + header.paddingLength);
266 }
ivica5d6a06c2015-09-17 05:30:24 -0700267 }
sprangce4aef12015-11-02 07:23:20 -0800268 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700269 }
270
271 bool SendRtcp(const uint8_t* packet, size_t length) override {
272 return transport_->SendRtcp(packet, length);
273 }
274
275 void EncodedFrameCallback(const EncodedFrame& frame) override {
276 rtc::CritScope lock(&comparison_lock_);
277 if (frames_recorded_ < frames_to_process_)
278 encoded_frame_size_.AddSample(frame.length_);
279 }
280
nisseeb83a1a2016-03-21 01:27:56 -0700281 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700282 int64_t render_time_ms =
283 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700284
285 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700286 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800287 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700288
sprang16daaa52016-03-09 01:30:24 -0800289 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700290 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700291 // No previous frame rendered, this one was dropped after sending but
292 // before rendering.
293 ++dropped_frames_before_rendering_;
294 frames_.pop_front();
295 RTC_CHECK(!frames_.empty());
296 continue;
297 }
nisse97f0b932016-05-26 09:44:40 -0700298 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
ivica5d6a06c2015-09-17 05:30:24 -0700299 render_time_ms);
300 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700301 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700302 }
303
304 VideoFrame reference_frame = frames_.front();
305 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800306 int64_t reference_timestamp =
307 wrap_handler_.Unwrap(reference_frame.timestamp());
308 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800309 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100310 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800311 ++send_timestamp;
312 }
sprang16daaa52016-03-09 01:30:24 -0800313 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700314
315 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
316
nisse97f0b932016-05-26 09:44:40 -0700317 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ivica5d6a06c2015-09-17 05:30:24 -0700318 }
319
ivica5d6a06c2015-09-17 05:30:24 -0700320 void Wait() {
321 // Frame comparisons can be very expensive. Wait for test to be done, but
322 // at time-out check if frames_processed is going up. If so, give it more
323 // time, otherwise fail. Hopefully this will reduce test flakiness.
324
Peter Boström8c38e8b2015-11-26 17:45:47 +0100325 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800326
ivica5d6a06c2015-09-17 05:30:24 -0700327 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700328 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100329 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700330 int frames_processed;
331 {
332 rtc::CritScope crit(&comparison_lock_);
333 frames_processed = frames_processed_;
334 }
335
336 // Print some output so test infrastructure won't think we've crashed.
337 const char* kKeepAliveMessages[3] = {
338 "Uh, I'm-I'm not quite dead, sir.",
339 "Uh, I-I think uh, I could pull through, sir.",
340 "Actually, I think I'm all right to come with you--"};
341 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
342
343 if (last_frames_processed == -1) {
344 last_frames_processed = frames_processed;
345 continue;
346 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100347 if (frames_processed == last_frames_processed) {
348 EXPECT_GT(frames_processed, last_frames_processed)
349 << "Analyzer stalled while waiting for test to finish.";
350 done_.Set();
351 break;
352 }
ivica5d6a06c2015-09-17 05:30:24 -0700353 last_frames_processed = frames_processed;
354 }
355
356 if (iteration > 0)
357 printf("- Farewell, sweet Concorde!\n");
358
Peter Boström8c38e8b2015-11-26 17:45:47 +0100359 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700360 }
361
pbos14fe7082016-04-20 06:35:56 -0700362 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
363 return &pre_encode_proxy_;
364 }
Peter Boströme4499152016-02-05 11:13:28 +0100365 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
366
sprangce4aef12015-11-02 07:23:20 -0800367 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700368 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700369
370 private:
371 struct FrameComparison {
372 FrameComparison()
373 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800374 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700375 send_time_ms(0),
376 recv_time_ms(0),
377 render_time_ms(0),
378 encoded_frame_size(0) {}
379
380 FrameComparison(const VideoFrame& reference,
381 const VideoFrame& render,
382 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800383 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700384 int64_t send_time_ms,
385 int64_t recv_time_ms,
386 int64_t render_time_ms,
387 size_t encoded_frame_size)
388 : reference(reference),
389 render(render),
390 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800391 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700392 send_time_ms(send_time_ms),
393 recv_time_ms(recv_time_ms),
394 render_time_ms(render_time_ms),
395 encoded_frame_size(encoded_frame_size) {}
396
nissedf2ceb82016-12-15 06:29:53 -0800397 FrameComparison(bool dropped,
398 int64_t input_time_ms,
399 int64_t send_time_ms,
400 int64_t recv_time_ms,
401 int64_t render_time_ms,
402 size_t encoded_frame_size)
403 : dropped(dropped),
404 input_time_ms(input_time_ms),
405 send_time_ms(send_time_ms),
406 recv_time_ms(recv_time_ms),
407 render_time_ms(render_time_ms),
408 encoded_frame_size(encoded_frame_size) {}
409
410 rtc::Optional<VideoFrame> reference;
411 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700412 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800413 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700414 int64_t send_time_ms;
415 int64_t recv_time_ms;
416 int64_t render_time_ms;
417 size_t encoded_frame_size;
418 };
419
420 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700421 Sample(int dropped,
422 int64_t input_time_ms,
423 int64_t send_time_ms,
424 int64_t recv_time_ms,
425 int64_t render_time_ms,
426 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700427 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700428 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700429 : dropped(dropped),
430 input_time_ms(input_time_ms),
431 send_time_ms(send_time_ms),
432 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700433 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700434 encoded_frame_size(encoded_frame_size),
435 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700436 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700437
ivica8d15bd62015-10-07 02:43:12 -0700438 int dropped;
439 int64_t input_time_ms;
440 int64_t send_time_ms;
441 int64_t recv_time_ms;
442 int64_t render_time_ms;
443 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700444 double psnr;
445 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700446 };
447
Peter Boströme4499152016-02-05 11:13:28 +0100448 // This class receives the send-side OnEncodeTiming and is provided to not
449 // conflict with the receiver-side pre_decode_callback.
450 class OnEncodeTimingProxy : public EncodedFrameObserver {
451 public:
452 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
453
454 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
455 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
456 }
457 void EncodedFrameCallback(const EncodedFrame& frame) override {}
458
459 private:
460 VideoAnalyzer* const parent_;
461 };
462
pbos14fe7082016-04-20 06:35:56 -0700463 // This class receives the send-side OnFrame callback and is provided to not
464 // conflict with the receiver-side renderer callback.
465 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
466 public:
467 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
468
469 void OnFrame(const VideoFrame& video_frame) override {
470 parent_->PreEncodeOnFrame(video_frame);
471 }
472
473 private:
474 VideoAnalyzer* const parent_;
475 };
476
ivica5d6a06c2015-09-17 05:30:24 -0700477 void AddFrameComparison(const VideoFrame& reference,
478 const VideoFrame& render,
479 bool dropped,
480 int64_t render_time_ms)
481 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800482 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
483 int64_t send_time_ms = send_times_[reference_timestamp];
484 send_times_.erase(reference_timestamp);
485 int64_t recv_time_ms = recv_times_[reference_timestamp];
486 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700487
sprangce4aef12015-11-02 07:23:20 -0800488 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800489 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800490 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800491 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800492 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
493 if (it != encoded_frame_sizes_.end())
494 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700495
ivica5d6a06c2015-09-17 05:30:24 -0700496 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700497 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800498 comparisons_.push_back(FrameComparison(reference, render, dropped,
499 reference.ntp_time_ms(),
500 send_time_ms, recv_time_ms,
501 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700502 } else {
nissedf2ceb82016-12-15 06:29:53 -0800503 comparisons_.push_back(FrameComparison(dropped,
504 reference.ntp_time_ms(),
505 send_time_ms, recv_time_ms,
506 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700507 }
Peter Boström5811a392015-12-10 13:02:50 +0100508 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700509 }
510
511 static bool PollStatsThread(void* obj) {
512 return static_cast<VideoAnalyzer*>(obj)->PollStats();
513 }
514
515 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100516 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100517 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700518
519 VideoSendStream::Stats stats = send_stream_->GetStats();
520
521 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100522 // It's not certain that we yet have estimates for any of these stats. Check
523 // that they are positive before mixing them in.
524 if (stats.encode_frame_rate > 0)
525 encode_frame_rate_.AddSample(stats.encode_frame_rate);
526 if (stats.avg_encode_time_ms > 0)
527 encode_time_ms.AddSample(stats.avg_encode_time_ms);
528 if (stats.encode_usage_percent > 0)
529 encode_usage_percent.AddSample(stats.encode_usage_percent);
530 if (stats.media_bitrate_bps > 0)
531 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700532
533 return true;
534 }
535
536 static bool FrameComparisonThread(void* obj) {
537 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
538 }
539
540 bool CompareFrames() {
541 if (AllFramesRecorded())
542 return false;
543
ivica5d6a06c2015-09-17 05:30:24 -0700544 FrameComparison comparison;
545
546 if (!PopComparison(&comparison)) {
547 // Wait until new comparison task is available, or test is done.
548 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100549 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700550 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100551 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700552 return false;
553 }
554 return true; // Try again.
555 }
556
557 PerformFrameComparison(comparison);
558
559 if (FrameProcessed()) {
560 PrintResults();
561 if (graph_data_output_file_)
562 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100563 done_.Set();
564 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700565 return false;
566 }
567
568 return true;
569 }
570
571 bool PopComparison(FrameComparison* comparison) {
572 rtc::CritScope crit(&comparison_lock_);
573 // If AllFramesRecorded() is true, it means we have already popped
574 // frames_to_process_ frames from comparisons_, so there is no more work
575 // for this thread to be done. frames_processed_ might still be lower if
576 // all comparisons are not done, but those frames are currently being
577 // worked on by other threads.
578 if (comparisons_.empty() || AllFramesRecorded())
579 return false;
580
581 *comparison = comparisons_.front();
582 comparisons_.pop_front();
583
584 FrameRecorded();
585 return true;
586 }
587
588 // Increment counter for number of frames received for comparison.
589 void FrameRecorded() {
590 rtc::CritScope crit(&comparison_lock_);
591 ++frames_recorded_;
592 }
593
594 // Returns true if all frames to be compared have been taken from the queue.
595 bool AllFramesRecorded() {
596 rtc::CritScope crit(&comparison_lock_);
597 assert(frames_recorded_ <= frames_to_process_);
598 return frames_recorded_ == frames_to_process_;
599 }
600
601 // Increase count of number of frames processed. Returns true if this was the
602 // last frame to be processed.
603 bool FrameProcessed() {
604 rtc::CritScope crit(&comparison_lock_);
605 ++frames_processed_;
606 assert(frames_processed_ <= frames_to_process_);
607 return frames_processed_ == frames_to_process_;
608 }
609
610 void PrintResults() {
611 rtc::CritScope crit(&comparison_lock_);
612 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800613 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700614 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700615 PrintResult("receiver_time", receiver_time_, " ms");
616 PrintResult("total_delay_incl_network", end_to_end_, " ms");
617 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
618 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
619 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
620 PrintResult("encode_time", encode_time_ms, " ms");
621 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
622 PrintResult("media_bitrate", media_bitrate_bps, " bps");
623
pbos14fe7082016-04-20 06:35:56 -0700624 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
625 dropped_frames_);
626 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
627 test_label_.c_str(), dropped_frames_before_first_encode_);
628 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
629 test_label_.c_str(), dropped_frames_before_rendering_);
630
ivica5d6a06c2015-09-17 05:30:24 -0700631 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
632 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
633 }
634
635 void PerformFrameComparison(const FrameComparison& comparison) {
636 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700637 double psnr = -1.0;
638 double ssim = -1.0;
nissedf2ceb82016-12-15 06:29:53 -0800639 if (comparison.reference) {
640 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
641 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700642 }
ivica5d6a06c2015-09-17 05:30:24 -0700643
ivica5d6a06c2015-09-17 05:30:24 -0700644 rtc::CritScope crit(&comparison_lock_);
645 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800646 samples_.push_back(Sample(
647 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
648 comparison.recv_time_ms, comparison.render_time_ms,
649 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700650 }
stefanb1797672016-08-11 07:00:57 -0700651 if (psnr >= 0.0)
652 psnr_.AddSample(psnr);
653 if (ssim >= 0.0)
654 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700655
656 if (comparison.dropped) {
657 ++dropped_frames_;
658 return;
659 }
660 if (last_render_time_ != 0)
661 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
662 last_render_time_ = comparison.render_time_ms;
663
nissedf2ceb82016-12-15 06:29:53 -0800664 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700665 receiver_time_.AddSample(comparison.render_time_ms -
666 comparison.recv_time_ms);
nissedf2ceb82016-12-15 06:29:53 -0800667 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700668 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
669 }
670
671 void PrintResult(const char* result_type,
672 test::Statistics stats,
673 const char* unit) {
674 printf("RESULT %s: %s = {%f, %f}%s\n",
675 result_type,
676 test_label_.c_str(),
677 stats.Mean(),
678 stats.StandardDeviation(),
679 unit);
680 }
681
682 void PrintSamplesToFile(void) {
683 FILE* out = graph_data_output_file_;
684 rtc::CritScope crit(&comparison_lock_);
685 std::sort(samples_.begin(), samples_.end(),
686 [](const Sample& A, const Sample& B) -> bool {
687 return A.input_time_ms < B.input_time_ms;
688 });
689
sprangce4aef12015-11-02 07:23:20 -0800690 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700691 fprintf(out, "%" PRIuS "\n", samples_.size());
692 fprintf(out,
693 "dropped "
694 "input_time_ms "
695 "send_time_ms "
696 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700697 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700698 "encoded_frame_size "
699 "psnr "
700 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700701 "encode_time_ms\n");
702 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700703 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700704 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
705 int encode_time_ms;
706 if (it != samples_encode_time_ms_.end()) {
707 encode_time_ms = it->second;
708 } else {
709 ++missing_encode_time_samples;
710 encode_time_ms = -1;
711 }
712 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
713 " %lf %lf %d\n",
714 sample.dropped, sample.input_time_ms, sample.send_time_ms,
715 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700716 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700717 encode_time_ms);
718 }
719 if (missing_encode_time_samples) {
720 fprintf(stderr,
721 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
722 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700723 }
724 }
725
perkja49cbd32016-09-16 07:53:41 -0700726 // Implements VideoSinkInterface to receive captured frames from a
727 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
728 // as a source to VideoSendStream.
729 // It forwards all input frames to the VideoAnalyzer for later comparison and
730 // forwards the captured frames to the VideoSendStream.
731 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
732 public rtc::VideoSourceInterface<VideoFrame> {
733 public:
734 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
735 : analyzer_(analyzer), send_stream_input_(nullptr) {}
736
737 private:
738 void OnFrame(const VideoFrame& video_frame) override {
739 VideoFrame copy = video_frame;
740 copy.set_timestamp(copy.ntp_time_ms() * 90);
741
742 analyzer_->AddCapturedFrameForComparison(video_frame);
743 rtc::CritScope lock(&crit_);
744 if (send_stream_input_)
745 send_stream_input_->OnFrame(video_frame);
746 }
747
748 // Called when |send_stream_.SetSource()| is called.
749 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
750 const rtc::VideoSinkWants& wants) override {
751 rtc::CritScope lock(&crit_);
752 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
753 send_stream_input_ = sink;
754 }
755
756 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
757 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
758 rtc::CritScope lock(&crit_);
759 RTC_DCHECK(sink == send_stream_input_);
760 send_stream_input_ = nullptr;
761 }
762
763 VideoAnalyzer* const analyzer_;
764 rtc::CriticalSection crit_;
765 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
766 };
767
768 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
769 rtc::CritScope lock(&crit_);
kwibergaf476c72016-11-28 15:21:39 -0800770 RTC_DCHECK_EQ(0, video_frame.timestamp());
perkja49cbd32016-09-16 07:53:41 -0700771 // Frames from the capturer does not have a rtp timestamp. Create one so it
772 // can be used for comparison.
773 VideoFrame copy = video_frame;
774 copy.set_timestamp(copy.ntp_time_ms() * 90);
775 frames_.push_back(copy);
776 }
777
778 VideoSendStream* send_stream_;
779 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700780 const std::string test_label_;
781 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800782 const std::string graph_title_;
783 const uint32_t ssrc_to_analyze_;
pbos14fe7082016-04-20 06:35:56 -0700784 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100785 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700786 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700787 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700788 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
789 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
790 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
791 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
792 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
793 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
794 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
795 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
796 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
797 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
798 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
799
800 const int frames_to_process_;
801 int frames_recorded_;
802 int frames_processed_;
803 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700804 int dropped_frames_before_first_encode_;
805 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700806 int64_t last_render_time_;
807 uint32_t rtp_timestamp_delta_;
808
809 rtc::CriticalSection crit_;
810 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700811 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800812 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
813 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
814 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
815 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 03:25:36 -0700816 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700817 const double avg_psnr_threshold_;
818 const double avg_ssim_threshold_;
819
820 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100821 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
822 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100823 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700824 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100825 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700826};
827
palmkviste75f2042016-09-28 06:19:48 -0700828VideoQualityTest::VideoQualityTest()
829 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 05:30:24 -0700830
minyue626bc952016-10-31 05:47:02 -0700831VideoQualityTest::Params::Params()
832 : call({false, Call::Config::BitrateConfig()}),
833 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -0800834 false, "", ""}),
minyue626bc952016-10-31 05:47:02 -0700835 audio({false, false}),
836 screenshare({false, 10, 0}),
837 analyzer({"", 0.0, 0.0, 0, "", ""}),
838 pipe(),
839 logs(false),
840 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}) {}
841
842VideoQualityTest::Params::~Params() = default;
843
ivica5d6a06c2015-09-17 05:30:24 -0700844void VideoQualityTest::TestBody() {}
845
sprangce4aef12015-11-02 07:23:20 -0800846std::string VideoQualityTest::GenerateGraphTitle() const {
847 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -0700848 ss << params_.video.codec;
849 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
850 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -0800851 if (params_.screenshare.scroll_duration)
852 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
853 if (params_.ss.streams.size() > 1)
854 ss << ", Stream #" << params_.ss.selected_stream;
855 if (params_.ss.num_spatial_layers > 1)
856 ss << ", Layer #" << params_.ss.selected_sl;
857 ss << ")";
858 return ss.str();
859}
860
861void VideoQualityTest::CheckParams() {
862 // Add a default stream in none specified.
863 if (params_.ss.streams.empty())
864 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
865 if (params_.ss.num_spatial_layers == 0)
866 params_.ss.num_spatial_layers = 1;
867
868 if (params_.pipe.loss_percent != 0 ||
869 params_.pipe.queue_length_packets != 0) {
870 // Since LayerFilteringTransport changes the sequence numbers, we can't
871 // use that feature with pack loss, since the NACK request would end up
872 // retransmitting the wrong packets.
873 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800874 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -0700875 RTC_CHECK(params_.video.selected_tl == -1 ||
876 params_.video.selected_tl ==
877 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800878 }
879
880 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
881 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -0700882 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
883 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
884 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -0800885 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
886 for (const VideoStream& stream : params_.ss.streams) {
887 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
888 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
889 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
kwiberg352444f2016-11-28 15:58:53 -0800890 RTC_CHECK_EQ(stream.temporal_layer_thresholds_bps.size(),
minyue626bc952016-10-31 05:47:02 -0700891 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800892 }
893 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
894 // the total bitrate? We anyway have to update them in the case bitrate
895 // estimator changes the total bitrates.
896 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
897 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
898 RTC_CHECK(params_.ss.spatial_layers.empty() ||
899 params_.ss.spatial_layers.size() ==
900 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -0700901 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -0800902 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -0700903 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -0800904 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -0800905 }
906}
907
908// Static.
909std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
910 // Parse comma separated nonnegative integers, where some elements may be
911 // empty. The empty values are replaced with -1.
912 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
913 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
914 std::vector<int> result;
915 if (str.empty())
916 return result;
917
918 const char* p = str.c_str();
919 int value = -1;
920 int pos;
921 while (*p) {
922 if (*p == ',') {
923 result.push_back(value);
924 value = -1;
925 ++p;
926 continue;
927 }
928 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
929 << "Unexpected non-number value.";
930 p += pos;
931 }
932 result.push_back(value);
933 return result;
934}
935
936// Static.
937VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
938 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -0700939 stream.width = params.video.width;
940 stream.height = params.video.height;
941 stream.max_framerate = params.video.fps;
942 stream.min_bitrate_bps = params.video.min_bitrate_bps;
943 stream.target_bitrate_bps = params.video.target_bitrate_bps;
944 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -0800945 stream.max_qp = 52;
minyue626bc952016-10-31 05:47:02 -0700946 if (params.video.num_temporal_layers == 2)
sprangce4aef12015-11-02 07:23:20 -0800947 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
948 return stream;
949}
950
951// Static.
952void VideoQualityTest::FillScalabilitySettings(
953 Params* params,
954 const std::vector<std::string>& stream_descriptors,
955 size_t selected_stream,
956 int num_spatial_layers,
957 int selected_sl,
958 const std::vector<std::string>& sl_descriptors) {
959 // Read VideoStream and SpatialLayer elements from a list of comma separated
960 // lists. To use a default value for an element, use -1 or leave empty.
961 // Validity checks performed in CheckParams.
962
963 RTC_CHECK(params->ss.streams.empty());
964 for (auto descriptor : stream_descriptors) {
965 if (descriptor.empty())
966 continue;
967 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
968 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
969 if (v[0] != -1)
970 stream.width = static_cast<size_t>(v[0]);
971 if (v[1] != -1)
972 stream.height = static_cast<size_t>(v[1]);
973 if (v[2] != -1)
974 stream.max_framerate = v[2];
975 if (v[3] != -1)
976 stream.min_bitrate_bps = v[3];
977 if (v[4] != -1)
978 stream.target_bitrate_bps = v[4];
979 if (v[5] != -1)
980 stream.max_bitrate_bps = v[5];
981 if (v.size() > 6 && v[6] != -1)
982 stream.max_qp = v[6];
983 if (v.size() > 7) {
984 stream.temporal_layer_thresholds_bps.clear();
985 stream.temporal_layer_thresholds_bps.insert(
986 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
987 } else {
988 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 05:47:02 -0700989 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -0800990 }
991 params->ss.streams.push_back(stream);
992 }
993 params->ss.selected_stream = selected_stream;
994
995 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
996 params->ss.selected_sl = selected_sl;
997 RTC_CHECK(params->ss.spatial_layers.empty());
998 for (auto descriptor : sl_descriptors) {
999 if (descriptor.empty())
1000 continue;
1001 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1002 RTC_CHECK_GT(v[2], 0);
1003
1004 SpatialLayer layer;
1005 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1006 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1007 layer.target_bitrate_bps = v[2];
1008 params->ss.spatial_layers.push_back(layer);
1009 }
1010}
1011
minyuea27172d2016-11-01 05:59:29 -07001012void VideoQualityTest::SetupVideo(Transport* send_transport,
1013 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001014 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001015 trace_to_stderr_.reset(new test::TraceToStderr);
1016
sprangce4aef12015-11-02 07:23:20 -08001017 size_t num_streams = params_.ss.streams.size();
brandtr841de6a2016-11-15 07:10:52 -08001018 CreateSendConfig(num_streams, 0, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001019
1020 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001021 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001022 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001023 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001024 } else if (params_.video.codec == "VP8") {
magjed509e4fe2016-11-18 01:34:11 -08001025 video_encoder_.reset(VP8Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001026 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001027 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001028 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001029 payload_type = kPayloadTypeVP9;
1030 } else {
1031 RTC_NOTREACHED() << "Codec not supported!";
1032 return;
1033 }
minyuea27172d2016-11-01 05:59:29 -07001034 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001035 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001036 video_send_config_.encoder_settings.payload_type = payload_type;
1037 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1038 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -08001039 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001040 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001041
stefanff483612015-12-21 03:14:00 -08001042 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001043 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001044 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001045 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001046 test::kTransportSequenceNumberExtensionId));
1047 } else {
stefanff483612015-12-21 03:14:00 -08001048 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001049 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001050 }
1051
stefanff483612015-12-21 03:14:00 -08001052 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001053 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001054
brandtr1293aca2016-11-16 22:47:29 -08001055 video_send_config_.suspend_below_min_bitrate =
1056 params_.video.suspend_below_min_bitrate;
1057
perkjfa10b552016-10-02 23:45:26 -07001058 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1059 video_encoder_config_.max_bitrate_bps = 0;
1060 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1061 video_encoder_config_.max_bitrate_bps +=
1062 params_.ss.streams[i].max_bitrate_bps;
1063 }
1064 video_encoder_config_.video_stream_factory =
1065 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1066
stefanff483612015-12-21 03:14:00 -08001067 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001068
1069 CreateMatchingReceiveConfigs(recv_transport);
1070
sprangce4aef12015-11-02 07:23:20 -08001071 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001072 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +01001073 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
1074 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -08001075 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001076 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -08001077 }
brandtr1293aca2016-11-16 22:47:29 -08001078
1079 if (params_.video.flexfec) {
1080 video_send_config_.rtp.flexfec.flexfec_payload_type = kFlexfecPayloadType;
1081 video_send_config_.rtp.flexfec.flexfec_ssrc = kFlexfecSendSsrc;
1082 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1083 kVideoSendSsrcs[params_.ss.selected_stream]};
1084
1085 FlexfecReceiveStream::Config flexfec_receive_config;
brandtr1cfbd602016-12-08 04:17:53 -08001086 flexfec_receive_config.payload_type =
brandtr1293aca2016-11-16 22:47:29 -08001087 video_send_config_.rtp.flexfec.flexfec_payload_type;
brandtr1cfbd602016-12-08 04:17:53 -08001088 flexfec_receive_config.remote_ssrc =
brandtr1293aca2016-11-16 22:47:29 -08001089 video_send_config_.rtp.flexfec.flexfec_ssrc;
1090 flexfec_receive_config.protected_media_ssrcs =
1091 video_send_config_.rtp.flexfec.protected_media_ssrcs;
1092 flexfec_receive_configs_.push_back(flexfec_receive_config);
1093 }
1094
1095 if (params_.video.ulpfec) {
1096 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1097 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1098 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1099
1100 video_receive_configs_[params_.ss.selected_stream]
1101 .rtp.ulpfec.red_payload_type =
1102 video_send_config_.rtp.ulpfec.red_payload_type;
1103 video_receive_configs_[params_.ss.selected_stream]
1104 .rtp.ulpfec.ulpfec_payload_type =
1105 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1106 video_receive_configs_[params_.ss.selected_stream]
1107 .rtp.ulpfec.red_rtx_payload_type =
1108 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1109 }
ivica5d6a06c2015-09-17 05:30:24 -07001110}
1111
sprangce4aef12015-11-02 07:23:20 -08001112void VideoQualityTest::SetupScreenshare() {
1113 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001114
1115 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -08001116 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
minyue626bc952016-10-31 05:47:02 -07001117 if (params_.video.codec == "VP8") {
kthelgason29a44e32016-09-27 03:52:02 -07001118 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1119 vp8_settings.denoisingOn = false;
1120 vp8_settings.frameDroppingOn = false;
1121 vp8_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001122 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001123 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1124 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
minyue626bc952016-10-31 05:47:02 -07001125 } else if (params_.video.codec == "VP9") {
kthelgason29a44e32016-09-27 03:52:02 -07001126 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1127 vp9_settings.denoisingOn = false;
1128 vp9_settings.frameDroppingOn = false;
1129 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001130 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001131 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001132 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001133 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1134 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001135 }
1136
1137 // Setup frame generator.
1138 const size_t kWidth = 1850;
1139 const size_t kHeight = 1110;
1140 std::vector<std::string> slides;
1141 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1142 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1143 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1144 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1145
sprangce4aef12015-11-02 07:23:20 -08001146 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -07001147 // Cycle image every slide_change_interval seconds.
1148 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
1149 slides, kWidth, kHeight,
minyue626bc952016-10-31 05:47:02 -07001150 params_.screenshare.slide_change_interval * params_.video.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001151 } else {
minyue626bc952016-10-31 05:47:02 -07001152 RTC_CHECK_LE(params_.video.width, kWidth);
1153 RTC_CHECK_LE(params_.video.height, kHeight);
sprangce4aef12015-11-02 07:23:20 -08001154 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1155 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1156 params_.screenshare.scroll_duration) *
1157 1000;
1158 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1159 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -07001160
sprangce4aef12015-11-02 07:23:20 -08001161 frame_generator_.reset(
1162 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
minyue626bc952016-10-31 05:47:02 -07001163 clock_, slides, kWidth, kHeight, params_.video.width,
1164 params_.video.height, params_.screenshare.scroll_duration * 1000,
sprangce4aef12015-11-02 07:23:20 -08001165 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -07001166 }
1167}
1168
perkja49cbd32016-09-16 07:53:41 -07001169void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001170 if (params_.screenshare.enabled) {
1171 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja49cbd32016-09-16 07:53:41 -07001172 new test::FrameGeneratorCapturer(clock_, frame_generator_.release(),
minyue626bc952016-10-31 05:47:02 -07001173 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001174 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001175 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001176 } else {
sprangce4aef12015-11-02 07:23:20 -08001177 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001178 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 05:47:02 -07001179 params_.video.width, params_.video.height, params_.video.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001180 } else {
minyuea27172d2016-11-01 05:59:29 -07001181 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001182 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001183 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001184 clock_));
minyuea27172d2016-11-01 05:59:29 -07001185 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1186 << params_.video.clip_name
1187 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001188 }
1189 }
1190}
1191
sprang7a975f72015-10-12 06:33:21 -07001192void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001193 params_ = params;
1194
minyue626bc952016-10-31 05:47:02 -07001195 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001196 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1197 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001198 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001199
1200 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001201 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001202 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001203 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001204 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001205 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1206 << "!";
ivica87f83a92015-10-08 05:13:32 -07001207 }
sprang7a975f72015-10-12 06:33:21 -07001208
skvlad11a9cbf2016-10-07 11:53:05 -07001209 webrtc::RtcEventLogNullImpl event_log;
1210 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001211 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001212 CreateCalls(call_config, call_config);
1213
ivica87f83a92015-10-08 05:13:32 -07001214 test::LayerFilteringTransport send_transport(
minyue626bc952016-10-31 05:47:02 -07001215 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1216 params_.video.selected_tl, params_.ss.selected_sl);
1217 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get());
stefanf116bd02015-10-27 08:29:42 -07001218
sprangce4aef12015-11-02 07:23:20 -08001219 std::string graph_title = params_.analyzer.graph_title;
1220 if (graph_title.empty())
1221 graph_title = VideoQualityTest::GenerateGraphTitle();
1222
1223 // In the case of different resolutions, the functions calculating PSNR and
1224 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1225 // aborts if the average psnr/ssim are below the given threshold, which is
1226 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1227 // abort.
1228 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1229 int selected_sl = params_.ss.selected_sl != -1
1230 ? params_.ss.selected_sl
1231 : params_.ss.num_spatial_layers - 1;
1232 bool disable_quality_check =
minyue626bc952016-10-31 05:47:02 -07001233 selected_stream.width != params_.video.width ||
1234 selected_stream.height != params_.video.height ||
sprangce4aef12015-11-02 07:23:20 -08001235 (!params_.ss.spatial_layers.empty() &&
1236 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1237 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1238 if (disable_quality_check) {
1239 fprintf(stderr,
1240 "Warning: Calculating PSNR and SSIM for downsized resolution "
1241 "not implemented yet! Skipping PSNR and SSIM calculations!");
1242 }
1243
ivica5d6a06c2015-09-17 05:30:24 -07001244 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001245 &send_transport, params_.analyzer.test_label,
1246 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1247 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
minyue626bc952016-10-31 05:47:02 -07001248 params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001249 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001250 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001251
ivica5d6a06c2015-09-17 05:30:24 -07001252 analyzer.SetReceiver(receiver_call_->Receiver());
1253 send_transport.SetReceiver(&analyzer);
1254 recv_transport.SetReceiver(sender_call_->Receiver());
1255
minyuea27172d2016-11-01 05:59:29 -07001256 SetupVideo(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001257 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001258 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
stefanff483612015-12-21 03:14:00 -08001259 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001260 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001261 RTC_DCHECK(!video_send_config_.post_encode_callback);
1262 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001263
sprangce4aef12015-11-02 07:23:20 -08001264 if (params_.screenshare.enabled)
1265 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001266
brandtr1293aca2016-11-16 22:47:29 -08001267 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001268 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001269 analyzer.SetSendStream(video_send_stream_);
perkj803d97f2016-11-01 11:45:46 -07001270 video_send_stream_->SetSource(
1271 analyzer.OutputInterface(),
1272 VideoSendStream::DegradationPreference::kBalanced);
ivica5d6a06c2015-09-17 05:30:24 -07001273
perkja49cbd32016-09-16 07:53:41 -07001274 CreateCapturer();
1275 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 05:59:29 -07001276 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001277
palmkviste75f2042016-09-28 06:19:48 -07001278 StartEncodedFrameLogs(video_send_stream_);
1279 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001280 video_send_stream_->Start();
1281 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1282 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001283 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1284 receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001285 video_capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001286
1287 analyzer.Wait();
1288
1289 send_transport.StopSending();
1290 recv_transport.StopSending();
1291
minyuea27172d2016-11-01 05:59:29 -07001292 video_capturer_->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001293 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1294 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001295 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1296 receive_stream->Stop();
1297 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001298
1299 DestroyStreams();
1300
1301 if (graph_data_output_file)
1302 fclose(graph_data_output_file);
1303}
1304
minyuea27172d2016-11-01 05:59:29 -07001305void VideoQualityTest::SetupAudio(int send_channel_id,
1306 int receive_channel_id,
1307 Call* call,
1308 Transport* transport,
1309 AudioReceiveStream** audio_receive_stream) {
1310 audio_send_config_ = AudioSendStream::Config(transport);
1311 audio_send_config_.voe_channel_id = send_channel_id;
1312 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1313
1314 // Add extension to enable audio send side BWE, and allow audio bit rate
1315 // adaptation.
1316 audio_send_config_.rtp.extensions.clear();
1317 if (params_.call.send_side_bwe) {
1318 audio_send_config_.rtp.extensions.push_back(
1319 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1320 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001321 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1322 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001323 }
1324 audio_send_config_.send_codec_spec.codec_inst =
1325 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
1326
1327 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1328
1329 AudioReceiveStream::Config audio_config;
1330 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1331 audio_config.rtcp_send_transport = transport;
1332 audio_config.voe_channel_id = receive_channel_id;
1333 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1334 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1335 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1336 audio_config.decoder_factory = decoder_factory_;
1337 if (params_.video.enabled && params_.audio.sync_video)
1338 audio_config.sync_group = kSyncGroup;
1339
1340 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1341}
1342
minyue73208662016-08-18 06:28:55 -07001343void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001344 params_ = params;
1345 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001346
ivica5d6a06c2015-09-17 05:30:24 -07001347 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1348 // match the full stack tests.
skvlad11a9cbf2016-10-07 11:53:05 -07001349 webrtc::RtcEventLogNullImpl event_log;
1350 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001351 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001352
1353 ::VoiceEngineState voe;
minyue626bc952016-10-31 05:47:02 -07001354 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001355 CreateVoiceEngine(&voe, decoder_factory_);
1356 AudioState::Config audio_state_config;
1357 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001358 audio_state_config.audio_mixer = AudioMixerImpl::Create();
minyue73208662016-08-18 06:28:55 -07001359 call_config.audio_state = AudioState::Create(audio_state_config);
1360 }
1361
kwiberg27f982b2016-03-01 11:52:33 -08001362 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001363
minyuea27172d2016-11-01 05:59:29 -07001364 // TODO(minyue): consider if this is a good transport even for audio only
1365 // calls.
ivica5d6a06c2015-09-17 05:30:24 -07001366 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001367 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue626bc952016-10-31 05:47:02 -07001368 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001369 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1370 // least share as much code as possible. That way this test would also match
1371 // the full stack tests better.
1372 transport.SetReceiver(call->Receiver());
1373
minyuea27172d2016-11-01 05:59:29 -07001374 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-16 22:47:29 -08001375 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 05:59:29 -07001376 std::unique_ptr<test::VideoRenderer> local_preview;
1377 std::unique_ptr<test::VideoRenderer> loopback_video;
1378 if (params_.video.enabled) {
1379 // Create video renderers.
1380 local_preview.reset(test::VideoRenderer::Create(
1381 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001382
minyuea27172d2016-11-01 05:59:29 -07001383 size_t stream_id = params_.ss.selected_stream;
1384 std::string title = "Loopback Video";
1385 if (params_.ss.streams.size() > 1) {
1386 std::ostringstream s;
1387 s << stream_id;
1388 title += " - Stream #" + s.str();
1389 }
sprangce4aef12015-11-02 07:23:20 -08001390
minyuea27172d2016-11-01 05:59:29 -07001391 loopback_video.reset(test::VideoRenderer::Create(
1392 title.c_str(), params_.ss.streams[stream_id].width,
1393 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 13:03:59 +02001394
minyuea27172d2016-11-01 05:59:29 -07001395 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 05:59:29 -07001396 video_send_config_.pre_encode_callback = local_preview.get();
1397 video_receive_configs_[stream_id].renderer = loopback_video.get();
1398 if (params_.audio.enabled && params_.audio.sync_video)
1399 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1400
minyuea27172d2016-11-01 05:59:29 -07001401 if (params_.screenshare.enabled)
1402 SetupScreenshare();
1403
1404 video_send_stream_ = call->CreateVideoSendStream(
1405 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-16 22:47:29 -08001406 if (params_.video.flexfec) {
1407 RTC_DCHECK(!flexfec_receive_configs_.empty());
1408 flexfec_receive_stream =
1409 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1410 }
minyuea27172d2016-11-01 05:59:29 -07001411 video_receive_stream = call->CreateVideoReceiveStream(
1412 video_receive_configs_[stream_id].Copy());
1413 CreateCapturer();
perkj803d97f2016-11-01 11:45:46 -07001414 video_send_stream_->SetSource(
1415 video_capturer_.get(),
1416 VideoSendStream::DegradationPreference::kBalanced);
philipel274c1dc2016-05-04 06:21:01 -07001417 }
1418
minyue73208662016-08-18 06:28:55 -07001419 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001420 if (params_.audio.enabled) {
minyuea27172d2016-11-01 05:59:29 -07001421 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1422 &transport, &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001423 }
1424
palmkviste75f2042016-09-28 06:19:48 -07001425 StartEncodedFrameLogs(video_receive_stream);
1426 StartEncodedFrameLogs(video_send_stream_);
1427
minyue73208662016-08-18 06:28:55 -07001428 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001429 if (params_.video.enabled) {
brandtr1293aca2016-11-16 22:47:29 -08001430 if (flexfec_receive_stream)
1431 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001432 video_receive_stream->Start();
1433 video_send_stream_->Start();
1434 video_capturer_->Start();
1435 }
ivica5d6a06c2015-09-17 05:30:24 -07001436
minyue626bc952016-10-31 05:47:02 -07001437 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001438 // Start receiving audio.
1439 audio_receive_stream->Start();
1440 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001441
1442 // Start sending audio.
1443 audio_send_stream_->Start();
1444 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1445 }
1446
ivica5d6a06c2015-09-17 05:30:24 -07001447 test::PressEnterToContinue();
1448
minyue626bc952016-10-31 05:47:02 -07001449 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001450 // Stop sending audio.
1451 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1452 audio_send_stream_->Stop();
1453
1454 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001455 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1456 audio_receive_stream->Stop();
minyuea27172d2016-11-01 05:59:29 -07001457 call->DestroyAudioSendStream(audio_send_stream_);
1458 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001459 }
1460
1461 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001462 if (params_.video.enabled) {
1463 video_capturer_->Stop();
1464 video_send_stream_->Stop();
1465 video_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001466 if (flexfec_receive_stream) {
1467 flexfec_receive_stream->Stop();
1468 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1469 }
minyuea27172d2016-11-01 05:59:29 -07001470 call->DestroyVideoReceiveStream(video_receive_stream);
1471 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001472 }
1473
ivica5d6a06c2015-09-17 05:30:24 -07001474 transport.StopSending();
minyue626bc952016-10-31 05:47:02 -07001475 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07001476 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001477}
1478
palmkviste75f2042016-09-28 06:19:48 -07001479void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001480 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001481 std::ostringstream str;
1482 str << send_logs_++;
1483 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07001484 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001485 stream->EnableEncodedFrameRecording(
1486 std::vector<rtc::PlatformFile>(
1487 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1488 rtc::CreatePlatformFile(prefix + "2.ivf"),
1489 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1490 10000000);
1491 }
1492}
1493void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001494 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001495 std::ostringstream str;
1496 str << receive_logs_++;
1497 std::string path =
minyue626bc952016-10-31 05:47:02 -07001498 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001499 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1500 10000000);
1501 }
1502}
1503
ivica5d6a06c2015-09-17 05:30:24 -07001504} // namespace webrtc