blob: 1ae2e16a883127e7cf3f381a2949824c4d386e6d [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>
ilnikcb8c1462017-03-09 09:23:30 -080016#include <set>
sprangce4aef12015-11-02 07:23:20 -080017#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080018#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070019#include <vector>
20
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020021#include "webrtc/base/checks.h"
22#include "webrtc/base/cpu_time.h"
23#include "webrtc/base/event.h"
24#include "webrtc/base/format_macros.h"
25#include "webrtc/base/memory_usage.h"
26#include "webrtc/base/optional.h"
27#include "webrtc/base/platform_file.h"
28#include "webrtc/base/timeutils.h"
ossuf515ab82016-12-07 04:52:58 -080029#include "webrtc/call/call.h"
ivica5d6a06c2015-09-17 05:30:24 -070030#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 11:53:05 -070031#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
sprang67561a62017-06-15 06:34:42 -070032#include "webrtc/media/engine/webrtcvideoengine.h"
aleloi10111bc2016-11-17 06:48:48 -080033#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010034#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
ilnik1e7732c2017-02-23 05:07:56 -080035#include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
sprangce4aef12015-11-02 07:23:20 -080036#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
magjed509e4fe2016-11-18 01:34:11 -080037#include "webrtc/modules/video_coding/codecs/h264/include/h264.h"
38#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
ilnikcb8c1462017-03-09 09:23:30 -080039#include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
magjed509e4fe2016-11-18 01:34:11 -080040#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010041#include "webrtc/system_wrappers/include/cpu_info.h"
ilnik9ae0d762017-02-15 00:53:12 -080042#include "webrtc/system_wrappers/include/field_trial.h"
kwibergac9f8762016-09-30 22:29:43 -070043#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070044#include "webrtc/test/layer_filtering_transport.h"
45#include "webrtc/test/run_loop.h"
46#include "webrtc/test/statistics.h"
47#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 07:53:41 -070048#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070049#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070050#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 06:28:55 -070051
52namespace {
53
54constexpr int kSendStatsPollingIntervalMs = 1000;
55constexpr int kPayloadTypeH264 = 122;
56constexpr int kPayloadTypeVP8 = 123;
57constexpr int kPayloadTypeVP9 = 124;
minyue20c84cc2017-04-10 16:57:57 -070058
minyue73208662016-08-18 06:28:55 -070059constexpr size_t kMaxComparisons = 10;
60constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 09:29:22 -080061constexpr int kOpusMinBitrateBps = 6000;
62constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 00:53:12 -080063constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 04:21:04 -080064constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
65constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 06:28:55 -070066
sprang1168fd42017-06-21 09:00:17 -070067constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
68
minyue73208662016-08-18 06:28:55 -070069struct VoiceEngineState {
70 VoiceEngineState()
71 : voice_engine(nullptr),
72 base(nullptr),
minyue73208662016-08-18 06:28:55 -070073 send_channel_id(-1),
74 receive_channel_id(-1) {}
75
76 webrtc::VoiceEngine* voice_engine;
77 webrtc::VoEBase* base;
minyue73208662016-08-18 06:28:55 -070078 int send_channel_id;
79 int receive_channel_id;
80};
81
peaha9cc40b2017-06-29 08:32:09 -070082void CreateVoiceEngine(
83 VoiceEngineState* voe,
84 webrtc::AudioProcessing* apm,
85 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
minyue73208662016-08-18 06:28:55 -070086 voe->voice_engine = webrtc::VoiceEngine::Create();
87 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
peaha9cc40b2017-06-29 08:32:09 -070088 EXPECT_EQ(0, voe->base->Init(nullptr, apm, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070089 webrtc::VoEBase::ChannelConfig config;
90 config.enable_voice_pacing = true;
91 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070092 EXPECT_GE(voe->send_channel_id, 0);
93 voe->receive_channel_id = voe->base->CreateChannel();
94 EXPECT_GE(voe->receive_channel_id, 0);
95}
96
97void DestroyVoiceEngine(VoiceEngineState* voe) {
98 voe->base->DeleteChannel(voe->send_channel_id);
99 voe->send_channel_id = -1;
100 voe->base->DeleteChannel(voe->receive_channel_id);
101 voe->receive_channel_id = -1;
102 voe->base->Release();
103 voe->base = nullptr;
minyue73208662016-08-18 06:28:55 -0700104
105 webrtc::VoiceEngine::Delete(voe->voice_engine);
106 voe->voice_engine = nullptr;
107}
108
perkjfa10b552016-10-02 23:45:26 -0700109class VideoStreamFactory
110 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
111 public:
112 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
113 : streams_(streams) {}
114
115 private:
116 std::vector<webrtc::VideoStream> CreateEncoderStreams(
117 int width,
118 int height,
119 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 07:24:33 -0800120 // The highest layer must match the incoming resolution.
121 std::vector<webrtc::VideoStream> streams = streams_;
122 streams[streams_.size() - 1].height = height;
123 streams[streams_.size() - 1].width = width;
124 return streams;
perkjfa10b552016-10-02 23:45:26 -0700125 }
126
127 std::vector<webrtc::VideoStream> streams_;
128};
129
brandtr504b95e2016-12-21 02:54:35 -0800130bool IsFlexfec(int payload_type) {
131 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
132}
133
minyue73208662016-08-18 06:28:55 -0700134} // namespace
ivica5d6a06c2015-09-17 05:30:24 -0700135
136namespace webrtc {
137
ivica5d6a06c2015-09-17 05:30:24 -0700138class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700139 public Transport,
ilnik1e7732c2017-02-23 05:07:56 -0800140 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 05:30:24 -0700141 public:
sprangce4aef12015-11-02 07:23:20 -0800142 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700143 const std::string& test_label,
144 double avg_psnr_threshold,
145 double avg_ssim_threshold,
146 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800147 FILE* graph_data_output_file,
148 const std::string& graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -0800149 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 09:16:05 -0800150 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 09:23:30 -0800151 size_t selected_stream,
ilnik1e7732c2017-02-23 05:07:56 -0800152 int selected_sl,
153 int selected_tl,
ilnik6b826ef2017-06-16 06:53:48 -0700154 bool is_quick_test_enabled,
155 Clock* clock)
perkja49cbd32016-09-16 07:53:41 -0700156 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700157 receiver_(nullptr),
stefan889d9652017-07-05 03:03:02 -0700158 call_(nullptr),
ivica5d6a06c2015-09-17 05:30:24 -0700159 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800160 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700161 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700162 test_label_(test_label),
163 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800164 graph_title_(graph_title),
165 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800166 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800167 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800168 selected_sl_(selected_sl),
169 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700170 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100171 encode_timing_proxy_(this),
stefan889d9652017-07-05 03:03:02 -0700172 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700173 frames_to_process_(duration_frames),
174 frames_recorded_(0),
175 frames_processed_(0),
176 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700177 dropped_frames_before_first_encode_(0),
178 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700179 last_render_time_(0),
180 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800181 total_media_bytes_(0),
182 first_sending_time_(0),
183 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800184 cpu_time_(0),
185 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700186 avg_psnr_threshold_(avg_psnr_threshold),
187 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800188 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100189 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100190 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100191 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700192 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
193
194 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
195 // so that we don't accidentally starve "real" worker threads (codec etc).
196 // Also, don't allocate more than kMaxComparisonThreads, even if there are
197 // spare cores.
198
199 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800200 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700201 static const uint32_t kMinCoresLeft = 4;
202 static const uint32_t kMaxComparisonThreads = 8;
203
204 if (num_cores <= kMinCoresLeft) {
205 num_cores = 1;
206 } else {
207 num_cores -= kMinCoresLeft;
208 num_cores = std::min(num_cores, kMaxComparisonThreads);
209 }
210
211 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100212 rtc::PlatformThread* thread =
213 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
214 thread->Start();
215 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700216 }
ivica5d6a06c2015-09-17 05:30:24 -0700217 }
218
219 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100220 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
221 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700222 delete thread;
223 }
224 }
225
226 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
227
ilnik6b826ef2017-06-16 06:53:48 -0700228 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
229 if (respect_sink_wants)
230 captured_frame_forwarder_.SetSource(video_capturer);
231 rtc::VideoSinkWants wants;
232 video_capturer->AddOrUpdateSink(InputInterface(), wants);
233 }
234
stefan889d9652017-07-05 03:03:02 -0700235 void SetCall(Call* call) {
236 rtc::CritScope lock(&crit_);
237 RTC_DCHECK(!call_);
238 call_ = call;
239 }
240
perkja49cbd32016-09-16 07:53:41 -0700241 void SetSendStream(VideoSendStream* stream) {
242 rtc::CritScope lock(&crit_);
243 RTC_DCHECK(!send_stream_);
244 send_stream_ = stream;
245 }
246
philipelfd870db2017-01-23 03:22:15 -0800247 void SetReceiveStream(VideoReceiveStream* stream) {
248 rtc::CritScope lock(&crit_);
249 RTC_DCHECK(!receive_stream_);
250 receive_stream_ = stream;
251 }
252
perkja49cbd32016-09-16 07:53:41 -0700253 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
254 return &captured_frame_forwarder_;
255 }
256 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
257 return &captured_frame_forwarder_;
258 }
259
ivica5d6a06c2015-09-17 05:30:24 -0700260 DeliveryStatus DeliverPacket(MediaType media_type,
261 const uint8_t* packet,
262 size_t length,
263 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700264 // Ignore timestamps of RTCP packets. They're not synchronized with
265 // RTP packet timestamps and so they would confuse wrap_handler_.
266 if (RtpHeaderParser::IsRtcp(packet, length)) {
267 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
268 }
sprangce4aef12015-11-02 07:23:20 -0800269 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700270 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800271 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800272 if (!IsFlexfec(header.payloadType) &&
273 (header.ssrc == ssrc_to_analyze_ ||
274 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800275 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
276 // (FlexFEC and media are sent on different SSRCs, which have different
277 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800278 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700279 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800280 int64_t timestamp =
281 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
282 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700283 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
284 }
285
286 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
287 }
288
Peter Boströme4499152016-02-05 11:13:28 +0100289 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700290 rtc::CritScope crit(&comparison_lock_);
291 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
292 }
293
pbos14fe7082016-04-20 06:35:56 -0700294 void PreEncodeOnFrame(const VideoFrame& video_frame) {
295 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800296 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700297 while (frames_.front().timestamp() != video_frame.timestamp()) {
298 ++dropped_frames_before_first_encode_;
299 frames_.pop_front();
300 RTC_CHECK(!frames_.empty());
301 }
ilnik3dd5ad92017-02-09 04:58:53 -0800302 first_encoded_timestamp_ =
303 rtc::Optional<uint32_t>(video_frame.timestamp());
304 }
305 }
306
307 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
308 rtc::CritScope lock(&crit_);
309 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800310 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800311 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700312 }
313 }
314
stefan1d8a5062015-10-02 03:39:33 -0700315 bool SendRtp(const uint8_t* packet,
316 size_t length,
317 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800318 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700319 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800320 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700321
sprangce4aef12015-11-02 07:23:20 -0800322 int64_t current_time =
323 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800324
sprangce4aef12015-11-02 07:23:20 -0800325 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700326 {
327 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800328 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800329 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800330 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800331 }
ilnik3dd5ad92017-02-09 04:58:53 -0800332
333 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800334 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
335 // (FlexFEC and media are sent on different SSRCs, which have different
336 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800337 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800338 int64_t timestamp =
339 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
340 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800341
342 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800343 encoded_frame_sizes_[timestamp] +=
344 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800345 total_media_bytes_ +=
346 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800347 }
ilnik1e7732c2017-02-23 05:07:56 -0800348 if (first_sending_time_ == 0)
349 first_sending_time_ = current_time;
350 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800351 }
ivica5d6a06c2015-09-17 05:30:24 -0700352 }
sprangce4aef12015-11-02 07:23:20 -0800353 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700354 }
355
356 bool SendRtcp(const uint8_t* packet, size_t length) override {
357 return transport_->SendRtcp(packet, length);
358 }
359
nisseeb83a1a2016-03-21 01:27:56 -0700360 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700361 int64_t render_time_ms =
362 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700363
364 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800365
366 StartExcludingCpuThreadTime();
367
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700368 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800369 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700370
sprang16daaa52016-03-09 01:30:24 -0800371 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700372 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700373 // No previous frame rendered, this one was dropped after sending but
374 // before rendering.
375 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800376 } else {
377 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
378 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700379 }
ivica5d6a06c2015-09-17 05:30:24 -0700380 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700381 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700382 }
383
384 VideoFrame reference_frame = frames_.front();
385 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800386 int64_t reference_timestamp =
387 wrap_handler_.Unwrap(reference_frame.timestamp());
388 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800389 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100390 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800391 ++send_timestamp;
392 }
sprang16daaa52016-03-09 01:30:24 -0800393 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700394
395 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
396
nisse97f0b932016-05-26 09:44:40 -0700397 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800398
399 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700400 }
401
ivica5d6a06c2015-09-17 05:30:24 -0700402 void Wait() {
403 // Frame comparisons can be very expensive. Wait for test to be done, but
404 // at time-out check if frames_processed is going up. If so, give it more
405 // time, otherwise fail. Hopefully this will reduce test flakiness.
406
Peter Boström8c38e8b2015-11-26 17:45:47 +0100407 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800408
ivica5d6a06c2015-09-17 05:30:24 -0700409 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700410 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100411 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700412 int frames_processed;
413 {
414 rtc::CritScope crit(&comparison_lock_);
415 frames_processed = frames_processed_;
416 }
417
418 // Print some output so test infrastructure won't think we've crashed.
419 const char* kKeepAliveMessages[3] = {
420 "Uh, I'm-I'm not quite dead, sir.",
421 "Uh, I-I think uh, I could pull through, sir.",
422 "Actually, I think I'm all right to come with you--"};
423 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
424
425 if (last_frames_processed == -1) {
426 last_frames_processed = frames_processed;
427 continue;
428 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100429 if (frames_processed == last_frames_processed) {
430 EXPECT_GT(frames_processed, last_frames_processed)
431 << "Analyzer stalled while waiting for test to finish.";
432 done_.Set();
433 break;
434 }
ivica5d6a06c2015-09-17 05:30:24 -0700435 last_frames_processed = frames_processed;
436 }
437
438 if (iteration > 0)
439 printf("- Farewell, sweet Concorde!\n");
440
Peter Boström8c38e8b2015-11-26 17:45:47 +0100441 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700442 }
443
pbos14fe7082016-04-20 06:35:56 -0700444 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
445 return &pre_encode_proxy_;
446 }
Peter Boströme4499152016-02-05 11:13:28 +0100447 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
448
ilnikdf92c5c2017-02-23 02:08:44 -0800449 void StartMeasuringCpuProcessTime() {
450 rtc::CritScope lock(&cpu_measurement_lock_);
451 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
452 wallclock_time_ -= rtc::SystemTimeNanos();
453 }
454
455 void StopMeasuringCpuProcessTime() {
456 rtc::CritScope lock(&cpu_measurement_lock_);
457 cpu_time_ += rtc::GetProcessCpuTimeNanos();
458 wallclock_time_ += rtc::SystemTimeNanos();
459 }
460
461 void StartExcludingCpuThreadTime() {
462 rtc::CritScope lock(&cpu_measurement_lock_);
463 cpu_time_ += rtc::GetThreadCpuTimeNanos();
464 }
465
466 void StopExcludingCpuThreadTime() {
467 rtc::CritScope lock(&cpu_measurement_lock_);
468 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
469 }
470
471 double GetCpuUsagePercent() {
472 rtc::CritScope lock(&cpu_measurement_lock_);
473 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
474 }
475
sprangce4aef12015-11-02 07:23:20 -0800476 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700477 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700478
479 private:
480 struct FrameComparison {
481 FrameComparison()
482 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800483 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700484 send_time_ms(0),
485 recv_time_ms(0),
486 render_time_ms(0),
487 encoded_frame_size(0) {}
488
489 FrameComparison(const VideoFrame& reference,
490 const VideoFrame& render,
491 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800492 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700493 int64_t send_time_ms,
494 int64_t recv_time_ms,
495 int64_t render_time_ms,
496 size_t encoded_frame_size)
497 : reference(reference),
498 render(render),
499 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800500 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700501 send_time_ms(send_time_ms),
502 recv_time_ms(recv_time_ms),
503 render_time_ms(render_time_ms),
504 encoded_frame_size(encoded_frame_size) {}
505
nissedf2ceb82016-12-15 06:29:53 -0800506 FrameComparison(bool dropped,
507 int64_t input_time_ms,
508 int64_t send_time_ms,
509 int64_t recv_time_ms,
510 int64_t render_time_ms,
511 size_t encoded_frame_size)
512 : dropped(dropped),
513 input_time_ms(input_time_ms),
514 send_time_ms(send_time_ms),
515 recv_time_ms(recv_time_ms),
516 render_time_ms(render_time_ms),
517 encoded_frame_size(encoded_frame_size) {}
518
519 rtc::Optional<VideoFrame> reference;
520 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700521 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800522 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700523 int64_t send_time_ms;
524 int64_t recv_time_ms;
525 int64_t render_time_ms;
526 size_t encoded_frame_size;
527 };
528
529 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700530 Sample(int dropped,
531 int64_t input_time_ms,
532 int64_t send_time_ms,
533 int64_t recv_time_ms,
534 int64_t render_time_ms,
535 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700536 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700537 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700538 : dropped(dropped),
539 input_time_ms(input_time_ms),
540 send_time_ms(send_time_ms),
541 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700542 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700543 encoded_frame_size(encoded_frame_size),
544 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700545 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700546
ivica8d15bd62015-10-07 02:43:12 -0700547 int dropped;
548 int64_t input_time_ms;
549 int64_t send_time_ms;
550 int64_t recv_time_ms;
551 int64_t render_time_ms;
552 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700553 double psnr;
554 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700555 };
556
Peter Boströme4499152016-02-05 11:13:28 +0100557 // This class receives the send-side OnEncodeTiming and is provided to not
558 // conflict with the receiver-side pre_decode_callback.
559 class OnEncodeTimingProxy : public EncodedFrameObserver {
560 public:
561 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
562
563 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
564 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
565 }
ilnik3dd5ad92017-02-09 04:58:53 -0800566 void EncodedFrameCallback(const EncodedFrame& frame) override {
567 parent_->PostEncodeFrameCallback(frame);
568 }
Peter Boströme4499152016-02-05 11:13:28 +0100569
570 private:
571 VideoAnalyzer* const parent_;
572 };
573
pbos14fe7082016-04-20 06:35:56 -0700574 // This class receives the send-side OnFrame callback and is provided to not
575 // conflict with the receiver-side renderer callback.
576 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
577 public:
578 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
579
580 void OnFrame(const VideoFrame& video_frame) override {
581 parent_->PreEncodeOnFrame(video_frame);
582 }
583
584 private:
585 VideoAnalyzer* const parent_;
586 };
587
ilnik1e7732c2017-02-23 05:07:56 -0800588 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
589 size_t length,
590 const RTPHeader& header) {
591 if (header.payloadType != kPayloadTypeVP9 &&
592 header.payloadType != kPayloadTypeVP8) {
593 return true;
594 } else {
595 // Get VP8 and VP9 specific header to check layers indexes.
596 const uint8_t* payload = packet + header.headerLength;
597 const size_t payload_length = length - header.headerLength;
598 const size_t payload_data_length = payload_length - header.paddingLength;
599 const bool is_vp8 = header.payloadType == kPayloadTypeVP8;
600 std::unique_ptr<RtpDepacketizer> depacketizer(
601 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
602 RtpDepacketizer::ParsedPayload parsed_payload;
603 bool result =
604 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
605 RTC_DCHECK(result);
606 const int temporal_idx = static_cast<int>(
607 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
608 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
609 const int spatial_idx = static_cast<int>(
610 is_vp8 ? kNoSpatialIdx
611 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
612 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
613 temporal_idx <= selected_tl_) &&
614 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
615 spatial_idx <= selected_sl_);
616 }
617 }
618
ivica5d6a06c2015-09-17 05:30:24 -0700619 void AddFrameComparison(const VideoFrame& reference,
620 const VideoFrame& render,
621 bool dropped,
622 int64_t render_time_ms)
623 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800624 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
625 int64_t send_time_ms = send_times_[reference_timestamp];
626 send_times_.erase(reference_timestamp);
627 int64_t recv_time_ms = recv_times_[reference_timestamp];
628 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700629
sprangce4aef12015-11-02 07:23:20 -0800630 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800631 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800632 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800633 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800634 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
635 if (it != encoded_frame_sizes_.end())
636 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700637
ivica5d6a06c2015-09-17 05:30:24 -0700638 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700639 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800640 comparisons_.push_back(FrameComparison(reference, render, dropped,
641 reference.ntp_time_ms(),
642 send_time_ms, recv_time_ms,
643 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700644 } else {
nissedf2ceb82016-12-15 06:29:53 -0800645 comparisons_.push_back(FrameComparison(dropped,
646 reference.ntp_time_ms(),
647 send_time_ms, recv_time_ms,
648 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700649 }
Peter Boström5811a392015-12-10 13:02:50 +0100650 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700651 }
652
tommi0f8b4032017-02-22 11:22:05 -0800653 static void PollStatsThread(void* obj) {
654 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700655 }
656
tommi0f8b4032017-02-22 11:22:05 -0800657 void PollStats() {
658 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
659 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700660
stefan889d9652017-07-05 03:03:02 -0700661 Call::Stats call_stats = call_->GetStats();
662 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
663
tommi0f8b4032017-02-22 11:22:05 -0800664 VideoSendStream::Stats send_stats = send_stream_->GetStats();
665 // It's not certain that we yet have estimates for any of these stats.
666 // Check that they are positive before mixing them in.
667 if (send_stats.encode_frame_rate > 0)
668 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
669 if (send_stats.avg_encode_time_ms > 0)
670 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
671 if (send_stats.encode_usage_percent > 0)
672 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
673 if (send_stats.media_bitrate_bps > 0)
674 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 03:03:02 -0700675 size_t fec_bytes = 0;
676 for (auto kv : send_stats.substreams) {
677 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
678 kv.second.rtp_stats.fec.padding_bytes;
679 }
680 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
681 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 03:22:15 -0800682
tommi0f8b4032017-02-22 11:22:05 -0800683 if (receive_stream_ != nullptr) {
684 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
685 if (receive_stats.decode_ms > 0)
686 decode_time_ms_.AddSample(receive_stats.decode_ms);
687 if (receive_stats.max_decode_ms > 0)
688 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
689 }
ilnikdaa574d2017-03-01 06:46:05 -0800690
691 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800692 }
ivica5d6a06c2015-09-17 05:30:24 -0700693 }
694
695 static bool FrameComparisonThread(void* obj) {
696 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
697 }
698
699 bool CompareFrames() {
700 if (AllFramesRecorded())
701 return false;
702
ivica5d6a06c2015-09-17 05:30:24 -0700703 FrameComparison comparison;
704
705 if (!PopComparison(&comparison)) {
706 // Wait until new comparison task is available, or test is done.
707 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100708 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700709 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100710 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700711 return false;
712 }
713 return true; // Try again.
714 }
715
ilnikdf92c5c2017-02-23 02:08:44 -0800716 StartExcludingCpuThreadTime();
717
ivica5d6a06c2015-09-17 05:30:24 -0700718 PerformFrameComparison(comparison);
719
ilnikdf92c5c2017-02-23 02:08:44 -0800720 StopExcludingCpuThreadTime();
721
ivica5d6a06c2015-09-17 05:30:24 -0700722 if (FrameProcessed()) {
723 PrintResults();
724 if (graph_data_output_file_)
725 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100726 done_.Set();
727 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700728 return false;
729 }
730
731 return true;
732 }
733
734 bool PopComparison(FrameComparison* comparison) {
735 rtc::CritScope crit(&comparison_lock_);
736 // If AllFramesRecorded() is true, it means we have already popped
737 // frames_to_process_ frames from comparisons_, so there is no more work
738 // for this thread to be done. frames_processed_ might still be lower if
739 // all comparisons are not done, but those frames are currently being
740 // worked on by other threads.
741 if (comparisons_.empty() || AllFramesRecorded())
742 return false;
743
744 *comparison = comparisons_.front();
745 comparisons_.pop_front();
746
747 FrameRecorded();
748 return true;
749 }
750
751 // Increment counter for number of frames received for comparison.
752 void FrameRecorded() {
753 rtc::CritScope crit(&comparison_lock_);
754 ++frames_recorded_;
755 }
756
757 // Returns true if all frames to be compared have been taken from the queue.
758 bool AllFramesRecorded() {
759 rtc::CritScope crit(&comparison_lock_);
760 assert(frames_recorded_ <= frames_to_process_);
761 return frames_recorded_ == frames_to_process_;
762 }
763
764 // Increase count of number of frames processed. Returns true if this was the
765 // last frame to be processed.
766 bool FrameProcessed() {
767 rtc::CritScope crit(&comparison_lock_);
768 ++frames_processed_;
769 assert(frames_processed_ <= frames_to_process_);
770 return frames_processed_ == frames_to_process_;
771 }
772
773 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800774 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700775 rtc::CritScope crit(&comparison_lock_);
776 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800777 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700778 PrintResult("sender_time", sender_time_, " ms");
779 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700780 PrintResult("total_delay_incl_network", end_to_end_, " ms");
781 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700782 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800783 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800784 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 03:03:02 -0700785 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
786 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 03:22:15 -0800787
788 if (receive_stream_ != nullptr) {
789 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800790 }
ivica5d6a06c2015-09-17 05:30:24 -0700791
pbos14fe7082016-04-20 06:35:56 -0700792 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
793 dropped_frames_);
ilnikdf92c5c2017-02-23 02:08:44 -0800794 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
795 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 06:46:05 -0800796
797#if defined(WEBRTC_WIN)
798 // On Linux and Mac in Resident Set some unused pages may be counted.
799 // Therefore this metric will depend on order in which tests are run and
800 // will be flaky.
801 PrintResult("memory_usage", memory_usage_, " bytes");
802#endif
803
ilnik9ae0d762017-02-15 00:53:12 -0800804 // Disable quality check for quick test, as quality checks may fail
805 // because too few samples were collected.
806 if (!is_quick_test_enabled_) {
807 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
808 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
809 }
ivica5d6a06c2015-09-17 05:30:24 -0700810 }
811
812 void PerformFrameComparison(const FrameComparison& comparison) {
813 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700814 double psnr = -1.0;
815 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700816 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800817 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
818 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700819 }
ivica5d6a06c2015-09-17 05:30:24 -0700820
ivica5d6a06c2015-09-17 05:30:24 -0700821 rtc::CritScope crit(&comparison_lock_);
822 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800823 samples_.push_back(Sample(
824 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
825 comparison.recv_time_ms, comparison.render_time_ms,
826 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700827 }
stefanb1797672016-08-11 07:00:57 -0700828 if (psnr >= 0.0)
829 psnr_.AddSample(psnr);
830 if (ssim >= 0.0)
831 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700832
833 if (comparison.dropped) {
834 ++dropped_frames_;
835 return;
836 }
837 if (last_render_time_ != 0)
838 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
839 last_render_time_ = comparison.render_time_ms;
840
nissedf2ceb82016-12-15 06:29:53 -0800841 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800842 if (comparison.recv_time_ms > 0) {
843 // If recv_time_ms == 0, this frame consisted of a packets which were all
844 // lost in the transport. Since we were able to render the frame, however,
845 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
846 // happens internally in Call, and we can therefore here not know which
847 // FEC packets that protected the lost media packets. Consequently, we
848 // were not able to record a meaningful recv_time_ms. We therefore skip
849 // this sample.
850 //
851 // The reasoning above does not hold for ULPFEC and RTX, as for those
852 // strategies the timestamp of the received packets is set to the
853 // timestamp of the protected/retransmitted media packet. I.e., then
854 // recv_time_ms != 0, even though the media packets were lost.
855 receiver_time_.AddSample(comparison.render_time_ms -
856 comparison.recv_time_ms);
857 }
nissedf2ceb82016-12-15 06:29:53 -0800858 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700859 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
860 }
861
862 void PrintResult(const char* result_type,
863 test::Statistics stats,
864 const char* unit) {
865 printf("RESULT %s: %s = {%f, %f}%s\n",
866 result_type,
867 test_label_.c_str(),
868 stats.Mean(),
869 stats.StandardDeviation(),
870 unit);
871 }
872
873 void PrintSamplesToFile(void) {
874 FILE* out = graph_data_output_file_;
875 rtc::CritScope crit(&comparison_lock_);
876 std::sort(samples_.begin(), samples_.end(),
877 [](const Sample& A, const Sample& B) -> bool {
878 return A.input_time_ms < B.input_time_ms;
879 });
880
sprangce4aef12015-11-02 07:23:20 -0800881 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700882 fprintf(out, "%" PRIuS "\n", samples_.size());
883 fprintf(out,
884 "dropped "
885 "input_time_ms "
886 "send_time_ms "
887 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700888 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700889 "encoded_frame_size "
890 "psnr "
891 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700892 "encode_time_ms\n");
893 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700894 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700895 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
896 int encode_time_ms;
897 if (it != samples_encode_time_ms_.end()) {
898 encode_time_ms = it->second;
899 } else {
900 ++missing_encode_time_samples;
901 encode_time_ms = -1;
902 }
903 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
904 " %lf %lf %d\n",
905 sample.dropped, sample.input_time_ms, sample.send_time_ms,
906 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700907 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700908 encode_time_ms);
909 }
910 if (missing_encode_time_samples) {
911 fprintf(stderr,
912 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
913 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700914 }
915 }
916
ilnik1e7732c2017-02-23 05:07:56 -0800917 double GetAverageMediaBitrateBps() {
918 if (last_sending_time_ == first_sending_time_) {
919 return 0;
920 } else {
921 return static_cast<double>(total_media_bytes_) * 8 /
922 (last_sending_time_ - first_sending_time_) *
923 rtc::kNumMillisecsPerSec;
924 }
925 }
926
perkja49cbd32016-09-16 07:53:41 -0700927 // Implements VideoSinkInterface to receive captured frames from a
928 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
929 // as a source to VideoSendStream.
930 // It forwards all input frames to the VideoAnalyzer for later comparison and
931 // forwards the captured frames to the VideoSendStream.
932 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
933 public rtc::VideoSourceInterface<VideoFrame> {
934 public:
ilnik6b826ef2017-06-16 06:53:48 -0700935 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
936 : analyzer_(analyzer),
937 send_stream_input_(nullptr),
938 video_capturer_(nullptr),
939 clock_(clock) {}
940
941 void SetSource(test::VideoCapturer* video_capturer) {
942 video_capturer_ = video_capturer;
943 }
perkja49cbd32016-09-16 07:53:41 -0700944
945 private:
946 void OnFrame(const VideoFrame& video_frame) override {
947 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800948 // Frames from the capturer does not have a rtp timestamp.
949 // Create one so it can be used for comparison.
950 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -0700951 if (video_frame.ntp_time_ms() == 0)
952 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -0700953 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800954 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700955 rtc::CritScope lock(&crit_);
956 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -0700957 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -0700958 }
959
960 // Called when |send_stream_.SetSource()| is called.
961 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
962 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -0700963 {
964 rtc::CritScope lock(&crit_);
965 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
966 send_stream_input_ = sink;
967 }
ilnik6b826ef2017-06-16 06:53:48 -0700968 if (video_capturer_) {
969 video_capturer_->AddOrUpdateSink(this, wants);
970 }
perkja49cbd32016-09-16 07:53:41 -0700971 }
972
973 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
974 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
975 rtc::CritScope lock(&crit_);
976 RTC_DCHECK(sink == send_stream_input_);
977 send_stream_input_ = nullptr;
978 }
979
980 VideoAnalyzer* const analyzer_;
981 rtc::CriticalSection crit_;
982 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -0700983 test::VideoCapturer* video_capturer_;
984 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -0700985 };
986
987 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
988 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800989 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700990 }
991
stefan889d9652017-07-05 03:03:02 -0700992 Call* call_;
perkja49cbd32016-09-16 07:53:41 -0700993 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800994 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700995 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700996 const std::string test_label_;
997 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800998 const std::string graph_title_;
999 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -08001000 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -08001001 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -08001002 const int selected_sl_;
1003 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -07001004 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +01001005 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -07001006 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -07001007 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -07001008 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
1009 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
1010 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
1011 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
1012 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
1013 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
1014 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
1015 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
philipelfd870db2017-01-23 03:22:15 -08001016 test::Statistics encode_time_ms_ GUARDED_BY(comparison_lock_);
1017 test::Statistics encode_usage_percent_ GUARDED_BY(comparison_lock_);
1018 test::Statistics decode_time_ms_ GUARDED_BY(comparison_lock_);
1019 test::Statistics decode_time_max_ms_ GUARDED_BY(comparison_lock_);
1020 test::Statistics media_bitrate_bps_ GUARDED_BY(comparison_lock_);
stefan889d9652017-07-05 03:03:02 -07001021 test::Statistics fec_bitrate_bps_ GUARDED_BY(comparison_lock_);
1022 test::Statistics send_bandwidth_bps_ GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -08001023 test::Statistics memory_usage_ GUARDED_BY(comparison_lock_);
1024
stefan889d9652017-07-05 03:03:02 -07001025 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 05:30:24 -07001026
1027 const int frames_to_process_;
1028 int frames_recorded_;
1029 int frames_processed_;
1030 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001031 int dropped_frames_before_first_encode_;
1032 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001033 int64_t last_render_time_;
1034 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001035 int64_t total_media_bytes_;
1036 int64_t first_sending_time_;
1037 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001038
ilnikdf92c5c2017-02-23 02:08:44 -08001039 int64_t cpu_time_ GUARDED_BY(cpu_measurement_lock_);
1040 int64_t wallclock_time_ GUARDED_BY(cpu_measurement_lock_);
1041 rtc::CriticalSection cpu_measurement_lock_;
1042
ivica5d6a06c2015-09-17 05:30:24 -07001043 rtc::CriticalSection crit_;
1044 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -07001045 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -08001046 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
1047 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
1048 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
1049 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ilnik3dd5ad92017-02-09 04:58:53 -08001050 rtc::Optional<uint32_t> first_encoded_timestamp_ GUARDED_BY(crit_);
1051 rtc::Optional<uint32_t> first_sent_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001052 const double avg_psnr_threshold_;
1053 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001054 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001055
1056 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001057 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1058 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001059 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -07001060 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001061 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -07001062};
1063
ilnikcb8c1462017-03-09 09:23:30 -08001064class Vp8EncoderFactory : public VideoEncoderFactory {
1065 public:
1066 Vp8EncoderFactory() = default;
1067 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1068
1069 VideoEncoder* Create() override {
1070 VideoEncoder* encoder = VP8Encoder::Create();
1071 live_encoders_.insert(encoder);
1072 return encoder;
1073 }
1074
1075 void Destroy(VideoEncoder* encoder) override {
1076 auto it = live_encoders_.find(encoder);
1077 RTC_CHECK(it != live_encoders_.end());
1078 live_encoders_.erase(it);
1079 delete encoder;
1080 }
1081
1082 std::set<VideoEncoder*> live_encoders_;
1083};
1084
palmkviste75f2042016-09-28 06:19:48 -07001085VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001086 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1087 payload_type_map_ = test::CallTest::payload_type_map_;
1088 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1089 payload_type_map_.end());
1090 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1091 payload_type_map_.end());
1092 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1093 payload_type_map_.end());
1094 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1095 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1096 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1097}
ivica5d6a06c2015-09-17 05:30:24 -07001098
minyue626bc952016-10-31 05:47:02 -07001099VideoQualityTest::Params::Params()
1100 : call({false, Call::Config::BitrateConfig()}),
1101 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -08001102 false, "", ""}),
minyue4c8b9422017-03-21 04:11:43 -07001103 audio({false, false, false}),
minyue626bc952016-10-31 05:47:02 -07001104 screenshare({false, 10, 0}),
1105 analyzer({"", 0.0, 0.0, 0, "", ""}),
1106 pipe(),
1107 logs(false),
ilnika014cc52017-03-07 04:21:04 -08001108 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
1109 num_thumbnails(0) {}
minyue626bc952016-10-31 05:47:02 -07001110
1111VideoQualityTest::Params::~Params() = default;
1112
ivica5d6a06c2015-09-17 05:30:24 -07001113void VideoQualityTest::TestBody() {}
1114
sprangce4aef12015-11-02 07:23:20 -08001115std::string VideoQualityTest::GenerateGraphTitle() const {
1116 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001117 ss << params_.video.codec;
1118 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1119 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001120 if (params_.screenshare.scroll_duration)
1121 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1122 if (params_.ss.streams.size() > 1)
1123 ss << ", Stream #" << params_.ss.selected_stream;
1124 if (params_.ss.num_spatial_layers > 1)
1125 ss << ", Layer #" << params_.ss.selected_sl;
1126 ss << ")";
1127 return ss.str();
1128}
1129
1130void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001131 if (!params_.video.enabled)
1132 return;
sprangce4aef12015-11-02 07:23:20 -08001133 // Add a default stream in none specified.
1134 if (params_.ss.streams.empty())
1135 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1136 if (params_.ss.num_spatial_layers == 0)
1137 params_.ss.num_spatial_layers = 1;
1138
1139 if (params_.pipe.loss_percent != 0 ||
1140 params_.pipe.queue_length_packets != 0) {
1141 // Since LayerFilteringTransport changes the sequence numbers, we can't
1142 // use that feature with pack loss, since the NACK request would end up
1143 // retransmitting the wrong packets.
1144 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001145 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001146 RTC_CHECK(params_.video.selected_tl == -1 ||
1147 params_.video.selected_tl ==
1148 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001149 }
1150
1151 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1152 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001153 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1154 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1155 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprang1168fd42017-06-21 09:00:17 -07001156 RTC_CHECK_LE(params_.ss.selected_stream, params_.ss.streams.size());
sprangce4aef12015-11-02 07:23:20 -08001157 for (const VideoStream& stream : params_.ss.streams) {
1158 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1159 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1160 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
sprangce4aef12015-11-02 07:23:20 -08001161 }
1162 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1163 // the total bitrate? We anyway have to update them in the case bitrate
1164 // estimator changes the total bitrates.
1165 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1166 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1167 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1168 params_.ss.spatial_layers.size() ==
1169 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001170 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001171 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001172 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001173 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001174 }
ilnika014cc52017-03-07 04:21:04 -08001175 RTC_CHECK_GE(params_.num_thumbnails, 0);
1176 if (params_.num_thumbnails > 0) {
1177 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1178 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1179 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1180 RTC_CHECK_EQ(params_.video.codec, "VP8");
1181 }
sprangce4aef12015-11-02 07:23:20 -08001182}
1183
1184// Static.
1185std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1186 // Parse comma separated nonnegative integers, where some elements may be
1187 // empty. The empty values are replaced with -1.
1188 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1189 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1190 std::vector<int> result;
1191 if (str.empty())
1192 return result;
1193
1194 const char* p = str.c_str();
1195 int value = -1;
1196 int pos;
1197 while (*p) {
1198 if (*p == ',') {
1199 result.push_back(value);
1200 value = -1;
1201 ++p;
1202 continue;
1203 }
1204 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1205 << "Unexpected non-number value.";
1206 p += pos;
1207 }
1208 result.push_back(value);
1209 return result;
1210}
1211
1212// Static.
1213VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1214 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001215 stream.width = params.video.width;
1216 stream.height = params.video.height;
1217 stream.max_framerate = params.video.fps;
1218 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1219 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1220 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001221 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 02:01:32 -07001222 // TODO(sprang): Can we make this less of a hack?
1223 if (params.video.num_temporal_layers == 2) {
1224 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1225 } else if (params.video.num_temporal_layers == 3) {
1226 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1227 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
ilnika014cc52017-03-07 04:21:04 -08001228 }
1229 return stream;
1230}
1231
1232// Static.
1233VideoStream VideoQualityTest::DefaultThumbnailStream() {
1234 VideoStream stream;
1235 stream.width = 320;
1236 stream.height = 180;
1237 stream.max_framerate = 7;
1238 stream.min_bitrate_bps = 7500;
1239 stream.target_bitrate_bps = 37500;
1240 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001241 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001242 return stream;
1243}
1244
1245// Static.
1246void VideoQualityTest::FillScalabilitySettings(
1247 Params* params,
1248 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001249 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001250 size_t selected_stream,
1251 int num_spatial_layers,
1252 int selected_sl,
1253 const std::vector<std::string>& sl_descriptors) {
sprang1168fd42017-06-21 09:00:17 -07001254 if (params->ss.streams.empty() && params->ss.infer_streams) {
1255 webrtc::VideoEncoderConfig encoder_config;
1256 encoder_config.content_type =
1257 params->screenshare.enabled
1258 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1259 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
1260 encoder_config.max_bitrate_bps = params->video.max_bitrate_bps;
1261 encoder_config.min_transmit_bitrate_bps = params->video.min_transmit_bps;
1262 encoder_config.number_of_streams = num_streams;
1263 encoder_config.spatial_layers = params->ss.spatial_layers;
1264 encoder_config.video_stream_factory =
1265 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1266 params->video.codec, kDefaultMaxQp, params->video.fps,
1267 params->screenshare.enabled, true);
1268 params->ss.streams =
1269 encoder_config.video_stream_factory->CreateEncoderStreams(
1270 static_cast<int>(params->video.width),
1271 static_cast<int>(params->video.height), encoder_config);
1272 } else {
1273 // Read VideoStream and SpatialLayer elements from a list of comma separated
1274 // lists. To use a default value for an element, use -1 or leave empty.
1275 // Validity checks performed in CheckParams.
1276 RTC_CHECK(params->ss.streams.empty());
1277 for (auto descriptor : stream_descriptors) {
1278 if (descriptor.empty())
1279 continue;
1280 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1281 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1282 if (v[0] != -1)
1283 stream.width = static_cast<size_t>(v[0]);
1284 if (v[1] != -1)
1285 stream.height = static_cast<size_t>(v[1]);
1286 if (v[2] != -1)
1287 stream.max_framerate = v[2];
1288 if (v[3] != -1)
1289 stream.min_bitrate_bps = v[3];
1290 if (v[4] != -1)
1291 stream.target_bitrate_bps = v[4];
1292 if (v[5] != -1)
1293 stream.max_bitrate_bps = v[5];
1294 if (v.size() > 6 && v[6] != -1)
1295 stream.max_qp = v[6];
1296 if (v.size() > 7) {
1297 stream.temporal_layer_thresholds_bps.clear();
1298 stream.temporal_layer_thresholds_bps.insert(
1299 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1300 } else {
1301 // Automatic TL thresholds for more than two layers not supported.
1302 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
1303 }
1304 params->ss.streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001305 }
sprangce4aef12015-11-02 07:23:20 -08001306 }
sprangce4aef12015-11-02 07:23:20 -08001307
sprang1168fd42017-06-21 09:00:17 -07001308 params->ss.num_spatial_layers = std::max(1, num_spatial_layers);
1309 params->ss.selected_stream = selected_stream;
1310
sprangce4aef12015-11-02 07:23:20 -08001311 params->ss.selected_sl = selected_sl;
1312 RTC_CHECK(params->ss.spatial_layers.empty());
1313 for (auto descriptor : sl_descriptors) {
1314 if (descriptor.empty())
1315 continue;
1316 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1317 RTC_CHECK_GT(v[2], 0);
1318
1319 SpatialLayer layer;
1320 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1321 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1322 layer.target_bitrate_bps = v[2];
1323 params->ss.spatial_layers.push_back(layer);
1324 }
1325}
1326
minyuea27172d2016-11-01 05:59:29 -07001327void VideoQualityTest::SetupVideo(Transport* send_transport,
1328 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001329 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001330 trace_to_stderr_.reset(new test::TraceToStderr);
1331
brandtr8313a6f2017-01-13 07:41:19 -08001332 size_t num_video_streams = params_.ss.streams.size();
1333 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1334 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001335
1336 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001337 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001338 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001339 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001340 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 09:23:30 -08001341 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
1342 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1343 // encoders usually can't natively do simulcast with different frame rates
1344 // for the different layers.
1345 video_encoder_.reset(
1346 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
1347 } else {
1348 video_encoder_.reset(VP8Encoder::Create());
1349 }
ivica5d6a06c2015-09-17 05:30:24 -07001350 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001351 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001352 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001353 payload_type = kPayloadTypeVP9;
1354 } else {
1355 RTC_NOTREACHED() << "Codec not supported!";
1356 return;
1357 }
minyuea27172d2016-11-01 05:59:29 -07001358 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001359 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001360 video_send_config_.encoder_settings.payload_type = payload_type;
1361 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1362 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001363 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001364 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001365
stefanff483612015-12-21 03:14:00 -08001366 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001367 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001368 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001369 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001370 test::kTransportSequenceNumberExtensionId));
1371 } else {
stefanff483612015-12-21 03:14:00 -08001372 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001373 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001374 }
ilnik00d802b2017-04-11 10:34:31 -07001375 video_send_config_.rtp.extensions.push_back(RtpExtension(
1376 RtpExtension::kVideoContentTypeUri, test::kVideoContentTypeExtensionId));
ilnik04f4d122017-06-19 07:18:55 -07001377 video_send_config_.rtp.extensions.push_back(RtpExtension(
1378 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001379
stefanff483612015-12-21 03:14:00 -08001380 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001381 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001382
brandtr1293aca2016-11-16 22:47:29 -08001383 video_send_config_.suspend_below_min_bitrate =
1384 params_.video.suspend_below_min_bitrate;
1385
perkjfa10b552016-10-02 23:45:26 -07001386 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1387 video_encoder_config_.max_bitrate_bps = 0;
1388 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1389 video_encoder_config_.max_bitrate_bps +=
1390 params_.ss.streams[i].max_bitrate_bps;
1391 }
ilnik6b826ef2017-06-16 06:53:48 -07001392 if (params_.ss.infer_streams) {
1393 video_encoder_config_.video_stream_factory =
1394 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1395 params_.video.codec, params_.ss.streams[0].max_qp,
1396 params_.video.fps, params_.screenshare.enabled, true);
1397 } else {
1398 video_encoder_config_.video_stream_factory =
1399 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1400 }
perkjfa10b552016-10-02 23:45:26 -07001401
stefanff483612015-12-21 03:14:00 -08001402 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001403
1404 CreateMatchingReceiveConfigs(recv_transport);
1405
sprang1168fd42017-06-21 09:00:17 -07001406 const bool decode_all_receive_streams =
1407 params_.ss.selected_stream == params_.ss.streams.size();
1408
brandtr8313a6f2017-01-13 07:41:19 -08001409 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001410 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001411 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
1412 video_receive_configs_[i].rtp.rtx_payload_types[payload_type] =
sprangce4aef12015-11-02 07:23:20 -08001413 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001414 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001415 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik00d802b2017-04-11 10:34:31 -07001416 // Enable RTT calculation so NTP time estimator will work.
1417 video_receive_configs_[i].rtp.rtcp_xr.receiver_reference_time_report = true;
ilnik9fd9f6c2017-03-02 08:10:10 -08001418 // Force fake decoders on non-selected simulcast streams.
sprang1168fd42017-06-21 09:00:17 -07001419 if (!decode_all_receive_streams && i != params_.ss.selected_stream) {
ilnik9fd9f6c2017-03-02 08:10:10 -08001420 VideoReceiveStream::Decoder decoder;
1421 decoder.decoder = new test::FakeDecoder();
1422 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1423 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1424 video_receive_configs_[i].decoders.clear();
1425 allocated_decoders_.emplace_back(decoder.decoder);
1426 video_receive_configs_[i].decoders.push_back(decoder);
1427 }
sprangce4aef12015-11-02 07:23:20 -08001428 }
brandtr1293aca2016-11-16 22:47:29 -08001429
1430 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001431 // Override send config constructed by CreateSendConfig.
sprang1168fd42017-06-21 09:00:17 -07001432 if (decode_all_receive_streams) {
1433 for (uint32_t media_ssrc : video_send_config_.rtp.ssrcs) {
1434 video_send_config_.rtp.flexfec.protected_media_ssrcs.push_back(
1435 media_ssrc);
1436 }
1437 } else {
1438 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1439 kVideoSendSsrcs[params_.ss.selected_stream]};
1440 }
brandtr1293aca2016-11-16 22:47:29 -08001441
brandtr8313a6f2017-01-13 07:41:19 -08001442 // The matching receive config is _not_ created by
1443 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1444 // Set up the receive config manually instead.
1445 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001446 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001447 video_send_config_.rtp.flexfec.payload_type;
1448 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001449 flexfec_receive_config.protected_media_ssrcs =
1450 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001451 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001452 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1453 if (params_.call.send_side_bwe) {
1454 flexfec_receive_config.rtp_header_extensions.push_back(
1455 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1456 test::kTransportSequenceNumberExtensionId));
1457 } else {
1458 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1459 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1460 }
brandtr1293aca2016-11-16 22:47:29 -08001461 flexfec_receive_configs_.push_back(flexfec_receive_config);
1462 }
1463
1464 if (params_.video.ulpfec) {
1465 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1466 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1467 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1468
sprang1168fd42017-06-21 09:00:17 -07001469 if (decode_all_receive_streams) {
1470 for (auto it = video_receive_configs_.begin();
1471 it != video_receive_configs_.end(); ++it) {
1472 it->rtp.ulpfec.red_payload_type =
1473 video_send_config_.rtp.ulpfec.red_payload_type;
1474 it->rtp.ulpfec.ulpfec_payload_type =
1475 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1476 it->rtp.ulpfec.red_rtx_payload_type =
1477 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1478 }
1479 } else {
1480 video_receive_configs_[params_.ss.selected_stream]
1481 .rtp.ulpfec.red_payload_type =
1482 video_send_config_.rtp.ulpfec.red_payload_type;
1483 video_receive_configs_[params_.ss.selected_stream]
1484 .rtp.ulpfec.ulpfec_payload_type =
1485 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1486 video_receive_configs_[params_.ss.selected_stream]
1487 .rtp.ulpfec.red_rtx_payload_type =
1488 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1489 }
brandtr1293aca2016-11-16 22:47:29 -08001490 }
ivica5d6a06c2015-09-17 05:30:24 -07001491}
1492
ilnika014cc52017-03-07 04:21:04 -08001493void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1494 Transport* recv_transport) {
1495 for (int i = 0; i < params_.num_thumbnails; ++i) {
1496 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1497
1498 // Thumbnails will be send in the other way: from receiver_call to
1499 // sender_call.
1500 VideoSendStream::Config thumbnail_send_config(recv_transport);
1501 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1502 thumbnail_send_config.encoder_settings.encoder =
1503 thumbnail_encoders_.back().get();
1504 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1505 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1506 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1507 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1508 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1509 thumbnail_send_config.rtp.extensions.clear();
1510 if (params_.call.send_side_bwe) {
1511 thumbnail_send_config.rtp.extensions.push_back(
1512 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1513 test::kTransportSequenceNumberExtensionId));
1514 } else {
1515 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1516 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1517 }
1518
1519 VideoEncoderConfig thumbnail_encoder_config;
1520 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1521 thumbnail_send_config.suspend_below_min_bitrate =
1522 params_.video.suspend_below_min_bitrate;
1523 thumbnail_encoder_config.number_of_streams = 1;
1524 thumbnail_encoder_config.max_bitrate_bps = 50000;
ilnik6b826ef2017-06-16 06:53:48 -07001525 if (params_.ss.infer_streams) {
1526 thumbnail_encoder_config.video_stream_factory =
1527 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1528 } else {
1529 thumbnail_encoder_config.video_stream_factory =
1530 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1531 params_.video.codec, params_.ss.streams[0].max_qp,
1532 params_.video.fps, params_.screenshare.enabled, true);
1533 }
ilnika014cc52017-03-07 04:21:04 -08001534 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1535
1536 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1537 thumbnail_receive_config.rtp.remb = false;
1538 thumbnail_receive_config.rtp.transport_cc = true;
1539 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1540 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1541 thumbnail_receive_config.rtp.extensions.push_back(extension);
1542 thumbnail_receive_config.renderer = &fake_renderer_;
1543
1544 VideoReceiveStream::Decoder decoder =
1545 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1546 allocated_decoders_.push_back(
1547 std::unique_ptr<VideoDecoder>(decoder.decoder));
1548 thumbnail_receive_config.decoders.clear();
1549 thumbnail_receive_config.decoders.push_back(decoder);
1550 thumbnail_receive_config.rtp.remote_ssrc =
1551 thumbnail_send_config.rtp.ssrcs[0];
1552
1553 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1554 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
1555 thumbnail_receive_config.rtp.rtx_payload_types[kPayloadTypeVP8] =
1556 kSendRtxPayloadType;
1557 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1558 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1559
1560 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1561 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1562 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1563 }
1564
1565 for (int i = 0; i < params_.num_thumbnails; ++i) {
1566 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1567 thumbnail_send_configs_[i].Copy(),
1568 thumbnail_encoder_configs_[i].Copy()));
1569 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1570 thumbnail_receive_configs_[i].Copy()));
1571 }
1572}
1573
1574void VideoQualityTest::DestroyThumbnailStreams() {
1575 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1576 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1577 thumbnail_send_streams_.clear();
1578 for (VideoReceiveStream* thumbnail_receive_stream :
1579 thumbnail_receive_streams_)
1580 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1581 thumbnail_send_streams_.clear();
1582 thumbnail_receive_streams_.clear();
1583}
1584
ilnik2a8c2f52017-02-15 02:23:28 -08001585void VideoQualityTest::SetupScreenshareOrSVC() {
1586 if (params_.screenshare.enabled) {
1587 // Fill out codec settings.
1588 video_encoder_config_.content_type =
1589 VideoEncoderConfig::ContentType::kScreen;
1590 degradation_preference_ =
1591 VideoSendStream::DegradationPreference::kMaintainResolution;
1592 if (params_.video.codec == "VP8") {
1593 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1594 vp8_settings.denoisingOn = false;
1595 vp8_settings.frameDroppingOn = false;
1596 vp8_settings.numberOfTemporalLayers =
1597 static_cast<unsigned char>(params_.video.num_temporal_layers);
1598 video_encoder_config_.encoder_specific_settings =
1599 new rtc::RefCountedObject<
1600 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1601 } else if (params_.video.codec == "VP9") {
1602 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1603 vp9_settings.denoisingOn = false;
1604 vp9_settings.frameDroppingOn = false;
1605 vp9_settings.numberOfTemporalLayers =
1606 static_cast<unsigned char>(params_.video.num_temporal_layers);
1607 vp9_settings.numberOfSpatialLayers =
1608 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1609 video_encoder_config_.encoder_specific_settings =
1610 new rtc::RefCountedObject<
1611 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1612 }
1613 // Setup frame generator.
1614 const size_t kWidth = 1850;
1615 const size_t kHeight = 1110;
ilnik8d8185c2017-04-12 04:52:55 -07001616 std::vector<std::string> slides = params_.screenshare.slides;
1617 if (slides.size() == 0) {
1618 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1619 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1620 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1621 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1622 }
ilnik2a8c2f52017-02-15 02:23:28 -08001623 if (params_.screenshare.scroll_duration == 0) {
1624 // Cycle image every slide_change_interval seconds.
perkja8ba1952017-02-27 06:52:10 -08001625 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
ilnik2a8c2f52017-02-15 02:23:28 -08001626 slides, kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001627 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001628 } else {
1629 RTC_CHECK_LE(params_.video.width, kWidth);
1630 RTC_CHECK_LE(params_.video.height, kHeight);
1631 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1632 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1633 params_.screenshare.scroll_duration) *
1634 1000;
1635 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1636 params_.screenshare.slide_change_interval);
1637
perkja8ba1952017-02-27 06:52:10 -08001638 frame_generator_ = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1639 clock_, slides, kWidth, kHeight, params_.video.width,
1640 params_.video.height, params_.screenshare.scroll_duration * 1000,
1641 kPauseDurationMs);
ilnik2a8c2f52017-02-15 02:23:28 -08001642 }
1643 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1644 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001645 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001646 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001647 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001648 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001649 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001650 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1651 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001652 }
ivica5d6a06c2015-09-17 05:30:24 -07001653}
1654
ilnika014cc52017-03-07 04:21:04 -08001655void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1656 VideoStream thumbnail = DefaultThumbnailStream();
1657 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1658 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1659 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1660 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001661 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001662 }
1663}
1664
perkja49cbd32016-09-16 07:53:41 -07001665void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001666 if (params_.screenshare.enabled) {
1667 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001668 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001669 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001670 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001671 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001672 } else {
ilnik6b826ef2017-06-16 06:53:48 -07001673 if (params_.video.clip_name == "Generator") {
1674 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
1675 static_cast<int>(params_.video.width),
1676 static_cast<int>(params_.video.height), params_.video.fps, clock_));
1677 } else if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001678 video_capturer_.reset(test::VcmCapturer::Create(
Tarun Chawla8e857d12017-05-31 19:20:57 +05301679 params_.video.width, params_.video.height, params_.video.fps,
1680 params_.video.capture_device_index));
sprang1bed2e42017-01-23 08:46:51 -08001681 if (!video_capturer_) {
1682 // Failed to get actual camera, use chroma generator as backup.
1683 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001684 static_cast<int>(params_.video.width),
1685 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001686 }
ivica5d6a06c2015-09-17 05:30:24 -07001687 } else {
minyuea27172d2016-11-01 05:59:29 -07001688 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001689 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001690 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001691 clock_));
minyuea27172d2016-11-01 05:59:29 -07001692 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1693 << params_.video.clip_name
1694 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001695 }
1696 }
sprang1bed2e42017-01-23 08:46:51 -08001697 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001698}
1699
sprang7a975f72015-10-12 06:33:21 -07001700void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001701 params_ = params;
1702
minyue626bc952016-10-31 05:47:02 -07001703 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001704 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1705 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001706 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001707
1708 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001709 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001710 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001711 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001712 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001713 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1714 << "!";
ivica87f83a92015-10-08 05:13:32 -07001715 }
sprang7a975f72015-10-12 06:33:21 -07001716
philipel4fb651d2017-04-10 03:54:05 -07001717 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001718 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001719 CreateCalls(call_config, call_config);
1720
ilnik68af10d2017-03-02 04:59:33 -08001721 test::LayerFilteringTransport send_transport(
1722 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue20c84cc2017-04-10 16:57:57 -07001723 params_.video.selected_tl, params_.ss.selected_sl, payload_type_map_);
1724
1725 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get(),
1726 payload_type_map_);
stefanf116bd02015-10-27 08:29:42 -07001727
sprangce4aef12015-11-02 07:23:20 -08001728 std::string graph_title = params_.analyzer.graph_title;
1729 if (graph_title.empty())
1730 graph_title = VideoQualityTest::GenerateGraphTitle();
1731
sprangc1b57a12017-02-28 08:50:47 -08001732 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
ivica5d6a06c2015-09-17 05:30:24 -07001733 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001734 &send_transport, params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001735 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001736 is_quick_test_enabled
1737 ? kFramesSentInQuickTest
1738 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001739 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001740 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001741 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 09:23:30 -08001742 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik6b826ef2017-06-16 06:53:48 -07001743 params_.video.selected_tl, is_quick_test_enabled, clock_);
stefan889d9652017-07-05 03:03:02 -07001744 analyzer.SetCall(sender_call_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001745 analyzer.SetReceiver(receiver_call_->Receiver());
1746 send_transport.SetReceiver(&analyzer);
1747 recv_transport.SetReceiver(sender_call_->Receiver());
1748
minyuea27172d2016-11-01 05:59:29 -07001749 SetupVideo(&analyzer, &recv_transport);
ilnika014cc52017-03-07 04:21:04 -08001750 SetupThumbnails(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001751 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001752 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
Peter Boströme4499152016-02-05 11:13:28 +01001753 RTC_DCHECK(!video_send_config_.post_encode_callback);
1754 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001755
ilnik2a8c2f52017-02-15 02:23:28 -08001756 SetupScreenshareOrSVC();
ivica5d6a06c2015-09-17 05:30:24 -07001757
brandtr1293aca2016-11-16 22:47:29 -08001758 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001759 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001760 analyzer.SetSendStream(video_send_stream_);
philipelfd870db2017-01-23 03:22:15 -08001761 if (video_receive_streams_.size() == 1)
1762 analyzer.SetReceiveStream(video_receive_streams_[0]);
kthelgason2bc68642017-02-07 07:02:22 -08001763
1764 video_send_stream_->SetSource(analyzer.OutputInterface(),
1765 degradation_preference_);
ivica5d6a06c2015-09-17 05:30:24 -07001766
ilnika014cc52017-03-07 04:21:04 -08001767 SetupThumbnailCapturers(params_.num_thumbnails);
1768 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1769 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1770 degradation_preference_);
1771 }
1772
perkja49cbd32016-09-16 07:53:41 -07001773 CreateCapturer();
ilnika014cc52017-03-07 04:21:04 -08001774
ilnik6b826ef2017-06-16 06:53:48 -07001775 analyzer.SetSource(video_capturer_.get(), params_.ss.infer_streams);
ivicac1cc8542015-10-08 03:44:06 -07001776
palmkviste75f2042016-09-28 06:19:48 -07001777 StartEncodedFrameLogs(video_send_stream_);
1778 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001779 video_send_stream_->Start();
ilnika014cc52017-03-07 04:21:04 -08001780 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1781 thumbnail_send_stream->Start();
stefanff483612015-12-21 03:14:00 -08001782 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1783 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001784 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1785 receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001786 for (VideoReceiveStream* thumbnail_receive_stream :
1787 thumbnail_receive_streams_)
1788 thumbnail_receive_stream->Start();
1789
ilnikdf92c5c2017-02-23 02:08:44 -08001790 analyzer.StartMeasuringCpuProcessTime();
ilnika014cc52017-03-07 04:21:04 -08001791
minyuea27172d2016-11-01 05:59:29 -07001792 video_capturer_->Start();
ilnika014cc52017-03-07 04:21:04 -08001793 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1794 thumbnail_capturers_) {
1795 video_caputurer->Start();
1796 }
ivica5d6a06c2015-09-17 05:30:24 -07001797
1798 analyzer.Wait();
1799
1800 send_transport.StopSending();
1801 recv_transport.StopSending();
1802
ilnika014cc52017-03-07 04:21:04 -08001803 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1804 thumbnail_capturers_)
1805 video_caputurer->Stop();
minyuea27172d2016-11-01 05:59:29 -07001806 video_capturer_->Stop();
ilnika014cc52017-03-07 04:21:04 -08001807 for (VideoReceiveStream* thumbnail_receive_stream :
1808 thumbnail_receive_streams_)
1809 thumbnail_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001810 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1811 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001812 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1813 receive_stream->Stop();
ilnika014cc52017-03-07 04:21:04 -08001814 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1815 thumbnail_send_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001816 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001817
1818 DestroyStreams();
ilnika014cc52017-03-07 04:21:04 -08001819 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001820
1821 if (graph_data_output_file)
1822 fclose(graph_data_output_file);
1823}
1824
minyuea27172d2016-11-01 05:59:29 -07001825void VideoQualityTest::SetupAudio(int send_channel_id,
1826 int receive_channel_id,
minyuea27172d2016-11-01 05:59:29 -07001827 Transport* transport,
1828 AudioReceiveStream** audio_receive_stream) {
1829 audio_send_config_ = AudioSendStream::Config(transport);
1830 audio_send_config_.voe_channel_id = send_channel_id;
1831 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1832
1833 // Add extension to enable audio send side BWE, and allow audio bit rate
1834 // adaptation.
1835 audio_send_config_.rtp.extensions.clear();
1836 if (params_.call.send_side_bwe) {
1837 audio_send_config_.rtp.extensions.push_back(
1838 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1839 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001840 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1841 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001842 }
ossu20a4b3f2017-04-27 02:08:52 -07001843 audio_send_config_.send_codec_spec =
1844 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
1845 {kAudioSendPayloadType,
1846 {"OPUS", 48000, 2,
1847 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
1848 {"stereo", "1"}}}});
minyue48368ad2017-05-10 04:06:11 -07001849 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07001850 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07001851
1852 AudioReceiveStream::Config audio_config;
1853 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1854 audio_config.rtcp_send_transport = transport;
1855 audio_config.voe_channel_id = receive_channel_id;
1856 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1857 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1858 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1859 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07001860 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
minyuea27172d2016-11-01 05:59:29 -07001861 if (params_.video.enabled && params_.audio.sync_video)
1862 audio_config.sync_group = kSyncGroup;
1863
sprang1168fd42017-06-21 09:00:17 -07001864 *audio_receive_stream =
1865 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07001866}
1867
minyue73208662016-08-18 06:28:55 -07001868void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001869 params_ = params;
1870 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001871
ivica5d6a06c2015-09-17 05:30:24 -07001872 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1873 // match the full stack tests.
philipel4fb651d2017-04-10 03:54:05 -07001874 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001875 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001876
1877 ::VoiceEngineState voe;
peaha9cc40b2017-06-29 08:32:09 -07001878 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
1879 webrtc::AudioProcessing::Create());
1880
minyue626bc952016-10-31 05:47:02 -07001881 if (params_.audio.enabled) {
peaha9cc40b2017-06-29 08:32:09 -07001882 CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
minyue73208662016-08-18 06:28:55 -07001883 AudioState::Config audio_state_config;
1884 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001885 audio_state_config.audio_mixer = AudioMixerImpl::Create();
peaha9cc40b2017-06-29 08:32:09 -07001886 audio_state_config.audio_processing = audio_processing;
minyue73208662016-08-18 06:28:55 -07001887 call_config.audio_state = AudioState::Create(audio_state_config);
1888 }
1889
sprang1168fd42017-06-21 09:00:17 -07001890 CreateCalls(call_config, call_config);
ivica5d6a06c2015-09-17 05:30:24 -07001891
minyuea27172d2016-11-01 05:59:29 -07001892 // TODO(minyue): consider if this is a good transport even for audio only
1893 // calls.
sprang1168fd42017-06-21 09:00:17 -07001894 test::LayerFilteringTransport send_transport(
1895 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue20c84cc2017-04-10 16:57:57 -07001896 params.video.selected_tl, params_.ss.selected_sl, payload_type_map_);
1897
sprang1168fd42017-06-21 09:00:17 -07001898 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get(),
1899 payload_type_map_);
1900
ivica5d6a06c2015-09-17 05:30:24 -07001901 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1902 // least share as much code as possible. That way this test would also match
1903 // the full stack tests better.
sprang1168fd42017-06-21 09:00:17 -07001904 send_transport.SetReceiver(receiver_call_->Receiver());
1905 recv_transport.SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001906
minyuea27172d2016-11-01 05:59:29 -07001907 std::unique_ptr<test::VideoRenderer> local_preview;
sprang1168fd42017-06-21 09:00:17 -07001908 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers_;
minyuea27172d2016-11-01 05:59:29 -07001909 if (params_.video.enabled) {
1910 // Create video renderers.
1911 local_preview.reset(test::VideoRenderer::Create(
1912 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001913
sprang1168fd42017-06-21 09:00:17 -07001914 const size_t selected_stream_id = params_.ss.selected_stream;
1915 const size_t num_streams = params_.ss.streams.size();
1916
1917 if (selected_stream_id == num_streams) {
1918 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
1919 std::ostringstream oss;
1920 oss << "Loopback Video - Stream #" << static_cast<int>(stream_id);
1921 loopback_renderers_.emplace_back(test::VideoRenderer::Create(
1922 oss.str().c_str(), params_.ss.streams[stream_id].width,
1923 params_.ss.streams[stream_id].height));
1924 }
1925 } else {
1926 loopback_renderers_.emplace_back(test::VideoRenderer::Create(
1927 "Loopback Video", params_.ss.streams[selected_stream_id].width,
1928 params_.ss.streams[selected_stream_id].height));
minyuea27172d2016-11-01 05:59:29 -07001929 }
sprangce4aef12015-11-02 07:23:20 -08001930
sprang1168fd42017-06-21 09:00:17 -07001931 SetupVideo(&send_transport, &recv_transport);
mflodman48a4beb2016-07-01 13:03:59 +02001932
minyuea27172d2016-11-01 05:59:29 -07001933 video_send_config_.pre_encode_callback = local_preview.get();
sprang1168fd42017-06-21 09:00:17 -07001934 if (selected_stream_id == num_streams) {
1935 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
1936 video_receive_configs_[stream_id].renderer =
1937 loopback_renderers_[stream_id].get();
1938 if (params_.audio.enabled && params_.audio.sync_video)
1939 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1940 }
1941 } else {
1942 video_receive_configs_[selected_stream_id].renderer =
1943 loopback_renderers_.back().get();
1944 if (params_.audio.enabled && params_.audio.sync_video)
1945 video_receive_configs_[selected_stream_id].sync_group = kSyncGroup;
1946 }
minyuea27172d2016-11-01 05:59:29 -07001947
ilnik68af10d2017-03-02 04:59:33 -08001948 if (params_.screenshare.enabled)
1949 SetupScreenshareOrSVC();
minyuea27172d2016-11-01 05:59:29 -07001950
sprang1168fd42017-06-21 09:00:17 -07001951 CreateFlexfecStreams();
1952 CreateVideoStreams();
1953
minyuea27172d2016-11-01 05:59:29 -07001954 CreateCapturer();
kthelgason2bc68642017-02-07 07:02:22 -08001955 video_send_stream_->SetSource(video_capturer_.get(),
1956 degradation_preference_);
philipel274c1dc2016-05-04 06:21:01 -07001957 }
1958
minyue73208662016-08-18 06:28:55 -07001959 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001960 if (params_.audio.enabled) {
sprang1168fd42017-06-21 09:00:17 -07001961 SetupAudio(voe.send_channel_id, voe.receive_channel_id, &send_transport,
1962 &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001963 }
1964
sprang1168fd42017-06-21 09:00:17 -07001965 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1966 StartEncodedFrameLogs(receive_stream);
palmkviste75f2042016-09-28 06:19:48 -07001967 StartEncodedFrameLogs(video_send_stream_);
1968
minyue73208662016-08-18 06:28:55 -07001969 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001970 if (params_.video.enabled) {
brandtrcb8f0452017-06-30 02:34:20 -07001971 for (FlexfecReceiveStream* flexfec_receive_stream :
1972 flexfec_receive_streams_) {
brandtr1293aca2016-11-16 22:47:29 -08001973 flexfec_receive_stream->Start();
brandtrcb8f0452017-06-30 02:34:20 -07001974 }
sprang1168fd42017-06-21 09:00:17 -07001975 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1976 receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001977 video_send_stream_->Start();
1978 video_capturer_->Start();
1979 }
ivica5d6a06c2015-09-17 05:30:24 -07001980
minyue626bc952016-10-31 05:47:02 -07001981 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001982 // Start receiving audio.
1983 audio_receive_stream->Start();
1984 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001985
1986 // Start sending audio.
1987 audio_send_stream_->Start();
1988 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1989 }
1990
ivica5d6a06c2015-09-17 05:30:24 -07001991 test::PressEnterToContinue();
1992
minyue626bc952016-10-31 05:47:02 -07001993 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001994 // Stop sending audio.
1995 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1996 audio_send_stream_->Stop();
1997
1998 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001999 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
2000 audio_receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07002001 sender_call_->DestroyAudioSendStream(audio_send_stream_);
2002 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07002003 }
2004
2005 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07002006 if (params_.video.enabled) {
2007 video_capturer_->Stop();
2008 video_send_stream_->Stop();
brandtrcb8f0452017-06-30 02:34:20 -07002009 for (FlexfecReceiveStream* flexfec_receive_stream :
2010 flexfec_receive_streams_) {
brandtr1293aca2016-11-16 22:47:29 -08002011 flexfec_receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07002012 receiver_call_->DestroyFlexfecReceiveStream(flexfec_receive_stream);
brandtr1293aca2016-11-16 22:47:29 -08002013 }
brandtrcb8f0452017-06-30 02:34:20 -07002014 for (VideoReceiveStream* receive_stream : video_receive_streams_) {
2015 receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07002016 receiver_call_->DestroyVideoReceiveStream(receive_stream);
brandtrcb8f0452017-06-30 02:34:20 -07002017 }
sprang1168fd42017-06-21 09:00:17 -07002018 sender_call_->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07002019 }
2020
sprang1168fd42017-06-21 09:00:17 -07002021 send_transport.StopSending();
2022 recv_transport.StopSending();
2023
minyue626bc952016-10-31 05:47:02 -07002024 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07002025 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07002026}
2027
palmkviste75f2042016-09-28 06:19:48 -07002028void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07002029 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002030 std::ostringstream str;
2031 str << send_logs_++;
2032 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07002033 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002034 stream->EnableEncodedFrameRecording(
2035 std::vector<rtc::PlatformFile>(
2036 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2037 rtc::CreatePlatformFile(prefix + "2.ivf"),
2038 rtc::CreatePlatformFile(prefix + "3.ivf")}),
2039 10000000);
2040 }
2041}
ilnikcb8c1462017-03-09 09:23:30 -08002042
palmkviste75f2042016-09-28 06:19:48 -07002043void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07002044 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002045 std::ostringstream str;
2046 str << receive_logs_++;
2047 std::string path =
minyue626bc952016-10-31 05:47:02 -07002048 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002049 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
2050 10000000);
2051 }
2052}
ivica5d6a06c2015-09-17 05:30:24 -07002053} // namespace webrtc