blob: 5e99b50ff0c297404be5e8eca2e75001b4e953e5 [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),
158 send_stream_(nullptr),
philipelfd870db2017-01-23 03:22:15 -0800159 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 06:53:48 -0700160 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 05:30:24 -0700161 test_label_(test_label),
162 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800163 graph_title_(graph_title),
164 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 09:16:05 -0800165 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 09:23:30 -0800166 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 05:07:56 -0800167 selected_sl_(selected_sl),
168 selected_tl_(selected_tl),
pbos14fe7082016-04-20 06:35:56 -0700169 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100170 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700171 frames_to_process_(duration_frames),
172 frames_recorded_(0),
173 frames_processed_(0),
174 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700175 dropped_frames_before_first_encode_(0),
176 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700177 last_render_time_(0),
178 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 05:07:56 -0800179 total_media_bytes_(0),
180 first_sending_time_(0),
181 last_sending_time_(0),
ilnikdf92c5c2017-02-23 02:08:44 -0800182 cpu_time_(0),
183 wallclock_time_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700184 avg_psnr_threshold_(avg_psnr_threshold),
185 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 00:53:12 -0800186 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100187 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100188 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100189 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700190 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
191
192 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
193 // so that we don't accidentally starve "real" worker threads (codec etc).
194 // Also, don't allocate more than kMaxComparisonThreads, even if there are
195 // spare cores.
196
197 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 15:21:39 -0800198 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 05:30:24 -0700199 static const uint32_t kMinCoresLeft = 4;
200 static const uint32_t kMaxComparisonThreads = 8;
201
202 if (num_cores <= kMinCoresLeft) {
203 num_cores = 1;
204 } else {
205 num_cores -= kMinCoresLeft;
206 num_cores = std::min(num_cores, kMaxComparisonThreads);
207 }
208
209 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100210 rtc::PlatformThread* thread =
211 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
212 thread->Start();
213 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700214 }
ivica5d6a06c2015-09-17 05:30:24 -0700215 }
216
217 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100218 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
219 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700220 delete thread;
221 }
222 }
223
224 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
225
ilnik6b826ef2017-06-16 06:53:48 -0700226 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
227 if (respect_sink_wants)
228 captured_frame_forwarder_.SetSource(video_capturer);
229 rtc::VideoSinkWants wants;
230 video_capturer->AddOrUpdateSink(InputInterface(), wants);
231 }
232
perkja49cbd32016-09-16 07:53:41 -0700233 void SetSendStream(VideoSendStream* stream) {
234 rtc::CritScope lock(&crit_);
235 RTC_DCHECK(!send_stream_);
236 send_stream_ = stream;
237 }
238
philipelfd870db2017-01-23 03:22:15 -0800239 void SetReceiveStream(VideoReceiveStream* stream) {
240 rtc::CritScope lock(&crit_);
241 RTC_DCHECK(!receive_stream_);
242 receive_stream_ = stream;
243 }
244
perkja49cbd32016-09-16 07:53:41 -0700245 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
246 return &captured_frame_forwarder_;
247 }
248 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
249 return &captured_frame_forwarder_;
250 }
251
ivica5d6a06c2015-09-17 05:30:24 -0700252 DeliveryStatus DeliverPacket(MediaType media_type,
253 const uint8_t* packet,
254 size_t length,
255 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700256 // Ignore timestamps of RTCP packets. They're not synchronized with
257 // RTP packet timestamps and so they would confuse wrap_handler_.
258 if (RtpHeaderParser::IsRtcp(packet, length)) {
259 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
260 }
sprangce4aef12015-11-02 07:23:20 -0800261 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700262 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800263 parser.Parse(&header);
ilnik46a00212017-02-10 09:16:05 -0800264 if (!IsFlexfec(header.payloadType) &&
265 (header.ssrc == ssrc_to_analyze_ ||
266 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 02:54:35 -0800267 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
268 // (FlexFEC and media are sent on different SSRCs, which have different
269 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800270 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 05:30:24 -0700271 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800272 int64_t timestamp =
273 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
274 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700275 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
276 }
277
278 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
279 }
280
Peter Boströme4499152016-02-05 11:13:28 +0100281 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700282 rtc::CritScope crit(&comparison_lock_);
283 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
284 }
285
pbos14fe7082016-04-20 06:35:56 -0700286 void PreEncodeOnFrame(const VideoFrame& video_frame) {
287 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800288 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 06:35:56 -0700289 while (frames_.front().timestamp() != video_frame.timestamp()) {
290 ++dropped_frames_before_first_encode_;
291 frames_.pop_front();
292 RTC_CHECK(!frames_.empty());
293 }
ilnik3dd5ad92017-02-09 04:58:53 -0800294 first_encoded_timestamp_ =
295 rtc::Optional<uint32_t>(video_frame.timestamp());
296 }
297 }
298
299 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
300 rtc::CritScope lock(&crit_);
301 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 09:23:30 -0800302 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 04:58:53 -0800303 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 06:35:56 -0700304 }
305 }
306
stefan1d8a5062015-10-02 03:39:33 -0700307 bool SendRtp(const uint8_t* packet,
308 size_t length,
309 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800310 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700311 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800312 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700313
sprangce4aef12015-11-02 07:23:20 -0800314 int64_t current_time =
315 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 02:23:28 -0800316
sprangce4aef12015-11-02 07:23:20 -0800317 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700318 {
319 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800320 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 08:32:53 -0800321 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 04:58:53 -0800322 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 04:08:56 -0800323 }
ilnik3dd5ad92017-02-09 04:58:53 -0800324
325 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 02:54:35 -0800326 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
327 // (FlexFEC and media are sent on different SSRCs, which have different
328 // timestamps spaces.)
ilnik46a00212017-02-10 09:16:05 -0800329 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 02:54:35 -0800330 int64_t timestamp =
331 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
332 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 05:07:56 -0800333
334 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 02:54:35 -0800335 encoded_frame_sizes_[timestamp] +=
336 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 05:07:56 -0800337 total_media_bytes_ +=
338 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 02:54:35 -0800339 }
ilnik1e7732c2017-02-23 05:07:56 -0800340 if (first_sending_time_ == 0)
341 first_sending_time_ = current_time;
342 last_sending_time_ = current_time;
sprangce4aef12015-11-02 07:23:20 -0800343 }
ivica5d6a06c2015-09-17 05:30:24 -0700344 }
sprangce4aef12015-11-02 07:23:20 -0800345 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700346 }
347
348 bool SendRtcp(const uint8_t* packet, size_t length) override {
349 return transport_->SendRtcp(packet, length);
350 }
351
nisseeb83a1a2016-03-21 01:27:56 -0700352 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700353 int64_t render_time_ms =
354 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700355
356 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 02:08:44 -0800357
358 StartExcludingCpuThreadTime();
359
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700360 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800361 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700362
sprang16daaa52016-03-09 01:30:24 -0800363 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700364 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700365 // No previous frame rendered, this one was dropped after sending but
366 // before rendering.
367 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 07:02:22 -0800368 } else {
369 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
370 render_time_ms);
pbos14fe7082016-04-20 06:35:56 -0700371 }
ivica5d6a06c2015-09-17 05:30:24 -0700372 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700373 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700374 }
375
376 VideoFrame reference_frame = frames_.front();
377 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800378 int64_t reference_timestamp =
379 wrap_handler_.Unwrap(reference_frame.timestamp());
380 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800381 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100382 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800383 ++send_timestamp;
384 }
sprang16daaa52016-03-09 01:30:24 -0800385 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700386
387 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
388
nisse97f0b932016-05-26 09:44:40 -0700389 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 02:08:44 -0800390
391 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 05:30:24 -0700392 }
393
ivica5d6a06c2015-09-17 05:30:24 -0700394 void Wait() {
395 // Frame comparisons can be very expensive. Wait for test to be done, but
396 // at time-out check if frames_processed is going up. If so, give it more
397 // time, otherwise fail. Hopefully this will reduce test flakiness.
398
Peter Boström8c38e8b2015-11-26 17:45:47 +0100399 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800400
ivica5d6a06c2015-09-17 05:30:24 -0700401 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700402 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100403 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700404 int frames_processed;
405 {
406 rtc::CritScope crit(&comparison_lock_);
407 frames_processed = frames_processed_;
408 }
409
410 // Print some output so test infrastructure won't think we've crashed.
411 const char* kKeepAliveMessages[3] = {
412 "Uh, I'm-I'm not quite dead, sir.",
413 "Uh, I-I think uh, I could pull through, sir.",
414 "Actually, I think I'm all right to come with you--"};
415 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
416
417 if (last_frames_processed == -1) {
418 last_frames_processed = frames_processed;
419 continue;
420 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100421 if (frames_processed == last_frames_processed) {
422 EXPECT_GT(frames_processed, last_frames_processed)
423 << "Analyzer stalled while waiting for test to finish.";
424 done_.Set();
425 break;
426 }
ivica5d6a06c2015-09-17 05:30:24 -0700427 last_frames_processed = frames_processed;
428 }
429
430 if (iteration > 0)
431 printf("- Farewell, sweet Concorde!\n");
432
Peter Boström8c38e8b2015-11-26 17:45:47 +0100433 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700434 }
435
pbos14fe7082016-04-20 06:35:56 -0700436 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
437 return &pre_encode_proxy_;
438 }
Peter Boströme4499152016-02-05 11:13:28 +0100439 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
440
ilnikdf92c5c2017-02-23 02:08:44 -0800441 void StartMeasuringCpuProcessTime() {
442 rtc::CritScope lock(&cpu_measurement_lock_);
443 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
444 wallclock_time_ -= rtc::SystemTimeNanos();
445 }
446
447 void StopMeasuringCpuProcessTime() {
448 rtc::CritScope lock(&cpu_measurement_lock_);
449 cpu_time_ += rtc::GetProcessCpuTimeNanos();
450 wallclock_time_ += rtc::SystemTimeNanos();
451 }
452
453 void StartExcludingCpuThreadTime() {
454 rtc::CritScope lock(&cpu_measurement_lock_);
455 cpu_time_ += rtc::GetThreadCpuTimeNanos();
456 }
457
458 void StopExcludingCpuThreadTime() {
459 rtc::CritScope lock(&cpu_measurement_lock_);
460 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
461 }
462
463 double GetCpuUsagePercent() {
464 rtc::CritScope lock(&cpu_measurement_lock_);
465 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
466 }
467
sprangce4aef12015-11-02 07:23:20 -0800468 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700469 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700470
471 private:
472 struct FrameComparison {
473 FrameComparison()
474 : dropped(false),
nissedf2ceb82016-12-15 06:29:53 -0800475 input_time_ms(0),
ivica5d6a06c2015-09-17 05:30:24 -0700476 send_time_ms(0),
477 recv_time_ms(0),
478 render_time_ms(0),
479 encoded_frame_size(0) {}
480
481 FrameComparison(const VideoFrame& reference,
482 const VideoFrame& render,
483 bool dropped,
nissedf2ceb82016-12-15 06:29:53 -0800484 int64_t input_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700485 int64_t send_time_ms,
486 int64_t recv_time_ms,
487 int64_t render_time_ms,
488 size_t encoded_frame_size)
489 : reference(reference),
490 render(render),
491 dropped(dropped),
nissedf2ceb82016-12-15 06:29:53 -0800492 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700493 send_time_ms(send_time_ms),
494 recv_time_ms(recv_time_ms),
495 render_time_ms(render_time_ms),
496 encoded_frame_size(encoded_frame_size) {}
497
nissedf2ceb82016-12-15 06:29:53 -0800498 FrameComparison(bool dropped,
499 int64_t input_time_ms,
500 int64_t send_time_ms,
501 int64_t recv_time_ms,
502 int64_t render_time_ms,
503 size_t encoded_frame_size)
504 : dropped(dropped),
505 input_time_ms(input_time_ms),
506 send_time_ms(send_time_ms),
507 recv_time_ms(recv_time_ms),
508 render_time_ms(render_time_ms),
509 encoded_frame_size(encoded_frame_size) {}
510
511 rtc::Optional<VideoFrame> reference;
512 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 05:30:24 -0700513 bool dropped;
nissedf2ceb82016-12-15 06:29:53 -0800514 int64_t input_time_ms;
ivica5d6a06c2015-09-17 05:30:24 -0700515 int64_t send_time_ms;
516 int64_t recv_time_ms;
517 int64_t render_time_ms;
518 size_t encoded_frame_size;
519 };
520
521 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700522 Sample(int dropped,
523 int64_t input_time_ms,
524 int64_t send_time_ms,
525 int64_t recv_time_ms,
526 int64_t render_time_ms,
527 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700528 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700529 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700530 : dropped(dropped),
531 input_time_ms(input_time_ms),
532 send_time_ms(send_time_ms),
533 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700534 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700535 encoded_frame_size(encoded_frame_size),
536 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700537 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700538
ivica8d15bd62015-10-07 02:43:12 -0700539 int dropped;
540 int64_t input_time_ms;
541 int64_t send_time_ms;
542 int64_t recv_time_ms;
543 int64_t render_time_ms;
544 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700545 double psnr;
546 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700547 };
548
Peter Boströme4499152016-02-05 11:13:28 +0100549 // This class receives the send-side OnEncodeTiming and is provided to not
550 // conflict with the receiver-side pre_decode_callback.
551 class OnEncodeTimingProxy : public EncodedFrameObserver {
552 public:
553 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
554
555 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
556 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
557 }
ilnik3dd5ad92017-02-09 04:58:53 -0800558 void EncodedFrameCallback(const EncodedFrame& frame) override {
559 parent_->PostEncodeFrameCallback(frame);
560 }
Peter Boströme4499152016-02-05 11:13:28 +0100561
562 private:
563 VideoAnalyzer* const parent_;
564 };
565
pbos14fe7082016-04-20 06:35:56 -0700566 // This class receives the send-side OnFrame callback and is provided to not
567 // conflict with the receiver-side renderer callback.
568 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
569 public:
570 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
571
572 void OnFrame(const VideoFrame& video_frame) override {
573 parent_->PreEncodeOnFrame(video_frame);
574 }
575
576 private:
577 VideoAnalyzer* const parent_;
578 };
579
ilnik1e7732c2017-02-23 05:07:56 -0800580 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
581 size_t length,
582 const RTPHeader& header) {
583 if (header.payloadType != kPayloadTypeVP9 &&
584 header.payloadType != kPayloadTypeVP8) {
585 return true;
586 } else {
587 // Get VP8 and VP9 specific header to check layers indexes.
588 const uint8_t* payload = packet + header.headerLength;
589 const size_t payload_length = length - header.headerLength;
590 const size_t payload_data_length = payload_length - header.paddingLength;
591 const bool is_vp8 = header.payloadType == kPayloadTypeVP8;
592 std::unique_ptr<RtpDepacketizer> depacketizer(
593 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
594 RtpDepacketizer::ParsedPayload parsed_payload;
595 bool result =
596 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
597 RTC_DCHECK(result);
598 const int temporal_idx = static_cast<int>(
599 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
600 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
601 const int spatial_idx = static_cast<int>(
602 is_vp8 ? kNoSpatialIdx
603 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
604 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
605 temporal_idx <= selected_tl_) &&
606 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
607 spatial_idx <= selected_sl_);
608 }
609 }
610
ivica5d6a06c2015-09-17 05:30:24 -0700611 void AddFrameComparison(const VideoFrame& reference,
612 const VideoFrame& render,
613 bool dropped,
614 int64_t render_time_ms)
615 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800616 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
617 int64_t send_time_ms = send_times_[reference_timestamp];
618 send_times_.erase(reference_timestamp);
619 int64_t recv_time_ms = recv_times_[reference_timestamp];
620 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700621
sprangce4aef12015-11-02 07:23:20 -0800622 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800623 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800624 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800625 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800626 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
627 if (it != encoded_frame_sizes_.end())
628 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700629
ivica5d6a06c2015-09-17 05:30:24 -0700630 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700631 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 06:29:53 -0800632 comparisons_.push_back(FrameComparison(reference, render, dropped,
633 reference.ntp_time_ms(),
634 send_time_ms, recv_time_ms,
635 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700636 } else {
nissedf2ceb82016-12-15 06:29:53 -0800637 comparisons_.push_back(FrameComparison(dropped,
638 reference.ntp_time_ms(),
639 send_time_ms, recv_time_ms,
640 render_time_ms, encoded_size));
stefanb1797672016-08-11 07:00:57 -0700641 }
Peter Boström5811a392015-12-10 13:02:50 +0100642 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700643 }
644
tommi0f8b4032017-02-22 11:22:05 -0800645 static void PollStatsThread(void* obj) {
646 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 05:30:24 -0700647 }
648
tommi0f8b4032017-02-22 11:22:05 -0800649 void PollStats() {
650 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
651 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700652
tommi0f8b4032017-02-22 11:22:05 -0800653 VideoSendStream::Stats send_stats = send_stream_->GetStats();
654 // It's not certain that we yet have estimates for any of these stats.
655 // Check that they are positive before mixing them in.
656 if (send_stats.encode_frame_rate > 0)
657 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
658 if (send_stats.avg_encode_time_ms > 0)
659 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
660 if (send_stats.encode_usage_percent > 0)
661 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
662 if (send_stats.media_bitrate_bps > 0)
663 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
philipelfd870db2017-01-23 03:22:15 -0800664
tommi0f8b4032017-02-22 11:22:05 -0800665 if (receive_stream_ != nullptr) {
666 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
667 if (receive_stats.decode_ms > 0)
668 decode_time_ms_.AddSample(receive_stats.decode_ms);
669 if (receive_stats.max_decode_ms > 0)
670 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
671 }
ilnikdaa574d2017-03-01 06:46:05 -0800672
673 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 03:22:15 -0800674 }
ivica5d6a06c2015-09-17 05:30:24 -0700675 }
676
677 static bool FrameComparisonThread(void* obj) {
678 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
679 }
680
681 bool CompareFrames() {
682 if (AllFramesRecorded())
683 return false;
684
ivica5d6a06c2015-09-17 05:30:24 -0700685 FrameComparison comparison;
686
687 if (!PopComparison(&comparison)) {
688 // Wait until new comparison task is available, or test is done.
689 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100690 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700691 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100692 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700693 return false;
694 }
695 return true; // Try again.
696 }
697
ilnikdf92c5c2017-02-23 02:08:44 -0800698 StartExcludingCpuThreadTime();
699
ivica5d6a06c2015-09-17 05:30:24 -0700700 PerformFrameComparison(comparison);
701
ilnikdf92c5c2017-02-23 02:08:44 -0800702 StopExcludingCpuThreadTime();
703
ivica5d6a06c2015-09-17 05:30:24 -0700704 if (FrameProcessed()) {
705 PrintResults();
706 if (graph_data_output_file_)
707 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100708 done_.Set();
709 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700710 return false;
711 }
712
713 return true;
714 }
715
716 bool PopComparison(FrameComparison* comparison) {
717 rtc::CritScope crit(&comparison_lock_);
718 // If AllFramesRecorded() is true, it means we have already popped
719 // frames_to_process_ frames from comparisons_, so there is no more work
720 // for this thread to be done. frames_processed_ might still be lower if
721 // all comparisons are not done, but those frames are currently being
722 // worked on by other threads.
723 if (comparisons_.empty() || AllFramesRecorded())
724 return false;
725
726 *comparison = comparisons_.front();
727 comparisons_.pop_front();
728
729 FrameRecorded();
730 return true;
731 }
732
733 // Increment counter for number of frames received for comparison.
734 void FrameRecorded() {
735 rtc::CritScope crit(&comparison_lock_);
736 ++frames_recorded_;
737 }
738
739 // Returns true if all frames to be compared have been taken from the queue.
740 bool AllFramesRecorded() {
741 rtc::CritScope crit(&comparison_lock_);
742 assert(frames_recorded_ <= frames_to_process_);
743 return frames_recorded_ == frames_to_process_;
744 }
745
746 // Increase count of number of frames processed. Returns true if this was the
747 // last frame to be processed.
748 bool FrameProcessed() {
749 rtc::CritScope crit(&comparison_lock_);
750 ++frames_processed_;
751 assert(frames_processed_ <= frames_to_process_);
752 return frames_processed_ == frames_to_process_;
753 }
754
755 void PrintResults() {
ilnikdf92c5c2017-02-23 02:08:44 -0800756 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 05:30:24 -0700757 rtc::CritScope crit(&comparison_lock_);
758 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800759 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 01:02:15 -0700760 PrintResult("sender_time", sender_time_, " ms");
761 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700762 PrintResult("total_delay_incl_network", end_to_end_, " ms");
763 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700764 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 03:22:15 -0800765 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800766 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
767
768 if (receive_stream_ != nullptr) {
769 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 03:22:15 -0800770 }
ivica5d6a06c2015-09-17 05:30:24 -0700771
pbos14fe7082016-04-20 06:35:56 -0700772 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
773 dropped_frames_);
ilnikdf92c5c2017-02-23 02:08:44 -0800774 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
775 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 06:46:05 -0800776
777#if defined(WEBRTC_WIN)
778 // On Linux and Mac in Resident Set some unused pages may be counted.
779 // Therefore this metric will depend on order in which tests are run and
780 // will be flaky.
781 PrintResult("memory_usage", memory_usage_, " bytes");
782#endif
783
ilnik9ae0d762017-02-15 00:53:12 -0800784 // Disable quality check for quick test, as quality checks may fail
785 // because too few samples were collected.
786 if (!is_quick_test_enabled_) {
787 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
788 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
789 }
ivica5d6a06c2015-09-17 05:30:24 -0700790 }
791
792 void PerformFrameComparison(const FrameComparison& comparison) {
793 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700794 double psnr = -1.0;
795 double ssim = -1.0;
ilnik6b826ef2017-06-16 06:53:48 -0700796 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 06:29:53 -0800797 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
798 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 07:00:57 -0700799 }
ivica5d6a06c2015-09-17 05:30:24 -0700800
ivica5d6a06c2015-09-17 05:30:24 -0700801 rtc::CritScope crit(&comparison_lock_);
802 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 06:29:53 -0800803 samples_.push_back(Sample(
804 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
805 comparison.recv_time_ms, comparison.render_time_ms,
806 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700807 }
stefanb1797672016-08-11 07:00:57 -0700808 if (psnr >= 0.0)
809 psnr_.AddSample(psnr);
810 if (ssim >= 0.0)
811 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700812
813 if (comparison.dropped) {
814 ++dropped_frames_;
815 return;
816 }
817 if (last_render_time_ != 0)
818 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
819 last_render_time_ = comparison.render_time_ms;
820
nissedf2ceb82016-12-15 06:29:53 -0800821 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 02:54:35 -0800822 if (comparison.recv_time_ms > 0) {
823 // If recv_time_ms == 0, this frame consisted of a packets which were all
824 // lost in the transport. Since we were able to render the frame, however,
825 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
826 // happens internally in Call, and we can therefore here not know which
827 // FEC packets that protected the lost media packets. Consequently, we
828 // were not able to record a meaningful recv_time_ms. We therefore skip
829 // this sample.
830 //
831 // The reasoning above does not hold for ULPFEC and RTX, as for those
832 // strategies the timestamp of the received packets is set to the
833 // timestamp of the protected/retransmitted media packet. I.e., then
834 // recv_time_ms != 0, even though the media packets were lost.
835 receiver_time_.AddSample(comparison.render_time_ms -
836 comparison.recv_time_ms);
837 }
nissedf2ceb82016-12-15 06:29:53 -0800838 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 05:30:24 -0700839 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
840 }
841
842 void PrintResult(const char* result_type,
843 test::Statistics stats,
844 const char* unit) {
845 printf("RESULT %s: %s = {%f, %f}%s\n",
846 result_type,
847 test_label_.c_str(),
848 stats.Mean(),
849 stats.StandardDeviation(),
850 unit);
851 }
852
853 void PrintSamplesToFile(void) {
854 FILE* out = graph_data_output_file_;
855 rtc::CritScope crit(&comparison_lock_);
856 std::sort(samples_.begin(), samples_.end(),
857 [](const Sample& A, const Sample& B) -> bool {
858 return A.input_time_ms < B.input_time_ms;
859 });
860
sprangce4aef12015-11-02 07:23:20 -0800861 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700862 fprintf(out, "%" PRIuS "\n", samples_.size());
863 fprintf(out,
864 "dropped "
865 "input_time_ms "
866 "send_time_ms "
867 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700868 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700869 "encoded_frame_size "
870 "psnr "
871 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700872 "encode_time_ms\n");
873 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700874 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700875 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
876 int encode_time_ms;
877 if (it != samples_encode_time_ms_.end()) {
878 encode_time_ms = it->second;
879 } else {
880 ++missing_encode_time_samples;
881 encode_time_ms = -1;
882 }
883 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
884 " %lf %lf %d\n",
885 sample.dropped, sample.input_time_ms, sample.send_time_ms,
886 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700887 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700888 encode_time_ms);
889 }
890 if (missing_encode_time_samples) {
891 fprintf(stderr,
892 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
893 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700894 }
895 }
896
ilnik1e7732c2017-02-23 05:07:56 -0800897 double GetAverageMediaBitrateBps() {
898 if (last_sending_time_ == first_sending_time_) {
899 return 0;
900 } else {
901 return static_cast<double>(total_media_bytes_) * 8 /
902 (last_sending_time_ - first_sending_time_) *
903 rtc::kNumMillisecsPerSec;
904 }
905 }
906
perkja49cbd32016-09-16 07:53:41 -0700907 // Implements VideoSinkInterface to receive captured frames from a
908 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
909 // as a source to VideoSendStream.
910 // It forwards all input frames to the VideoAnalyzer for later comparison and
911 // forwards the captured frames to the VideoSendStream.
912 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
913 public rtc::VideoSourceInterface<VideoFrame> {
914 public:
ilnik6b826ef2017-06-16 06:53:48 -0700915 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
916 : analyzer_(analyzer),
917 send_stream_input_(nullptr),
918 video_capturer_(nullptr),
919 clock_(clock) {}
920
921 void SetSource(test::VideoCapturer* video_capturer) {
922 video_capturer_ = video_capturer;
923 }
perkja49cbd32016-09-16 07:53:41 -0700924
925 private:
926 void OnFrame(const VideoFrame& video_frame) override {
927 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 04:58:53 -0800928 // Frames from the capturer does not have a rtp timestamp.
929 // Create one so it can be used for comparison.
930 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 06:53:48 -0700931 if (video_frame.ntp_time_ms() == 0)
932 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 07:53:41 -0700933 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 04:58:53 -0800934 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 07:53:41 -0700935 rtc::CritScope lock(&crit_);
936 if (send_stream_input_)
ilnikb82ac6a2017-05-02 00:48:41 -0700937 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 07:53:41 -0700938 }
939
940 // Called when |send_stream_.SetSource()| is called.
941 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
942 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 07:21:01 -0700943 {
944 rtc::CritScope lock(&crit_);
945 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
946 send_stream_input_ = sink;
947 }
ilnik6b826ef2017-06-16 06:53:48 -0700948 if (video_capturer_) {
949 video_capturer_->AddOrUpdateSink(this, wants);
950 }
perkja49cbd32016-09-16 07:53:41 -0700951 }
952
953 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
954 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
955 rtc::CritScope lock(&crit_);
956 RTC_DCHECK(sink == send_stream_input_);
957 send_stream_input_ = nullptr;
958 }
959
960 VideoAnalyzer* const analyzer_;
961 rtc::CriticalSection crit_;
962 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 06:53:48 -0700963 test::VideoCapturer* video_capturer_;
964 Clock* clock_;
perkja49cbd32016-09-16 07:53:41 -0700965 };
966
967 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
968 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 04:58:53 -0800969 frames_.push_back(video_frame);
perkja49cbd32016-09-16 07:53:41 -0700970 }
971
972 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 03:22:15 -0800973 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 07:53:41 -0700974 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700975 const std::string test_label_;
976 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800977 const std::string graph_title_;
978 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 09:16:05 -0800979 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 09:23:30 -0800980 const size_t selected_stream_;
ilnik1e7732c2017-02-23 05:07:56 -0800981 const int selected_sl_;
982 const int selected_tl_;
pbos14fe7082016-04-20 06:35:56 -0700983 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100984 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700985 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700986 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700987 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
988 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
989 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
990 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
991 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
992 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
993 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
994 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
philipelfd870db2017-01-23 03:22:15 -0800995 test::Statistics encode_time_ms_ GUARDED_BY(comparison_lock_);
996 test::Statistics encode_usage_percent_ GUARDED_BY(comparison_lock_);
997 test::Statistics decode_time_ms_ GUARDED_BY(comparison_lock_);
998 test::Statistics decode_time_max_ms_ GUARDED_BY(comparison_lock_);
999 test::Statistics media_bitrate_bps_ GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 06:46:05 -08001000 test::Statistics memory_usage_ GUARDED_BY(comparison_lock_);
1001
ivica5d6a06c2015-09-17 05:30:24 -07001002
1003 const int frames_to_process_;
1004 int frames_recorded_;
1005 int frames_processed_;
1006 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -07001007 int dropped_frames_before_first_encode_;
1008 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -07001009 int64_t last_render_time_;
1010 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 05:07:56 -08001011 int64_t total_media_bytes_;
1012 int64_t first_sending_time_;
1013 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 05:30:24 -07001014
ilnikdf92c5c2017-02-23 02:08:44 -08001015 int64_t cpu_time_ GUARDED_BY(cpu_measurement_lock_);
1016 int64_t wallclock_time_ GUARDED_BY(cpu_measurement_lock_);
1017 rtc::CriticalSection cpu_measurement_lock_;
1018
ivica5d6a06c2015-09-17 05:30:24 -07001019 rtc::CriticalSection crit_;
1020 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -07001021 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -08001022 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
1023 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
1024 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
1025 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
ilnik3dd5ad92017-02-09 04:58:53 -08001026 rtc::Optional<uint32_t> first_encoded_timestamp_ GUARDED_BY(crit_);
1027 rtc::Optional<uint32_t> first_sent_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -07001028 const double avg_psnr_threshold_;
1029 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 00:53:12 -08001030 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 05:30:24 -07001031
1032 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +01001033 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1034 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +01001035 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -07001036 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +01001037 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -07001038};
1039
ilnikcb8c1462017-03-09 09:23:30 -08001040class Vp8EncoderFactory : public VideoEncoderFactory {
1041 public:
1042 Vp8EncoderFactory() = default;
1043 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1044
1045 VideoEncoder* Create() override {
1046 VideoEncoder* encoder = VP8Encoder::Create();
1047 live_encoders_.insert(encoder);
1048 return encoder;
1049 }
1050
1051 void Destroy(VideoEncoder* encoder) override {
1052 auto it = live_encoders_.find(encoder);
1053 RTC_CHECK(it != live_encoders_.end());
1054 live_encoders_.erase(it);
1055 delete encoder;
1056 }
1057
1058 std::set<VideoEncoder*> live_encoders_;
1059};
1060
palmkviste75f2042016-09-28 06:19:48 -07001061VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 16:57:57 -07001062 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1063 payload_type_map_ = test::CallTest::payload_type_map_;
1064 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1065 payload_type_map_.end());
1066 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1067 payload_type_map_.end());
1068 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1069 payload_type_map_.end());
1070 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1071 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1072 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1073}
ivica5d6a06c2015-09-17 05:30:24 -07001074
minyue626bc952016-10-31 05:47:02 -07001075VideoQualityTest::Params::Params()
1076 : call({false, Call::Config::BitrateConfig()}),
1077 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-16 22:47:29 -08001078 false, "", ""}),
minyue4c8b9422017-03-21 04:11:43 -07001079 audio({false, false, false}),
minyue626bc952016-10-31 05:47:02 -07001080 screenshare({false, 10, 0}),
1081 analyzer({"", 0.0, 0.0, 0, "", ""}),
1082 pipe(),
1083 logs(false),
ilnika014cc52017-03-07 04:21:04 -08001084 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
1085 num_thumbnails(0) {}
minyue626bc952016-10-31 05:47:02 -07001086
1087VideoQualityTest::Params::~Params() = default;
1088
ivica5d6a06c2015-09-17 05:30:24 -07001089void VideoQualityTest::TestBody() {}
1090
sprangce4aef12015-11-02 07:23:20 -08001091std::string VideoQualityTest::GenerateGraphTitle() const {
1092 std::stringstream ss;
minyue626bc952016-10-31 05:47:02 -07001093 ss << params_.video.codec;
1094 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1095 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -08001096 if (params_.screenshare.scroll_duration)
1097 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1098 if (params_.ss.streams.size() > 1)
1099 ss << ", Stream #" << params_.ss.selected_stream;
1100 if (params_.ss.num_spatial_layers > 1)
1101 ss << ", Layer #" << params_.ss.selected_sl;
1102 ss << ")";
1103 return ss.str();
1104}
1105
1106void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 07:14:08 -08001107 if (!params_.video.enabled)
1108 return;
sprangce4aef12015-11-02 07:23:20 -08001109 // Add a default stream in none specified.
1110 if (params_.ss.streams.empty())
1111 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1112 if (params_.ss.num_spatial_layers == 0)
1113 params_.ss.num_spatial_layers = 1;
1114
1115 if (params_.pipe.loss_percent != 0 ||
1116 params_.pipe.queue_length_packets != 0) {
1117 // Since LayerFilteringTransport changes the sequence numbers, we can't
1118 // use that feature with pack loss, since the NACK request would end up
1119 // retransmitting the wrong packets.
1120 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -08001121 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 05:47:02 -07001122 RTC_CHECK(params_.video.selected_tl == -1 ||
1123 params_.video.selected_tl ==
1124 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -08001125 }
1126
1127 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1128 // does in some parts of the code?
minyue626bc952016-10-31 05:47:02 -07001129 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1130 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1131 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprang1168fd42017-06-21 09:00:17 -07001132 RTC_CHECK_LE(params_.ss.selected_stream, params_.ss.streams.size());
sprangce4aef12015-11-02 07:23:20 -08001133 for (const VideoStream& stream : params_.ss.streams) {
1134 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1135 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1136 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
sprangce4aef12015-11-02 07:23:20 -08001137 }
1138 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1139 // the total bitrate? We anyway have to update them in the case bitrate
1140 // estimator changes the total bitrates.
1141 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1142 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1143 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1144 params_.ss.spatial_layers.size() ==
1145 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 05:47:02 -07001146 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -08001147 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 05:47:02 -07001148 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 15:21:39 -08001149 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 07:23:20 -08001150 }
ilnika014cc52017-03-07 04:21:04 -08001151 RTC_CHECK_GE(params_.num_thumbnails, 0);
1152 if (params_.num_thumbnails > 0) {
1153 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1154 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1155 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1156 RTC_CHECK_EQ(params_.video.codec, "VP8");
1157 }
sprangce4aef12015-11-02 07:23:20 -08001158}
1159
1160// Static.
1161std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1162 // Parse comma separated nonnegative integers, where some elements may be
1163 // empty. The empty values are replaced with -1.
1164 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1165 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1166 std::vector<int> result;
1167 if (str.empty())
1168 return result;
1169
1170 const char* p = str.c_str();
1171 int value = -1;
1172 int pos;
1173 while (*p) {
1174 if (*p == ',') {
1175 result.push_back(value);
1176 value = -1;
1177 ++p;
1178 continue;
1179 }
1180 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1181 << "Unexpected non-number value.";
1182 p += pos;
1183 }
1184 result.push_back(value);
1185 return result;
1186}
1187
1188// Static.
1189VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1190 VideoStream stream;
minyue626bc952016-10-31 05:47:02 -07001191 stream.width = params.video.width;
1192 stream.height = params.video.height;
1193 stream.max_framerate = params.video.fps;
1194 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1195 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1196 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprang1168fd42017-06-21 09:00:17 -07001197 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 02:01:32 -07001198 // TODO(sprang): Can we make this less of a hack?
1199 if (params.video.num_temporal_layers == 2) {
1200 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1201 } else if (params.video.num_temporal_layers == 3) {
1202 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1203 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
ilnika014cc52017-03-07 04:21:04 -08001204 }
1205 return stream;
1206}
1207
1208// Static.
1209VideoStream VideoQualityTest::DefaultThumbnailStream() {
1210 VideoStream stream;
1211 stream.width = 320;
1212 stream.height = 180;
1213 stream.max_framerate = 7;
1214 stream.min_bitrate_bps = 7500;
1215 stream.target_bitrate_bps = 37500;
1216 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 09:00:17 -07001217 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 07:23:20 -08001218 return stream;
1219}
1220
1221// Static.
1222void VideoQualityTest::FillScalabilitySettings(
1223 Params* params,
1224 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 09:00:17 -07001225 int num_streams,
sprangce4aef12015-11-02 07:23:20 -08001226 size_t selected_stream,
1227 int num_spatial_layers,
1228 int selected_sl,
1229 const std::vector<std::string>& sl_descriptors) {
sprang1168fd42017-06-21 09:00:17 -07001230 if (params->ss.streams.empty() && params->ss.infer_streams) {
1231 webrtc::VideoEncoderConfig encoder_config;
1232 encoder_config.content_type =
1233 params->screenshare.enabled
1234 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1235 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
1236 encoder_config.max_bitrate_bps = params->video.max_bitrate_bps;
1237 encoder_config.min_transmit_bitrate_bps = params->video.min_transmit_bps;
1238 encoder_config.number_of_streams = num_streams;
1239 encoder_config.spatial_layers = params->ss.spatial_layers;
1240 encoder_config.video_stream_factory =
1241 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1242 params->video.codec, kDefaultMaxQp, params->video.fps,
1243 params->screenshare.enabled, true);
1244 params->ss.streams =
1245 encoder_config.video_stream_factory->CreateEncoderStreams(
1246 static_cast<int>(params->video.width),
1247 static_cast<int>(params->video.height), encoder_config);
1248 } else {
1249 // Read VideoStream and SpatialLayer elements from a list of comma separated
1250 // lists. To use a default value for an element, use -1 or leave empty.
1251 // Validity checks performed in CheckParams.
1252 RTC_CHECK(params->ss.streams.empty());
1253 for (auto descriptor : stream_descriptors) {
1254 if (descriptor.empty())
1255 continue;
1256 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1257 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1258 if (v[0] != -1)
1259 stream.width = static_cast<size_t>(v[0]);
1260 if (v[1] != -1)
1261 stream.height = static_cast<size_t>(v[1]);
1262 if (v[2] != -1)
1263 stream.max_framerate = v[2];
1264 if (v[3] != -1)
1265 stream.min_bitrate_bps = v[3];
1266 if (v[4] != -1)
1267 stream.target_bitrate_bps = v[4];
1268 if (v[5] != -1)
1269 stream.max_bitrate_bps = v[5];
1270 if (v.size() > 6 && v[6] != -1)
1271 stream.max_qp = v[6];
1272 if (v.size() > 7) {
1273 stream.temporal_layer_thresholds_bps.clear();
1274 stream.temporal_layer_thresholds_bps.insert(
1275 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1276 } else {
1277 // Automatic TL thresholds for more than two layers not supported.
1278 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
1279 }
1280 params->ss.streams.push_back(stream);
sprangce4aef12015-11-02 07:23:20 -08001281 }
sprangce4aef12015-11-02 07:23:20 -08001282 }
sprangce4aef12015-11-02 07:23:20 -08001283
sprang1168fd42017-06-21 09:00:17 -07001284 params->ss.num_spatial_layers = std::max(1, num_spatial_layers);
1285 params->ss.selected_stream = selected_stream;
1286
sprangce4aef12015-11-02 07:23:20 -08001287 params->ss.selected_sl = selected_sl;
1288 RTC_CHECK(params->ss.spatial_layers.empty());
1289 for (auto descriptor : sl_descriptors) {
1290 if (descriptor.empty())
1291 continue;
1292 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1293 RTC_CHECK_GT(v[2], 0);
1294
1295 SpatialLayer layer;
1296 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1297 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1298 layer.target_bitrate_bps = v[2];
1299 params->ss.spatial_layers.push_back(layer);
1300 }
1301}
1302
minyuea27172d2016-11-01 05:59:29 -07001303void VideoQualityTest::SetupVideo(Transport* send_transport,
1304 Transport* recv_transport) {
sprangce4aef12015-11-02 07:23:20 -08001305 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -07001306 trace_to_stderr_.reset(new test::TraceToStderr);
1307
brandtr8313a6f2017-01-13 07:41:19 -08001308 size_t num_video_streams = params_.ss.streams.size();
1309 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1310 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -07001311
1312 int payload_type;
minyue626bc952016-10-31 05:47:02 -07001313 if (params_.video.codec == "H264") {
magjedceecea42016-11-28 07:20:21 -08001314 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 01:36:03 -08001315 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 05:47:02 -07001316 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 09:23:30 -08001317 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
1318 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1319 // encoders usually can't natively do simulcast with different frame rates
1320 // for the different layers.
1321 video_encoder_.reset(
1322 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
1323 } else {
1324 video_encoder_.reset(VP8Encoder::Create());
1325 }
ivica5d6a06c2015-09-17 05:30:24 -07001326 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 05:47:02 -07001327 } else if (params_.video.codec == "VP9") {
magjed509e4fe2016-11-18 01:34:11 -08001328 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 05:30:24 -07001329 payload_type = kPayloadTypeVP9;
1330 } else {
1331 RTC_NOTREACHED() << "Codec not supported!";
1332 return;
1333 }
minyuea27172d2016-11-01 05:59:29 -07001334 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 05:47:02 -07001335 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 03:14:00 -08001336 video_send_config_.encoder_settings.payload_type = payload_type;
1337 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1338 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 07:41:19 -08001339 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001340 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001341
stefanff483612015-12-21 03:14:00 -08001342 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 05:47:02 -07001343 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001344 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001345 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001346 test::kTransportSequenceNumberExtensionId));
1347 } else {
stefanff483612015-12-21 03:14:00 -08001348 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001349 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001350 }
ilnik00d802b2017-04-11 10:34:31 -07001351 video_send_config_.rtp.extensions.push_back(RtpExtension(
1352 RtpExtension::kVideoContentTypeUri, test::kVideoContentTypeExtensionId));
ilnik04f4d122017-06-19 07:18:55 -07001353 video_send_config_.rtp.extensions.push_back(RtpExtension(
1354 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
Erik Språng6b8d3552015-09-24 15:06:57 +02001355
stefanff483612015-12-21 03:14:00 -08001356 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 05:47:02 -07001357 params_.video.min_transmit_bps;
perkjfa10b552016-10-02 23:45:26 -07001358
brandtr1293aca2016-11-16 22:47:29 -08001359 video_send_config_.suspend_below_min_bitrate =
1360 params_.video.suspend_below_min_bitrate;
1361
perkjfa10b552016-10-02 23:45:26 -07001362 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1363 video_encoder_config_.max_bitrate_bps = 0;
1364 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1365 video_encoder_config_.max_bitrate_bps +=
1366 params_.ss.streams[i].max_bitrate_bps;
1367 }
ilnik6b826ef2017-06-16 06:53:48 -07001368 if (params_.ss.infer_streams) {
1369 video_encoder_config_.video_stream_factory =
1370 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1371 params_.video.codec, params_.ss.streams[0].max_qp,
1372 params_.video.fps, params_.screenshare.enabled, true);
1373 } else {
1374 video_encoder_config_.video_stream_factory =
1375 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1376 }
perkjfa10b552016-10-02 23:45:26 -07001377
stefanff483612015-12-21 03:14:00 -08001378 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001379
1380 CreateMatchingReceiveConfigs(recv_transport);
1381
sprang1168fd42017-06-21 09:00:17 -07001382 const bool decode_all_receive_streams =
1383 params_.ss.selected_stream == params_.ss.streams.size();
1384
brandtr8313a6f2017-01-13 07:41:19 -08001385 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001386 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 04:53:07 -08001387 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
1388 video_receive_configs_[i].rtp.rtx_payload_types[payload_type] =
sprangce4aef12015-11-02 07:23:20 -08001389 kSendRtxPayloadType;
minyue626bc952016-10-31 05:47:02 -07001390 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 16:25:16 +01001391 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik00d802b2017-04-11 10:34:31 -07001392 // Enable RTT calculation so NTP time estimator will work.
1393 video_receive_configs_[i].rtp.rtcp_xr.receiver_reference_time_report = true;
ilnik9fd9f6c2017-03-02 08:10:10 -08001394 // Force fake decoders on non-selected simulcast streams.
sprang1168fd42017-06-21 09:00:17 -07001395 if (!decode_all_receive_streams && i != params_.ss.selected_stream) {
ilnik9fd9f6c2017-03-02 08:10:10 -08001396 VideoReceiveStream::Decoder decoder;
1397 decoder.decoder = new test::FakeDecoder();
1398 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1399 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1400 video_receive_configs_[i].decoders.clear();
1401 allocated_decoders_.emplace_back(decoder.decoder);
1402 video_receive_configs_[i].decoders.push_back(decoder);
1403 }
sprangce4aef12015-11-02 07:23:20 -08001404 }
brandtr1293aca2016-11-16 22:47:29 -08001405
1406 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 07:41:19 -08001407 // Override send config constructed by CreateSendConfig.
sprang1168fd42017-06-21 09:00:17 -07001408 if (decode_all_receive_streams) {
1409 for (uint32_t media_ssrc : video_send_config_.rtp.ssrcs) {
1410 video_send_config_.rtp.flexfec.protected_media_ssrcs.push_back(
1411 media_ssrc);
1412 }
1413 } else {
1414 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1415 kVideoSendSsrcs[params_.ss.selected_stream]};
1416 }
brandtr1293aca2016-11-16 22:47:29 -08001417
brandtr8313a6f2017-01-13 07:41:19 -08001418 // The matching receive config is _not_ created by
1419 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1420 // Set up the receive config manually instead.
1421 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 04:17:53 -08001422 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 06:59:19 -08001423 video_send_config_.rtp.flexfec.payload_type;
1424 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-16 22:47:29 -08001425 flexfec_receive_config.protected_media_ssrcs =
1426 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 01:33:54 -08001427 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 06:37:18 -08001428 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1429 if (params_.call.send_side_bwe) {
1430 flexfec_receive_config.rtp_header_extensions.push_back(
1431 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1432 test::kTransportSequenceNumberExtensionId));
1433 } else {
1434 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1435 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1436 }
brandtr1293aca2016-11-16 22:47:29 -08001437 flexfec_receive_configs_.push_back(flexfec_receive_config);
1438 }
1439
1440 if (params_.video.ulpfec) {
1441 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1442 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1443 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1444
sprang1168fd42017-06-21 09:00:17 -07001445 if (decode_all_receive_streams) {
1446 for (auto it = video_receive_configs_.begin();
1447 it != video_receive_configs_.end(); ++it) {
1448 it->rtp.ulpfec.red_payload_type =
1449 video_send_config_.rtp.ulpfec.red_payload_type;
1450 it->rtp.ulpfec.ulpfec_payload_type =
1451 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1452 it->rtp.ulpfec.red_rtx_payload_type =
1453 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1454 }
1455 } else {
1456 video_receive_configs_[params_.ss.selected_stream]
1457 .rtp.ulpfec.red_payload_type =
1458 video_send_config_.rtp.ulpfec.red_payload_type;
1459 video_receive_configs_[params_.ss.selected_stream]
1460 .rtp.ulpfec.ulpfec_payload_type =
1461 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1462 video_receive_configs_[params_.ss.selected_stream]
1463 .rtp.ulpfec.red_rtx_payload_type =
1464 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1465 }
brandtr1293aca2016-11-16 22:47:29 -08001466 }
ivica5d6a06c2015-09-17 05:30:24 -07001467}
1468
ilnika014cc52017-03-07 04:21:04 -08001469void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1470 Transport* recv_transport) {
1471 for (int i = 0; i < params_.num_thumbnails; ++i) {
1472 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1473
1474 // Thumbnails will be send in the other way: from receiver_call to
1475 // sender_call.
1476 VideoSendStream::Config thumbnail_send_config(recv_transport);
1477 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1478 thumbnail_send_config.encoder_settings.encoder =
1479 thumbnail_encoders_.back().get();
1480 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1481 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1482 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1483 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1484 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1485 thumbnail_send_config.rtp.extensions.clear();
1486 if (params_.call.send_side_bwe) {
1487 thumbnail_send_config.rtp.extensions.push_back(
1488 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1489 test::kTransportSequenceNumberExtensionId));
1490 } else {
1491 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1492 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1493 }
1494
1495 VideoEncoderConfig thumbnail_encoder_config;
1496 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1497 thumbnail_send_config.suspend_below_min_bitrate =
1498 params_.video.suspend_below_min_bitrate;
1499 thumbnail_encoder_config.number_of_streams = 1;
1500 thumbnail_encoder_config.max_bitrate_bps = 50000;
ilnik6b826ef2017-06-16 06:53:48 -07001501 if (params_.ss.infer_streams) {
1502 thumbnail_encoder_config.video_stream_factory =
1503 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1504 } else {
1505 thumbnail_encoder_config.video_stream_factory =
1506 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1507 params_.video.codec, params_.ss.streams[0].max_qp,
1508 params_.video.fps, params_.screenshare.enabled, true);
1509 }
ilnika014cc52017-03-07 04:21:04 -08001510 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1511
1512 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1513 thumbnail_receive_config.rtp.remb = false;
1514 thumbnail_receive_config.rtp.transport_cc = true;
1515 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1516 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1517 thumbnail_receive_config.rtp.extensions.push_back(extension);
1518 thumbnail_receive_config.renderer = &fake_renderer_;
1519
1520 VideoReceiveStream::Decoder decoder =
1521 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1522 allocated_decoders_.push_back(
1523 std::unique_ptr<VideoDecoder>(decoder.decoder));
1524 thumbnail_receive_config.decoders.clear();
1525 thumbnail_receive_config.decoders.push_back(decoder);
1526 thumbnail_receive_config.rtp.remote_ssrc =
1527 thumbnail_send_config.rtp.ssrcs[0];
1528
1529 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1530 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
1531 thumbnail_receive_config.rtp.rtx_payload_types[kPayloadTypeVP8] =
1532 kSendRtxPayloadType;
1533 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1534 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1535
1536 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1537 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1538 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1539 }
1540
1541 for (int i = 0; i < params_.num_thumbnails; ++i) {
1542 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1543 thumbnail_send_configs_[i].Copy(),
1544 thumbnail_encoder_configs_[i].Copy()));
1545 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1546 thumbnail_receive_configs_[i].Copy()));
1547 }
1548}
1549
1550void VideoQualityTest::DestroyThumbnailStreams() {
1551 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1552 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1553 thumbnail_send_streams_.clear();
1554 for (VideoReceiveStream* thumbnail_receive_stream :
1555 thumbnail_receive_streams_)
1556 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1557 thumbnail_send_streams_.clear();
1558 thumbnail_receive_streams_.clear();
1559}
1560
ilnik2a8c2f52017-02-15 02:23:28 -08001561void VideoQualityTest::SetupScreenshareOrSVC() {
1562 if (params_.screenshare.enabled) {
1563 // Fill out codec settings.
1564 video_encoder_config_.content_type =
1565 VideoEncoderConfig::ContentType::kScreen;
1566 degradation_preference_ =
1567 VideoSendStream::DegradationPreference::kMaintainResolution;
1568 if (params_.video.codec == "VP8") {
1569 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1570 vp8_settings.denoisingOn = false;
1571 vp8_settings.frameDroppingOn = false;
1572 vp8_settings.numberOfTemporalLayers =
1573 static_cast<unsigned char>(params_.video.num_temporal_layers);
1574 video_encoder_config_.encoder_specific_settings =
1575 new rtc::RefCountedObject<
1576 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1577 } else if (params_.video.codec == "VP9") {
1578 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1579 vp9_settings.denoisingOn = false;
1580 vp9_settings.frameDroppingOn = false;
1581 vp9_settings.numberOfTemporalLayers =
1582 static_cast<unsigned char>(params_.video.num_temporal_layers);
1583 vp9_settings.numberOfSpatialLayers =
1584 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1585 video_encoder_config_.encoder_specific_settings =
1586 new rtc::RefCountedObject<
1587 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1588 }
1589 // Setup frame generator.
1590 const size_t kWidth = 1850;
1591 const size_t kHeight = 1110;
ilnik8d8185c2017-04-12 04:52:55 -07001592 std::vector<std::string> slides = params_.screenshare.slides;
1593 if (slides.size() == 0) {
1594 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1595 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1596 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1597 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1598 }
ilnik2a8c2f52017-02-15 02:23:28 -08001599 if (params_.screenshare.scroll_duration == 0) {
1600 // Cycle image every slide_change_interval seconds.
perkja8ba1952017-02-27 06:52:10 -08001601 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
ilnik2a8c2f52017-02-15 02:23:28 -08001602 slides, kWidth, kHeight,
perkja8ba1952017-02-27 06:52:10 -08001603 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 02:23:28 -08001604 } else {
1605 RTC_CHECK_LE(params_.video.width, kWidth);
1606 RTC_CHECK_LE(params_.video.height, kHeight);
1607 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1608 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1609 params_.screenshare.scroll_duration) *
1610 1000;
1611 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1612 params_.screenshare.slide_change_interval);
1613
perkja8ba1952017-02-27 06:52:10 -08001614 frame_generator_ = test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1615 clock_, slides, kWidth, kHeight, params_.video.width,
1616 params_.video.height, params_.screenshare.scroll_duration * 1000,
1617 kPauseDurationMs);
ilnik2a8c2f52017-02-15 02:23:28 -08001618 }
1619 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1620 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 03:52:02 -07001621 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 03:52:02 -07001622 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 05:47:02 -07001623 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001624 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001625 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001626 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1627 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001628 }
ivica5d6a06c2015-09-17 05:30:24 -07001629}
1630
ilnika014cc52017-03-07 04:21:04 -08001631void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1632 VideoStream thumbnail = DefaultThumbnailStream();
1633 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1634 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1635 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1636 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 06:15:27 -08001637 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 04:21:04 -08001638 }
1639}
1640
perkja49cbd32016-09-16 07:53:41 -07001641void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001642 if (params_.screenshare.enabled) {
1643 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 06:52:10 -08001644 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 05:47:02 -07001645 params_.video.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001646 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 05:59:29 -07001647 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001648 } else {
ilnik6b826ef2017-06-16 06:53:48 -07001649 if (params_.video.clip_name == "Generator") {
1650 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
1651 static_cast<int>(params_.video.width),
1652 static_cast<int>(params_.video.height), params_.video.fps, clock_));
1653 } else if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 05:59:29 -07001654 video_capturer_.reset(test::VcmCapturer::Create(
Tarun Chawla8e857d12017-05-31 19:20:57 +05301655 params_.video.width, params_.video.height, params_.video.fps,
1656 params_.video.capture_device_index));
sprang1bed2e42017-01-23 08:46:51 -08001657 if (!video_capturer_) {
1658 // Failed to get actual camera, use chroma generator as backup.
1659 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 06:52:10 -08001660 static_cast<int>(params_.video.width),
1661 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 08:46:51 -08001662 }
ivica5d6a06c2015-09-17 05:30:24 -07001663 } else {
minyuea27172d2016-11-01 05:59:29 -07001664 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001665 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 05:47:02 -07001666 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001667 clock_));
minyuea27172d2016-11-01 05:59:29 -07001668 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1669 << params_.video.clip_name
1670 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001671 }
1672 }
sprang1bed2e42017-01-23 08:46:51 -08001673 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001674}
1675
sprang7a975f72015-10-12 06:33:21 -07001676void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001677 params_ = params;
1678
minyue626bc952016-10-31 05:47:02 -07001679 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001680 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1681 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001682 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001683
1684 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001685 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001686 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001687 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001688 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001689 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1690 << "!";
ivica87f83a92015-10-08 05:13:32 -07001691 }
sprang7a975f72015-10-12 06:33:21 -07001692
philipel4fb651d2017-04-10 03:54:05 -07001693 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001694 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001695 CreateCalls(call_config, call_config);
1696
ilnik68af10d2017-03-02 04:59:33 -08001697 test::LayerFilteringTransport send_transport(
1698 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue20c84cc2017-04-10 16:57:57 -07001699 params_.video.selected_tl, params_.ss.selected_sl, payload_type_map_);
1700
1701 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get(),
1702 payload_type_map_);
stefanf116bd02015-10-27 08:29:42 -07001703
sprangce4aef12015-11-02 07:23:20 -08001704 std::string graph_title = params_.analyzer.graph_title;
1705 if (graph_title.empty())
1706 graph_title = VideoQualityTest::GenerateGraphTitle();
1707
sprangc1b57a12017-02-28 08:50:47 -08001708 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
ivica5d6a06c2015-09-17 05:30:24 -07001709 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001710 &send_transport, params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 02:23:28 -08001711 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 00:53:12 -08001712 is_quick_test_enabled
1713 ? kFramesSentInQuickTest
1714 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 07:23:20 -08001715 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 04:58:53 -08001716 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 09:16:05 -08001717 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 09:23:30 -08001718 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik6b826ef2017-06-16 06:53:48 -07001719 params_.video.selected_tl, is_quick_test_enabled, clock_);
ivica5d6a06c2015-09-17 05:30:24 -07001720 analyzer.SetReceiver(receiver_call_->Receiver());
1721 send_transport.SetReceiver(&analyzer);
1722 recv_transport.SetReceiver(sender_call_->Receiver());
1723
minyuea27172d2016-11-01 05:59:29 -07001724 SetupVideo(&analyzer, &recv_transport);
ilnika014cc52017-03-07 04:21:04 -08001725 SetupThumbnails(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001726 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001727 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
Peter Boströme4499152016-02-05 11:13:28 +01001728 RTC_DCHECK(!video_send_config_.post_encode_callback);
1729 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001730
ilnik2a8c2f52017-02-15 02:23:28 -08001731 SetupScreenshareOrSVC();
ivica5d6a06c2015-09-17 05:30:24 -07001732
brandtr1293aca2016-11-16 22:47:29 -08001733 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001734 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001735 analyzer.SetSendStream(video_send_stream_);
philipelfd870db2017-01-23 03:22:15 -08001736 if (video_receive_streams_.size() == 1)
1737 analyzer.SetReceiveStream(video_receive_streams_[0]);
kthelgason2bc68642017-02-07 07:02:22 -08001738
1739 video_send_stream_->SetSource(analyzer.OutputInterface(),
1740 degradation_preference_);
ivica5d6a06c2015-09-17 05:30:24 -07001741
ilnika014cc52017-03-07 04:21:04 -08001742 SetupThumbnailCapturers(params_.num_thumbnails);
1743 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1744 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1745 degradation_preference_);
1746 }
1747
perkja49cbd32016-09-16 07:53:41 -07001748 CreateCapturer();
ilnika014cc52017-03-07 04:21:04 -08001749
ilnik6b826ef2017-06-16 06:53:48 -07001750 analyzer.SetSource(video_capturer_.get(), params_.ss.infer_streams);
ivicac1cc8542015-10-08 03:44:06 -07001751
palmkviste75f2042016-09-28 06:19:48 -07001752 StartEncodedFrameLogs(video_send_stream_);
1753 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 03:14:00 -08001754 video_send_stream_->Start();
ilnika014cc52017-03-07 04:21:04 -08001755 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1756 thumbnail_send_stream->Start();
stefanff483612015-12-21 03:14:00 -08001757 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1758 receive_stream->Start();
brandtr1293aca2016-11-16 22:47:29 -08001759 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1760 receive_stream->Start();
ilnika014cc52017-03-07 04:21:04 -08001761 for (VideoReceiveStream* thumbnail_receive_stream :
1762 thumbnail_receive_streams_)
1763 thumbnail_receive_stream->Start();
1764
ilnikdf92c5c2017-02-23 02:08:44 -08001765 analyzer.StartMeasuringCpuProcessTime();
ilnika014cc52017-03-07 04:21:04 -08001766
minyuea27172d2016-11-01 05:59:29 -07001767 video_capturer_->Start();
ilnika014cc52017-03-07 04:21:04 -08001768 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1769 thumbnail_capturers_) {
1770 video_caputurer->Start();
1771 }
ivica5d6a06c2015-09-17 05:30:24 -07001772
1773 analyzer.Wait();
1774
1775 send_transport.StopSending();
1776 recv_transport.StopSending();
1777
ilnika014cc52017-03-07 04:21:04 -08001778 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1779 thumbnail_capturers_)
1780 video_caputurer->Stop();
minyuea27172d2016-11-01 05:59:29 -07001781 video_capturer_->Stop();
ilnika014cc52017-03-07 04:21:04 -08001782 for (VideoReceiveStream* thumbnail_receive_stream :
1783 thumbnail_receive_streams_)
1784 thumbnail_receive_stream->Stop();
brandtr1293aca2016-11-16 22:47:29 -08001785 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1786 receive_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001787 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1788 receive_stream->Stop();
ilnika014cc52017-03-07 04:21:04 -08001789 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1790 thumbnail_send_stream->Stop();
stefanff483612015-12-21 03:14:00 -08001791 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001792
1793 DestroyStreams();
ilnika014cc52017-03-07 04:21:04 -08001794 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 05:30:24 -07001795
1796 if (graph_data_output_file)
1797 fclose(graph_data_output_file);
1798}
1799
minyuea27172d2016-11-01 05:59:29 -07001800void VideoQualityTest::SetupAudio(int send_channel_id,
1801 int receive_channel_id,
minyuea27172d2016-11-01 05:59:29 -07001802 Transport* transport,
1803 AudioReceiveStream** audio_receive_stream) {
1804 audio_send_config_ = AudioSendStream::Config(transport);
1805 audio_send_config_.voe_channel_id = send_channel_id;
1806 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1807
1808 // Add extension to enable audio send side BWE, and allow audio bit rate
1809 // adaptation.
1810 audio_send_config_.rtp.extensions.clear();
1811 if (params_.call.send_side_bwe) {
1812 audio_send_config_.rtp.extensions.push_back(
1813 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1814 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 09:29:22 -08001815 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1816 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 05:59:29 -07001817 }
ossu20a4b3f2017-04-27 02:08:52 -07001818 audio_send_config_.send_codec_spec =
1819 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
1820 {kAudioSendPayloadType,
1821 {"OPUS", 48000, 2,
1822 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
1823 {"stereo", "1"}}}});
minyue48368ad2017-05-10 04:06:11 -07001824 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 09:00:17 -07001825 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 05:59:29 -07001826
1827 AudioReceiveStream::Config audio_config;
1828 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1829 audio_config.rtcp_send_transport = transport;
1830 audio_config.voe_channel_id = receive_channel_id;
1831 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1832 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1833 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1834 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 16:57:57 -07001835 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
minyuea27172d2016-11-01 05:59:29 -07001836 if (params_.video.enabled && params_.audio.sync_video)
1837 audio_config.sync_group = kSyncGroup;
1838
sprang1168fd42017-06-21 09:00:17 -07001839 *audio_receive_stream =
1840 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 05:59:29 -07001841}
1842
minyue73208662016-08-18 06:28:55 -07001843void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001844 params_ = params;
1845 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001846
ivica5d6a06c2015-09-17 05:30:24 -07001847 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1848 // match the full stack tests.
philipel4fb651d2017-04-10 03:54:05 -07001849 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 05:47:02 -07001850 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001851
1852 ::VoiceEngineState voe;
peaha9cc40b2017-06-29 08:32:09 -07001853 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
1854 webrtc::AudioProcessing::Create());
1855
minyue626bc952016-10-31 05:47:02 -07001856 if (params_.audio.enabled) {
peaha9cc40b2017-06-29 08:32:09 -07001857 CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
minyue73208662016-08-18 06:28:55 -07001858 AudioState::Config audio_state_config;
1859 audio_state_config.voice_engine = voe.voice_engine;
aleloi10111bc2016-11-17 06:48:48 -08001860 audio_state_config.audio_mixer = AudioMixerImpl::Create();
peaha9cc40b2017-06-29 08:32:09 -07001861 audio_state_config.audio_processing = audio_processing;
minyue73208662016-08-18 06:28:55 -07001862 call_config.audio_state = AudioState::Create(audio_state_config);
1863 }
1864
sprang1168fd42017-06-21 09:00:17 -07001865 CreateCalls(call_config, call_config);
ivica5d6a06c2015-09-17 05:30:24 -07001866
minyuea27172d2016-11-01 05:59:29 -07001867 // TODO(minyue): consider if this is a good transport even for audio only
1868 // calls.
sprang1168fd42017-06-21 09:00:17 -07001869 test::LayerFilteringTransport send_transport(
1870 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue20c84cc2017-04-10 16:57:57 -07001871 params.video.selected_tl, params_.ss.selected_sl, payload_type_map_);
1872
sprang1168fd42017-06-21 09:00:17 -07001873 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get(),
1874 payload_type_map_);
1875
ivica5d6a06c2015-09-17 05:30:24 -07001876 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1877 // least share as much code as possible. That way this test would also match
1878 // the full stack tests better.
sprang1168fd42017-06-21 09:00:17 -07001879 send_transport.SetReceiver(receiver_call_->Receiver());
1880 recv_transport.SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 05:30:24 -07001881
minyuea27172d2016-11-01 05:59:29 -07001882 std::unique_ptr<test::VideoRenderer> local_preview;
sprang1168fd42017-06-21 09:00:17 -07001883 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers_;
minyuea27172d2016-11-01 05:59:29 -07001884 if (params_.video.enabled) {
1885 // Create video renderers.
1886 local_preview.reset(test::VideoRenderer::Create(
1887 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 05:13:32 -07001888
sprang1168fd42017-06-21 09:00:17 -07001889 const size_t selected_stream_id = params_.ss.selected_stream;
1890 const size_t num_streams = params_.ss.streams.size();
1891
1892 if (selected_stream_id == num_streams) {
1893 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
1894 std::ostringstream oss;
1895 oss << "Loopback Video - Stream #" << static_cast<int>(stream_id);
1896 loopback_renderers_.emplace_back(test::VideoRenderer::Create(
1897 oss.str().c_str(), params_.ss.streams[stream_id].width,
1898 params_.ss.streams[stream_id].height));
1899 }
1900 } else {
1901 loopback_renderers_.emplace_back(test::VideoRenderer::Create(
1902 "Loopback Video", params_.ss.streams[selected_stream_id].width,
1903 params_.ss.streams[selected_stream_id].height));
minyuea27172d2016-11-01 05:59:29 -07001904 }
sprangce4aef12015-11-02 07:23:20 -08001905
sprang1168fd42017-06-21 09:00:17 -07001906 SetupVideo(&send_transport, &recv_transport);
mflodman48a4beb2016-07-01 13:03:59 +02001907
minyuea27172d2016-11-01 05:59:29 -07001908 video_send_config_.pre_encode_callback = local_preview.get();
sprang1168fd42017-06-21 09:00:17 -07001909 if (selected_stream_id == num_streams) {
1910 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
1911 video_receive_configs_[stream_id].renderer =
1912 loopback_renderers_[stream_id].get();
1913 if (params_.audio.enabled && params_.audio.sync_video)
1914 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1915 }
1916 } else {
1917 video_receive_configs_[selected_stream_id].renderer =
1918 loopback_renderers_.back().get();
1919 if (params_.audio.enabled && params_.audio.sync_video)
1920 video_receive_configs_[selected_stream_id].sync_group = kSyncGroup;
1921 }
minyuea27172d2016-11-01 05:59:29 -07001922
ilnik68af10d2017-03-02 04:59:33 -08001923 if (params_.screenshare.enabled)
1924 SetupScreenshareOrSVC();
minyuea27172d2016-11-01 05:59:29 -07001925
sprang1168fd42017-06-21 09:00:17 -07001926 CreateFlexfecStreams();
1927 CreateVideoStreams();
1928
minyuea27172d2016-11-01 05:59:29 -07001929 CreateCapturer();
kthelgason2bc68642017-02-07 07:02:22 -08001930 video_send_stream_->SetSource(video_capturer_.get(),
1931 degradation_preference_);
philipel274c1dc2016-05-04 06:21:01 -07001932 }
1933
minyue73208662016-08-18 06:28:55 -07001934 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 05:47:02 -07001935 if (params_.audio.enabled) {
sprang1168fd42017-06-21 09:00:17 -07001936 SetupAudio(voe.send_channel_id, voe.receive_channel_id, &send_transport,
1937 &audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001938 }
1939
sprang1168fd42017-06-21 09:00:17 -07001940 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1941 StartEncodedFrameLogs(receive_stream);
palmkviste75f2042016-09-28 06:19:48 -07001942 StartEncodedFrameLogs(video_send_stream_);
1943
minyue73208662016-08-18 06:28:55 -07001944 // Start sending and receiving video.
minyuea27172d2016-11-01 05:59:29 -07001945 if (params_.video.enabled) {
brandtrcb8f0452017-06-30 02:34:20 -07001946 for (FlexfecReceiveStream* flexfec_receive_stream :
1947 flexfec_receive_streams_) {
brandtr1293aca2016-11-16 22:47:29 -08001948 flexfec_receive_stream->Start();
brandtrcb8f0452017-06-30 02:34:20 -07001949 }
sprang1168fd42017-06-21 09:00:17 -07001950 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1951 receive_stream->Start();
minyuea27172d2016-11-01 05:59:29 -07001952 video_send_stream_->Start();
1953 video_capturer_->Start();
1954 }
ivica5d6a06c2015-09-17 05:30:24 -07001955
minyue626bc952016-10-31 05:47:02 -07001956 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001957 // Start receiving audio.
1958 audio_receive_stream->Start();
1959 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 06:28:55 -07001960
1961 // Start sending audio.
1962 audio_send_stream_->Start();
1963 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1964 }
1965
ivica5d6a06c2015-09-17 05:30:24 -07001966 test::PressEnterToContinue();
1967
minyue626bc952016-10-31 05:47:02 -07001968 if (params_.audio.enabled) {
minyue73208662016-08-18 06:28:55 -07001969 // Stop sending audio.
1970 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1971 audio_send_stream_->Stop();
1972
1973 // Stop receiving audio.
minyue73208662016-08-18 06:28:55 -07001974 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1975 audio_receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07001976 sender_call_->DestroyAudioSendStream(audio_send_stream_);
1977 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001978 }
1979
1980 // Stop receiving and sending video.
minyuea27172d2016-11-01 05:59:29 -07001981 if (params_.video.enabled) {
1982 video_capturer_->Stop();
1983 video_send_stream_->Stop();
brandtrcb8f0452017-06-30 02:34:20 -07001984 for (FlexfecReceiveStream* flexfec_receive_stream :
1985 flexfec_receive_streams_) {
brandtr1293aca2016-11-16 22:47:29 -08001986 flexfec_receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07001987 receiver_call_->DestroyFlexfecReceiveStream(flexfec_receive_stream);
brandtr1293aca2016-11-16 22:47:29 -08001988 }
brandtrcb8f0452017-06-30 02:34:20 -07001989 for (VideoReceiveStream* receive_stream : video_receive_streams_) {
1990 receive_stream->Stop();
sprang1168fd42017-06-21 09:00:17 -07001991 receiver_call_->DestroyVideoReceiveStream(receive_stream);
brandtrcb8f0452017-06-30 02:34:20 -07001992 }
sprang1168fd42017-06-21 09:00:17 -07001993 sender_call_->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 06:28:55 -07001994 }
1995
sprang1168fd42017-06-21 09:00:17 -07001996 send_transport.StopSending();
1997 recv_transport.StopSending();
1998
minyue626bc952016-10-31 05:47:02 -07001999 if (params_.audio.enabled)
minyue73208662016-08-18 06:28:55 -07002000 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07002001}
2002
palmkviste75f2042016-09-28 06:19:48 -07002003void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 05:47:02 -07002004 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002005 std::ostringstream str;
2006 str << send_logs_++;
2007 std::string prefix =
minyue626bc952016-10-31 05:47:02 -07002008 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 06:19:48 -07002009 stream->EnableEncodedFrameRecording(
2010 std::vector<rtc::PlatformFile>(
2011 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2012 rtc::CreatePlatformFile(prefix + "2.ivf"),
2013 rtc::CreatePlatformFile(prefix + "3.ivf")}),
2014 10000000);
2015 }
2016}
ilnikcb8c1462017-03-09 09:23:30 -08002017
palmkviste75f2042016-09-28 06:19:48 -07002018void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 05:47:02 -07002019 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 06:19:48 -07002020 std::ostringstream str;
2021 str << receive_logs_++;
2022 std::string path =
minyue626bc952016-10-31 05:47:02 -07002023 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 06:19:48 -07002024 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
2025 10000000);
2026 }
2027}
ivica5d6a06c2015-09-17 05:30:24 -07002028} // namespace webrtc