blob: d72b60c7908818a464cb2d127bbd22551a1ea24c [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"
ivica5d6a06c2015-09-17 05:30:24 -070026#include "webrtc/call.h"
27#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),
374 send_time_ms(0),
375 recv_time_ms(0),
376 render_time_ms(0),
377 encoded_frame_size(0) {}
378
379 FrameComparison(const VideoFrame& reference,
380 const VideoFrame& render,
381 bool dropped,
382 int64_t send_time_ms,
383 int64_t recv_time_ms,
384 int64_t render_time_ms,
385 size_t encoded_frame_size)
386 : reference(reference),
387 render(render),
388 dropped(dropped),
389 send_time_ms(send_time_ms),
390 recv_time_ms(recv_time_ms),
391 render_time_ms(render_time_ms),
392 encoded_frame_size(encoded_frame_size) {}
393
394 VideoFrame reference;
395 VideoFrame render;
396 bool dropped;
397 int64_t send_time_ms;
398 int64_t recv_time_ms;
399 int64_t render_time_ms;
400 size_t encoded_frame_size;
401 };
402
403 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700404 Sample(int dropped,
405 int64_t input_time_ms,
406 int64_t send_time_ms,
407 int64_t recv_time_ms,
408 int64_t render_time_ms,
409 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700410 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700411 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700412 : dropped(dropped),
413 input_time_ms(input_time_ms),
414 send_time_ms(send_time_ms),
415 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700416 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700417 encoded_frame_size(encoded_frame_size),
418 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700419 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700420
ivica8d15bd62015-10-07 02:43:12 -0700421 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;
428 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700429 };
430
Peter Boströme4499152016-02-05 11:13:28 +0100431 // This class receives the send-side OnEncodeTiming and is provided to not
432 // conflict with the receiver-side pre_decode_callback.
433 class OnEncodeTimingProxy : public EncodedFrameObserver {
434 public:
435 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
436
437 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
438 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
439 }
440 void EncodedFrameCallback(const EncodedFrame& frame) override {}
441
442 private:
443 VideoAnalyzer* const parent_;
444 };
445
pbos14fe7082016-04-20 06:35:56 -0700446 // This class receives the send-side OnFrame callback and is provided to not
447 // conflict with the receiver-side renderer callback.
448 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
449 public:
450 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
451
452 void OnFrame(const VideoFrame& video_frame) override {
453 parent_->PreEncodeOnFrame(video_frame);
454 }
455
456 private:
457 VideoAnalyzer* const parent_;
458 };
459
ivica5d6a06c2015-09-17 05:30:24 -0700460 void AddFrameComparison(const VideoFrame& reference,
461 const VideoFrame& render,
462 bool dropped,
463 int64_t render_time_ms)
464 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800465 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
466 int64_t send_time_ms = send_times_[reference_timestamp];
467 send_times_.erase(reference_timestamp);
468 int64_t recv_time_ms = recv_times_[reference_timestamp];
469 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700470
sprangce4aef12015-11-02 07:23:20 -0800471 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800472 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800473 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800474 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800475 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
476 if (it != encoded_frame_sizes_.end())
477 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700478
479 VideoFrame reference_copy;
480 VideoFrame render_copy;
ivica5d6a06c2015-09-17 05:30:24 -0700481
482 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700483 if (comparisons_.size() < kMaxComparisons) {
nisse67dca9f2016-10-31 08:05:50 -0700484 reference_copy = reference;
485 render_copy = render;
stefanb1797672016-08-11 07:00:57 -0700486 } else {
487 // Copy the time to ensure that delay calculations can still be made.
488 reference_copy.set_ntp_time_ms(reference.ntp_time_ms());
489 render_copy.set_ntp_time_ms(render.ntp_time_ms());
490 }
ivica5d6a06c2015-09-17 05:30:24 -0700491 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
492 send_time_ms, recv_time_ms,
493 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100494 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700495 }
496
497 static bool PollStatsThread(void* obj) {
498 return static_cast<VideoAnalyzer*>(obj)->PollStats();
499 }
500
501 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100502 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100503 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700504
505 VideoSendStream::Stats stats = send_stream_->GetStats();
506
507 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100508 // It's not certain that we yet have estimates for any of these stats. Check
509 // that they are positive before mixing them in.
510 if (stats.encode_frame_rate > 0)
511 encode_frame_rate_.AddSample(stats.encode_frame_rate);
512 if (stats.avg_encode_time_ms > 0)
513 encode_time_ms.AddSample(stats.avg_encode_time_ms);
514 if (stats.encode_usage_percent > 0)
515 encode_usage_percent.AddSample(stats.encode_usage_percent);
516 if (stats.media_bitrate_bps > 0)
517 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700518
519 return true;
520 }
521
522 static bool FrameComparisonThread(void* obj) {
523 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
524 }
525
526 bool CompareFrames() {
527 if (AllFramesRecorded())
528 return false;
529
530 VideoFrame reference;
531 VideoFrame render;
532 FrameComparison comparison;
533
534 if (!PopComparison(&comparison)) {
535 // Wait until new comparison task is available, or test is done.
536 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100537 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700538 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100539 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700540 return false;
541 }
542 return true; // Try again.
543 }
544
545 PerformFrameComparison(comparison);
546
547 if (FrameProcessed()) {
548 PrintResults();
549 if (graph_data_output_file_)
550 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100551 done_.Set();
552 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700553 return false;
554 }
555
556 return true;
557 }
558
559 bool PopComparison(FrameComparison* comparison) {
560 rtc::CritScope crit(&comparison_lock_);
561 // If AllFramesRecorded() is true, it means we have already popped
562 // frames_to_process_ frames from comparisons_, so there is no more work
563 // for this thread to be done. frames_processed_ might still be lower if
564 // all comparisons are not done, but those frames are currently being
565 // worked on by other threads.
566 if (comparisons_.empty() || AllFramesRecorded())
567 return false;
568
569 *comparison = comparisons_.front();
570 comparisons_.pop_front();
571
572 FrameRecorded();
573 return true;
574 }
575
576 // Increment counter for number of frames received for comparison.
577 void FrameRecorded() {
578 rtc::CritScope crit(&comparison_lock_);
579 ++frames_recorded_;
580 }
581
582 // Returns true if all frames to be compared have been taken from the queue.
583 bool AllFramesRecorded() {
584 rtc::CritScope crit(&comparison_lock_);
585 assert(frames_recorded_ <= frames_to_process_);
586 return frames_recorded_ == frames_to_process_;
587 }
588
589 // Increase count of number of frames processed. Returns true if this was the
590 // last frame to be processed.
591 bool FrameProcessed() {
592 rtc::CritScope crit(&comparison_lock_);
593 ++frames_processed_;
594 assert(frames_processed_ <= frames_to_process_);
595 return frames_processed_ == frames_to_process_;
596 }
597
598 void PrintResults() {
599 rtc::CritScope crit(&comparison_lock_);
600 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800601 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700602 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700603 PrintResult("receiver_time", receiver_time_, " ms");
604 PrintResult("total_delay_incl_network", end_to_end_, " ms");
605 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
606 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
607 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
608 PrintResult("encode_time", encode_time_ms, " ms");
609 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
610 PrintResult("media_bitrate", media_bitrate_bps, " bps");
611
pbos14fe7082016-04-20 06:35:56 -0700612 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
613 dropped_frames_);
614 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
615 test_label_.c_str(), dropped_frames_before_first_encode_);
616 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
617 test_label_.c_str(), dropped_frames_before_rendering_);
618
ivica5d6a06c2015-09-17 05:30:24 -0700619 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
620 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
621 }
622
623 void PerformFrameComparison(const FrameComparison& comparison) {
624 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700625 double psnr = -1.0;
626 double ssim = -1.0;
627 if (!comparison.reference.IsZeroSize()) {
628 psnr = I420PSNR(&comparison.reference, &comparison.render);
629 ssim = I420SSIM(&comparison.reference, &comparison.render);
630 }
ivica5d6a06c2015-09-17 05:30:24 -0700631
632 int64_t input_time_ms = comparison.reference.ntp_time_ms();
633
634 rtc::CritScope crit(&comparison_lock_);
635 if (graph_data_output_file_) {
636 samples_.push_back(
637 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700638 comparison.recv_time_ms, comparison.render_time_ms,
639 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700640 }
stefanb1797672016-08-11 07:00:57 -0700641 if (psnr >= 0.0)
642 psnr_.AddSample(psnr);
643 if (ssim >= 0.0)
644 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700645
646 if (comparison.dropped) {
647 ++dropped_frames_;
648 return;
649 }
650 if (last_render_time_ != 0)
651 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
652 last_render_time_ = comparison.render_time_ms;
653
654 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
655 receiver_time_.AddSample(comparison.render_time_ms -
656 comparison.recv_time_ms);
657 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
658 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
659 }
660
661 void PrintResult(const char* result_type,
662 test::Statistics stats,
663 const char* unit) {
664 printf("RESULT %s: %s = {%f, %f}%s\n",
665 result_type,
666 test_label_.c_str(),
667 stats.Mean(),
668 stats.StandardDeviation(),
669 unit);
670 }
671
672 void PrintSamplesToFile(void) {
673 FILE* out = graph_data_output_file_;
674 rtc::CritScope crit(&comparison_lock_);
675 std::sort(samples_.begin(), samples_.end(),
676 [](const Sample& A, const Sample& B) -> bool {
677 return A.input_time_ms < B.input_time_ms;
678 });
679
sprangce4aef12015-11-02 07:23:20 -0800680 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700681 fprintf(out, "%" PRIuS "\n", samples_.size());
682 fprintf(out,
683 "dropped "
684 "input_time_ms "
685 "send_time_ms "
686 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700687 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700688 "encoded_frame_size "
689 "psnr "
690 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700691 "encode_time_ms\n");
692 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700693 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700694 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
695 int encode_time_ms;
696 if (it != samples_encode_time_ms_.end()) {
697 encode_time_ms = it->second;
698 } else {
699 ++missing_encode_time_samples;
700 encode_time_ms = -1;
701 }
702 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
703 " %lf %lf %d\n",
704 sample.dropped, sample.input_time_ms, sample.send_time_ms,
705 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700706 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700707 encode_time_ms);
708 }
709 if (missing_encode_time_samples) {
710 fprintf(stderr,
711 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
712 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700713 }
714 }
715
perkja49cbd32016-09-16 07:53:41 -0700716 // Implements VideoSinkInterface to receive captured frames from a
717 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
718 // as a source to VideoSendStream.
719 // It forwards all input frames to the VideoAnalyzer for later comparison and
720 // forwards the captured frames to the VideoSendStream.
721 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
722 public rtc::VideoSourceInterface<VideoFrame> {
723 public:
724 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
725 : analyzer_(analyzer), send_stream_input_(nullptr) {}
726
727 private:
728 void OnFrame(const VideoFrame& video_frame) override {
729 VideoFrame copy = video_frame;
730 copy.set_timestamp(copy.ntp_time_ms() * 90);
731
732 analyzer_->AddCapturedFrameForComparison(video_frame);
733 rtc::CritScope lock(&crit_);
734 if (send_stream_input_)
735 send_stream_input_->OnFrame(video_frame);
736 }
737
738 // Called when |send_stream_.SetSource()| is called.
739 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
740 const rtc::VideoSinkWants& wants) override {
741 rtc::CritScope lock(&crit_);
742 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
743 send_stream_input_ = sink;
744 }
745
746 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
747 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
748 rtc::CritScope lock(&crit_);
749 RTC_DCHECK(sink == send_stream_input_);
750 send_stream_input_ = nullptr;
751 }
752
753 VideoAnalyzer* const analyzer_;
754 rtc::CriticalSection crit_;
755 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
756 };
757
758 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
759 rtc::CritScope lock(&crit_);
kwibergaf476c72016-11-28 15:21:39 -0800760 RTC_DCHECK_EQ(0, video_frame.timestamp());
perkja49cbd32016-09-16 07:53:41 -0700761 // Frames from the capturer does not have a rtp timestamp. Create one so it
762 // can be used for comparison.
763 VideoFrame copy = video_frame;
764 copy.set_timestamp(copy.ntp_time_ms() * 90);
765 frames_.push_back(copy);
766 }
767
768 VideoSendStream* send_stream_;
769 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700770 const std::string test_label_;
771 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800772 const std::string graph_title_;
773 const uint32_t ssrc_to_analyze_;
pbos14fe7082016-04-20 06:35:56 -0700774 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100775 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700776 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700777 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700778 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
779 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
780 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
781 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
782 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
783 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
784 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
785 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
786 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
787 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
788 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
789
790 const int frames_to_process_;
791 int frames_recorded_;
792 int frames_processed_;
793 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700794 int dropped_frames_before_first_encode_;
795 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700796 int64_t last_render_time_;
797 uint32_t rtp_timestamp_delta_;
798
799 rtc::CriticalSection crit_;
800 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700801 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800802 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
803 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
804 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
805 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 03:25:36 -0700806 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700807 const double avg_psnr_threshold_;
808 const double avg_ssim_threshold_;
809
810 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100811 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
812 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100813 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700814 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100815 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700816};
817
palmkviste75f2042016-09-28 06:19:48 -0700818VideoQualityTest::VideoQualityTest()
819 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 05:30:24 -0700820
minyue626bc952016-10-31 05:47:02 -0700821VideoQualityTest::Params::Params()
822 : call({false, Call::Config::BitrateConfig()}),
823 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -0800824 false, "", ""}),
minyue626bc952016-10-31 05:47:02 -0700825 audio({false, false}),
826 screenshare({false, 10, 0}),
827 analyzer({"", 0.0, 0.0, 0, "", ""}),
828 pipe(),
829 logs(false),
830 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}) {}
831
832VideoQualityTest::Params::~Params() = default;
833
ivica5d6a06c2015-09-17 05:30:24 -0700834void VideoQualityTest::TestBody() {}
835
sprangce4aef12015-11-02 07:23:20 -0800836std::string VideoQualityTest::GenerateGraphTitle() const {
837 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -0700838 ss << params_.video.codec;
839 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
840 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -0800841 if (params_.screenshare.scroll_duration)
842 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
843 if (params_.ss.streams.size() > 1)
844 ss << ", Stream #" << params_.ss.selected_stream;
845 if (params_.ss.num_spatial_layers > 1)
846 ss << ", Layer #" << params_.ss.selected_sl;
847 ss << ")";
848 return ss.str();
849}
850
851void VideoQualityTest::CheckParams() {
852 // Add a default stream in none specified.
853 if (params_.ss.streams.empty())
854 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
855 if (params_.ss.num_spatial_layers == 0)
856 params_.ss.num_spatial_layers = 1;
857
858 if (params_.pipe.loss_percent != 0 ||
859 params_.pipe.queue_length_packets != 0) {
860 // Since LayerFilteringTransport changes the sequence numbers, we can't
861 // use that feature with pack loss, since the NACK request would end up
862 // retransmitting the wrong packets.
863 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800864 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -0700865 RTC_CHECK(params_.video.selected_tl == -1 ||
866 params_.video.selected_tl ==
867 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800868 }
869
870 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
871 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -0700872 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
873 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
874 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -0800875 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
876 for (const VideoStream& stream : params_.ss.streams) {
877 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
878 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
879 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
kwiberg352444f2016-11-28 15:58:53 -0800880 RTC_CHECK_EQ(stream.temporal_layer_thresholds_bps.size(),
minyue626bc952016-10-31 05:47:02 -0700881 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800882 }
883 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
884 // the total bitrate? We anyway have to update them in the case bitrate
885 // estimator changes the total bitrates.
886 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
887 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
888 RTC_CHECK(params_.ss.spatial_layers.empty() ||
889 params_.ss.spatial_layers.size() ==
890 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -0700891 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -0800892 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -0700893 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -0800894 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -0800895 }
896}
897
898// Static.
899std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
900 // Parse comma separated nonnegative integers, where some elements may be
901 // empty. The empty values are replaced with -1.
902 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
903 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
904 std::vector<int> result;
905 if (str.empty())
906 return result;
907
908 const char* p = str.c_str();
909 int value = -1;
910 int pos;
911 while (*p) {
912 if (*p == ',') {
913 result.push_back(value);
914 value = -1;
915 ++p;
916 continue;
917 }
918 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
919 << "Unexpected non-number value.";
920 p += pos;
921 }
922 result.push_back(value);
923 return result;
924}
925
926// Static.
927VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
928 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -0700929 stream.width = params.video.width;
930 stream.height = params.video.height;
931 stream.max_framerate = params.video.fps;
932 stream.min_bitrate_bps = params.video.min_bitrate_bps;
933 stream.target_bitrate_bps = params.video.target_bitrate_bps;
934 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -0800935 stream.max_qp = 52;
minyue626bc952016-10-31 05:47:02 -0700936 if (params.video.num_temporal_layers == 2)
sprangce4aef12015-11-02 07:23:20 -0800937 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
938 return stream;
939}
940
941// Static.
942void VideoQualityTest::FillScalabilitySettings(
943 Params* params,
944 const std::vector<std::string>& stream_descriptors,
945 size_t selected_stream,
946 int num_spatial_layers,
947 int selected_sl,
948 const std::vector<std::string>& sl_descriptors) {
949 // Read VideoStream and SpatialLayer elements from a list of comma separated
950 // lists. To use a default value for an element, use -1 or leave empty.
951 // Validity checks performed in CheckParams.
952
953 RTC_CHECK(params->ss.streams.empty());
954 for (auto descriptor : stream_descriptors) {
955 if (descriptor.empty())
956 continue;
957 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
958 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
959 if (v[0] != -1)
960 stream.width = static_cast<size_t>(v[0]);
961 if (v[1] != -1)
962 stream.height = static_cast<size_t>(v[1]);
963 if (v[2] != -1)
964 stream.max_framerate = v[2];
965 if (v[3] != -1)
966 stream.min_bitrate_bps = v[3];
967 if (v[4] != -1)
968 stream.target_bitrate_bps = v[4];
969 if (v[5] != -1)
970 stream.max_bitrate_bps = v[5];
971 if (v.size() > 6 && v[6] != -1)
972 stream.max_qp = v[6];
973 if (v.size() > 7) {
974 stream.temporal_layer_thresholds_bps.clear();
975 stream.temporal_layer_thresholds_bps.insert(
976 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
977 } else {
978 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 05:47:02 -0700979 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -0800980 }
981 params->ss.streams.push_back(stream);
982 }
983 params->ss.selected_stream = selected_stream;
984
985 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
986 params->ss.selected_sl = selected_sl;
987 RTC_CHECK(params->ss.spatial_layers.empty());
988 for (auto descriptor : sl_descriptors) {
989 if (descriptor.empty())
990 continue;
991 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
992 RTC_CHECK_GT(v[2], 0);
993
994 SpatialLayer layer;
995 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
996 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
997 layer.target_bitrate_bps = v[2];
998 params->ss.spatial_layers.push_back(layer);
999 }
1000}
1001
minyuea27172d2016-11-01 05:59:29 -07001002void VideoQualityTest::SetupVideo(Transport* send_transport,
1003 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001004 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001005 trace_to_stderr_.reset(new test::TraceToStderr);
1006
sprangce4aef12015-11-02 07:23:20 -08001007 size_t num_streams = params_.ss.streams.size();
brandtr841de6a2016-11-15 07:10:52 -08001008 CreateSendConfig(num_streams, 0, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001009
1010 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001011 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001012 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001013 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001014 } else if (params_.video.codec == "VP8") {
magjed509e4fe2016-11-18 01:34:11 -08001015 video_encoder_.reset(VP8Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001016 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001017 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001018 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001019 payload_type = kPayloadTypeVP9;
1020 } else {
1021 RTC_NOTREACHED() << "Codec not supported!";
1022 return;
1023 }
minyuea27172d2016-11-01 05:59:29 -07001024 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001025 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001026 video_send_config_.encoder_settings.payload_type = payload_type;
1027 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1028 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -08001029 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001030 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001031
stefanff483612015-12-21 03:14:00 -08001032 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001033 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001034 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001035 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001036 test::kTransportSequenceNumberExtensionId));
1037 } else {
stefanff483612015-12-21 03:14:00 -08001038 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001039 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001040 }
1041
stefanff483612015-12-21 03:14:00 -08001042 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001043 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001044
brandtr1293aca2016-11-16 22:47:29 -08001045 video_send_config_.suspend_below_min_bitrate =
1046 params_.video.suspend_below_min_bitrate;
1047
perkjfa10b552016-10-02 23:45:26 -07001048 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1049 video_encoder_config_.max_bitrate_bps = 0;
1050 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1051 video_encoder_config_.max_bitrate_bps +=
1052 params_.ss.streams[i].max_bitrate_bps;
1053 }
1054 video_encoder_config_.video_stream_factory =
1055 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1056
stefanff483612015-12-21 03:14:00 -08001057 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001058
1059 CreateMatchingReceiveConfigs(recv_transport);
1060
sprangce4aef12015-11-02 07:23:20 -08001061 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001062 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +01001063 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
1064 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -08001065 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001066 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -08001067 }
brandtr1293aca2016-11-16 22:47:29 -08001068
1069 if (params_.video.flexfec) {
1070 video_send_config_.rtp.flexfec.flexfec_payload_type = kFlexfecPayloadType;
1071 video_send_config_.rtp.flexfec.flexfec_ssrc = kFlexfecSendSsrc;
1072 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1073 kVideoSendSsrcs[params_.ss.selected_stream]};
1074
1075 FlexfecReceiveStream::Config flexfec_receive_config;
1076 flexfec_receive_config.flexfec_payload_type =
1077 video_send_config_.rtp.flexfec.flexfec_payload_type;
1078 flexfec_receive_config.flexfec_ssrc =
1079 video_send_config_.rtp.flexfec.flexfec_ssrc;
1080 flexfec_receive_config.protected_media_ssrcs =
1081 video_send_config_.rtp.flexfec.protected_media_ssrcs;
1082 flexfec_receive_configs_.push_back(flexfec_receive_config);
1083 }
1084
1085 if (params_.video.ulpfec) {
1086 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1087 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1088 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1089
1090 video_receive_configs_[params_.ss.selected_stream]
1091 .rtp.ulpfec.red_payload_type =
1092 video_send_config_.rtp.ulpfec.red_payload_type;
1093 video_receive_configs_[params_.ss.selected_stream]
1094 .rtp.ulpfec.ulpfec_payload_type =
1095 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1096 video_receive_configs_[params_.ss.selected_stream]
1097 .rtp.ulpfec.red_rtx_payload_type =
1098 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1099 }
ivica5d6a06c2015-09-17 05:30:24 -07001100}
1101
sprangce4aef12015-11-02 07:23:20 -08001102void VideoQualityTest::SetupScreenshare() {
1103 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001104
1105 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -08001106 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
minyue626bc952016-10-31 05:47:02 -07001107 if (params_.video.codec == "VP8") {
kthelgason29a44e32016-09-27 03:52:02 -07001108 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1109 vp8_settings.denoisingOn = false;
1110 vp8_settings.frameDroppingOn = false;
1111 vp8_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001112 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001113 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1114 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
minyue626bc952016-10-31 05:47:02 -07001115 } else if (params_.video.codec == "VP9") {
kthelgason29a44e32016-09-27 03:52:02 -07001116 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1117 vp9_settings.denoisingOn = false;
1118 vp9_settings.frameDroppingOn = false;
1119 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001120 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001121 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001122 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001123 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1124 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001125 }
1126
1127 // Setup frame generator.
1128 const size_t kWidth = 1850;
1129 const size_t kHeight = 1110;
1130 std::vector<std::string> slides;
1131 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1132 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1133 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1134 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1135
sprangce4aef12015-11-02 07:23:20 -08001136 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -07001137 // Cycle image every slide_change_interval seconds.
1138 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
1139 slides, kWidth, kHeight,
minyue626bc952016-10-31 05:47:02 -07001140 params_.screenshare.slide_change_interval * params_.video.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001141 } else {
minyue626bc952016-10-31 05:47:02 -07001142 RTC_CHECK_LE(params_.video.width, kWidth);
1143 RTC_CHECK_LE(params_.video.height, kHeight);
sprangce4aef12015-11-02 07:23:20 -08001144 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1145 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1146 params_.screenshare.scroll_duration) *
1147 1000;
1148 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1149 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -07001150
sprangce4aef12015-11-02 07:23:20 -08001151 frame_generator_.reset(
1152 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
minyue626bc952016-10-31 05:47:02 -07001153 clock_, slides, kWidth, kHeight, params_.video.width,
1154 params_.video.height, params_.screenshare.scroll_duration * 1000,
sprangce4aef12015-11-02 07:23:20 -08001155 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -07001156 }
1157}
1158
perkja49cbd32016-09-16 07:53:41 -07001159void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001160 if (params_.screenshare.enabled) {
1161 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja49cbd32016-09-16 07:53:41 -07001162 new test::FrameGeneratorCapturer(clock_, frame_generator_.release(),
minyue626bc952016-10-31 05:47:02 -07001163 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001164 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001165 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001166 } else {
sprangce4aef12015-11-02 07:23:20 -08001167 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001168 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 05:47:02 -07001169 params_.video.width, params_.video.height, params_.video.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001170 } else {
minyuea27172d2016-11-01 05:59:29 -07001171 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001172 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001173 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001174 clock_));
minyuea27172d2016-11-01 05:59:29 -07001175 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1176 << params_.video.clip_name
1177 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001178 }
1179 }
1180}
1181
sprang7a975f72015-10-12 06:33:21 -07001182void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001183 params_ = params;
1184
minyue626bc952016-10-31 05:47:02 -07001185 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001186 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1187 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001188 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001189
1190 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001191 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001192 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001193 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001194 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001195 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1196 << "!";
ivica87f83a92015-10-08 05:13:32 -07001197 }
sprang7a975f72015-10-12 06:33:21 -07001198
skvlad11a9cbf2016-10-07 11:53:05 -07001199 webrtc::RtcEventLogNullImpl event_log;
1200 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001201 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001202 CreateCalls(call_config, call_config);
1203
ivica87f83a92015-10-08 05:13:32 -07001204 test::LayerFilteringTransport send_transport(
minyue626bc952016-10-31 05:47:02 -07001205 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1206 params_.video.selected_tl, params_.ss.selected_sl);
1207 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get());
stefanf116bd02015-10-27 08:29:42 -07001208
sprangce4aef12015-11-02 07:23:20 -08001209 std::string graph_title = params_.analyzer.graph_title;
1210 if (graph_title.empty())
1211 graph_title = VideoQualityTest::GenerateGraphTitle();
1212
1213 // In the case of different resolutions, the functions calculating PSNR and
1214 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1215 // aborts if the average psnr/ssim are below the given threshold, which is
1216 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1217 // abort.
1218 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1219 int selected_sl = params_.ss.selected_sl != -1
1220 ? params_.ss.selected_sl
1221 : params_.ss.num_spatial_layers - 1;
1222 bool disable_quality_check =
minyue626bc952016-10-31 05:47:02 -07001223 selected_stream.width != params_.video.width ||
1224 selected_stream.height != params_.video.height ||
sprangce4aef12015-11-02 07:23:20 -08001225 (!params_.ss.spatial_layers.empty() &&
1226 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1227 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1228 if (disable_quality_check) {
1229 fprintf(stderr,
1230 "Warning: Calculating PSNR and SSIM for downsized resolution "
1231 "not implemented yet! Skipping PSNR and SSIM calculations!");
1232 }
1233
ivica5d6a06c2015-09-17 05:30:24 -07001234 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001235 &send_transport, params_.analyzer.test_label,
1236 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1237 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
minyue626bc952016-10-31 05:47:02 -07001238 params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001239 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001240 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001241
ivica5d6a06c2015-09-17 05:30:24 -07001242 analyzer.SetReceiver(receiver_call_->Receiver());
1243 send_transport.SetReceiver(&analyzer);
1244 recv_transport.SetReceiver(sender_call_->Receiver());
1245
minyuea27172d2016-11-01 05:59:29 -07001246 SetupVideo(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001247 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001248 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
stefanff483612015-12-21 03:14:00 -08001249 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001250 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001251 RTC_DCHECK(!video_send_config_.post_encode_callback);
1252 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001253
sprangce4aef12015-11-02 07:23:20 -08001254 if (params_.screenshare.enabled)
1255 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001256
brandtr1293aca2016-11-16 22:47:29 -08001257 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001258 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001259 analyzer.SetSendStream(video_send_stream_);
perkj803d97f2016-11-01 11:45:46 -07001260 video_send_stream_->SetSource(
1261 analyzer.OutputInterface(),
1262 VideoSendStream::DegradationPreference::kBalanced);
ivica5d6a06c2015-09-17 05:30:24 -07001263
perkja49cbd32016-09-16 07:53:41 -07001264 CreateCapturer();
1265 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 05:59:29 -07001266 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001267
palmkviste75f2042016-09-28 06:19:48 -07001268 StartEncodedFrameLogs(video_send_stream_);
1269 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001270 video_send_stream_->Start();
1271 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1272 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001273 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1274 receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001275 video_capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001276
1277 analyzer.Wait();
1278
1279 send_transport.StopSending();
1280 recv_transport.StopSending();
1281
minyuea27172d2016-11-01 05:59:29 -07001282 video_capturer_->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001283 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1284 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001285 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1286 receive_stream->Stop();
1287 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001288
1289 DestroyStreams();
1290
1291 if (graph_data_output_file)
1292 fclose(graph_data_output_file);
1293}
1294
minyuea27172d2016-11-01 05:59:29 -07001295void VideoQualityTest::SetupAudio(int send_channel_id,
1296 int receive_channel_id,
1297 Call* call,
1298 Transport* transport,
1299 AudioReceiveStream** audio_receive_stream) {
1300 audio_send_config_ = AudioSendStream::Config(transport);
1301 audio_send_config_.voe_channel_id = send_channel_id;
1302 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1303
1304 // Add extension to enable audio send side BWE, and allow audio bit rate
1305 // adaptation.
1306 audio_send_config_.rtp.extensions.clear();
1307 if (params_.call.send_side_bwe) {
1308 audio_send_config_.rtp.extensions.push_back(
1309 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1310 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001311 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1312 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001313 }
1314 audio_send_config_.send_codec_spec.codec_inst =
1315 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
1316
1317 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1318
1319 AudioReceiveStream::Config audio_config;
1320 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1321 audio_config.rtcp_send_transport = transport;
1322 audio_config.voe_channel_id = receive_channel_id;
1323 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1324 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1325 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1326 audio_config.decoder_factory = decoder_factory_;
1327 if (params_.video.enabled && params_.audio.sync_video)
1328 audio_config.sync_group = kSyncGroup;
1329
1330 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1331}
1332
minyue73208662016-08-18 06:28:55 -07001333void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001334 params_ = params;
1335 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001336
ivica5d6a06c2015-09-17 05:30:24 -07001337 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1338 // match the full stack tests.
skvlad11a9cbf2016-10-07 11:53:05 -07001339 webrtc::RtcEventLogNullImpl event_log;
1340 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 05:47:02 -07001341 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001342
1343 ::VoiceEngineState voe;
minyue626bc952016-10-31 05:47:02 -07001344 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001345 CreateVoiceEngine(&voe, decoder_factory_);
1346 AudioState::Config audio_state_config;
1347 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001348 audio_state_config.audio_mixer = AudioMixerImpl::Create();
minyue73208662016-08-18 06:28:55 -07001349 call_config.audio_state = AudioState::Create(audio_state_config);
1350 }
1351
kwiberg27f982b2016-03-01 11:52:33 -08001352 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001353
minyuea27172d2016-11-01 05:59:29 -07001354 // TODO(minyue): consider if this is a good transport even for audio only
1355 // calls.
ivica5d6a06c2015-09-17 05:30:24 -07001356 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001357 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue626bc952016-10-31 05:47:02 -07001358 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001359 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1360 // least share as much code as possible. That way this test would also match
1361 // the full stack tests better.
1362 transport.SetReceiver(call->Receiver());
1363
minyuea27172d2016-11-01 05:59:29 -07001364 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-16 22:47:29 -08001365 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 05:59:29 -07001366 std::unique_ptr<test::VideoRenderer> local_preview;
1367 std::unique_ptr<test::VideoRenderer> loopback_video;
1368 if (params_.video.enabled) {
1369 // Create video renderers.
1370 local_preview.reset(test::VideoRenderer::Create(
1371 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001372
minyuea27172d2016-11-01 05:59:29 -07001373 size_t stream_id = params_.ss.selected_stream;
1374 std::string title = "Loopback Video";
1375 if (params_.ss.streams.size() > 1) {
1376 std::ostringstream s;
1377 s << stream_id;
1378 title += " - Stream #" + s.str();
1379 }
sprangce4aef12015-11-02 07:23:20 -08001380
minyuea27172d2016-11-01 05:59:29 -07001381 loopback_video.reset(test::VideoRenderer::Create(
1382 title.c_str(), params_.ss.streams[stream_id].width,
1383 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 13:03:59 +02001384
minyuea27172d2016-11-01 05:59:29 -07001385 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 05:59:29 -07001386 video_send_config_.pre_encode_callback = local_preview.get();
1387 video_receive_configs_[stream_id].renderer = loopback_video.get();
1388 if (params_.audio.enabled && params_.audio.sync_video)
1389 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1390
minyuea27172d2016-11-01 05:59:29 -07001391 if (params_.screenshare.enabled)
1392 SetupScreenshare();
1393
1394 video_send_stream_ = call->CreateVideoSendStream(
1395 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-16 22:47:29 -08001396 if (params_.video.flexfec) {
1397 RTC_DCHECK(!flexfec_receive_configs_.empty());
1398 flexfec_receive_stream =
1399 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1400 }
minyuea27172d2016-11-01 05:59:29 -07001401 video_receive_stream = call->CreateVideoReceiveStream(
1402 video_receive_configs_[stream_id].Copy());
1403 CreateCapturer();
perkj803d97f2016-11-01 11:45:46 -07001404 video_send_stream_->SetSource(
1405 video_capturer_.get(),
1406 VideoSendStream::DegradationPreference::kBalanced);
philipel274c1dc2016-05-04 06:21:01 -07001407 }
1408
minyue73208662016-08-18 06:28:55 -07001409 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001410 if (params_.audio.enabled) {
minyuea27172d2016-11-01 05:59:29 -07001411 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1412 &transport, &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001413 }
1414
palmkviste75f2042016-09-28 06:19:48 -07001415 StartEncodedFrameLogs(video_receive_stream);
1416 StartEncodedFrameLogs(video_send_stream_);
1417
minyue73208662016-08-18 06:28:55 -07001418 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001419 if (params_.video.enabled) {
brandtr1293aca2016-11-16 22:47:29 -08001420 if (flexfec_receive_stream)
1421 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001422 video_receive_stream->Start();
1423 video_send_stream_->Start();
1424 video_capturer_->Start();
1425 }
ivica5d6a06c2015-09-17 05:30:24 -07001426
minyue626bc952016-10-31 05:47:02 -07001427 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001428 // Start receiving audio.
1429 audio_receive_stream->Start();
1430 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001431
1432 // Start sending audio.
1433 audio_send_stream_->Start();
1434 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1435 }
1436
ivica5d6a06c2015-09-17 05:30:24 -07001437 test::PressEnterToContinue();
1438
minyue626bc952016-10-31 05:47:02 -07001439 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001440 // Stop sending audio.
1441 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1442 audio_send_stream_->Stop();
1443
1444 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001445 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1446 audio_receive_stream->Stop();
minyuea27172d2016-11-01 05:59:29 -07001447 call->DestroyAudioSendStream(audio_send_stream_);
1448 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001449 }
1450
1451 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001452 if (params_.video.enabled) {
1453 video_capturer_->Stop();
1454 video_send_stream_->Stop();
1455 video_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001456 if (flexfec_receive_stream) {
1457 flexfec_receive_stream->Stop();
1458 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1459 }
minyuea27172d2016-11-01 05:59:29 -07001460 call->DestroyVideoReceiveStream(video_receive_stream);
1461 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001462 }
1463
ivica5d6a06c2015-09-17 05:30:24 -07001464 transport.StopSending();
minyue626bc952016-10-31 05:47:02 -07001465 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07001466 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001467}
1468
palmkviste75f2042016-09-28 06:19:48 -07001469void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001470 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001471 std::ostringstream str;
1472 str << send_logs_++;
1473 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07001474 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07001475 stream->EnableEncodedFrameRecording(
1476 std::vector<rtc::PlatformFile>(
1477 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1478 rtc::CreatePlatformFile(prefix + "2.ivf"),
1479 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1480 10000000);
1481 }
1482}
1483void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07001484 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07001485 std::ostringstream str;
1486 str << receive_logs_++;
1487 std::string path =
minyue626bc952016-10-31 05:47:02 -07001488 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07001489 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1490 10000000);
1491 }
1492}
1493
ivica5d6a06c2015-09-17 05:30:24 -07001494} // namespace webrtc