blob: 2526ac25a67ba3858edd9353f833b19e48c7e02e [file] [log] [blame]
ivica5d6a06c2015-09-17 12:30:241/*
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 */
Mirko Bonadei92ea95e2017-09-15 04:47:3110#include "video/video_quality_test.h"
perkj9fdbda62016-09-15 16:19:2011
perkja49cbd32016-09-16 14:53:4112#include <stdio.h>
ivica5d6a06c2015-09-17 12:30:2413#include <algorithm>
14#include <deque>
15#include <map>
ilnikcb8c1462017-03-09 17:23:3016#include <set>
sprangce4aef12015-11-02 15:23:2017#include <sstream>
mflodmand1590b22015-12-09 15:07:5918#include <string>
ivica5d6a06c2015-09-17 12:30:2419#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "api/optional.h"
22#include "call/call.h"
23#include "common_video/libyuv/include/webrtc_libyuv.h"
24#include "logging/rtc_event_log/rtc_event_log.h"
25#include "media/engine/webrtcvideoengine.h"
26#include "modules/audio_mixer/audio_mixer_impl.h"
27#include "modules/rtp_rtcp/include/rtp_header_parser.h"
28#include "modules/rtp_rtcp/source/rtp_format.h"
29#include "modules/rtp_rtcp/source/rtp_utility.h"
30#include "modules/video_coding/codecs/h264/include/h264.h"
31#include "modules/video_coding/codecs/vp8/include/vp8.h"
32#include "modules/video_coding/codecs/vp8/include/vp8_common_types.h"
33#include "modules/video_coding/codecs/vp9/include/vp9.h"
34#include "rtc_base/checks.h"
35#include "rtc_base/cpu_time.h"
36#include "rtc_base/event.h"
37#include "rtc_base/flags.h"
38#include "rtc_base/format_macros.h"
39#include "rtc_base/logging.h"
40#include "rtc_base/memory_usage.h"
41#include "rtc_base/pathutils.h"
42#include "rtc_base/platform_file.h"
43#include "rtc_base/ptr_util.h"
44#include "rtc_base/timeutils.h"
45#include "system_wrappers/include/cpu_info.h"
46#include "system_wrappers/include/field_trial.h"
47#include "test/gtest.h"
48#include "test/layer_filtering_transport.h"
49#include "test/run_loop.h"
50#include "test/statistics.h"
51#include "test/testsupport/fileutils.h"
52#include "test/testsupport/frame_writer.h"
Edward Lemuraf8659a2017-09-27 12:46:2453#include "test/testsupport/test_artifacts.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3154#include "test/vcm_capturer.h"
55#include "test/video_renderer.h"
56#include "voice_engine/include/voe_base.h"
minyue73208662016-08-18 13:28:5557
Mirko Bonadei92ea95e2017-09-15 04:47:3158#include "test/rtp_file_writer.h"
ilnik98436952017-07-13 07:47:0359
ilnikee42d192017-08-22 14:16:2060DEFINE_bool(save_worst_frame,
61 false,
62 "Enable saving a frame with the lowest PSNR to a jpeg file in the "
Edward Lemuraf8659a2017-09-27 12:46:2463 "test_artifacts_dir");
ilnikee42d192017-08-22 14:16:2064
minyue73208662016-08-18 13:28:5565namespace {
66
67constexpr int kSendStatsPollingIntervalMs = 1000;
minyue20c84cc2017-04-10 23:57:5768
minyue73208662016-08-18 13:28:5569constexpr size_t kMaxComparisons = 10;
70constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 17:29:2271constexpr int kOpusMinBitrateBps = 6000;
72constexpr int kOpusBitrateFbBps = 32000;
ilnik9ae0d762017-02-15 08:53:1273constexpr int kFramesSentInQuickTest = 1;
ilnika014cc52017-03-07 12:21:0474constexpr uint32_t kThumbnailSendSsrcStart = 0xE0000;
75constexpr uint32_t kThumbnailRtxSsrcStart = 0xF0000;
minyue73208662016-08-18 13:28:5576
sprang1168fd42017-06-21 16:00:1777constexpr int kDefaultMaxQp = cricket::WebRtcVideoChannel::kDefaultQpMax;
78
minyue73208662016-08-18 13:28:5579struct VoiceEngineState {
80 VoiceEngineState()
81 : voice_engine(nullptr),
82 base(nullptr),
minyue73208662016-08-18 13:28:5583 send_channel_id(-1),
84 receive_channel_id(-1) {}
85
86 webrtc::VoiceEngine* voice_engine;
87 webrtc::VoEBase* base;
minyue73208662016-08-18 13:28:5588 int send_channel_id;
89 int receive_channel_id;
90};
91
peaha9cc40b2017-06-29 15:32:0992void CreateVoiceEngine(
93 VoiceEngineState* voe,
94 webrtc::AudioProcessing* apm,
95 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
minyue73208662016-08-18 13:28:5596 voe->voice_engine = webrtc::VoiceEngine::Create();
97 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
peaha9cc40b2017-06-29 15:32:0998 EXPECT_EQ(0, voe->base->Init(nullptr, apm, decoder_factory));
solenberg88499ec2016-09-07 14:34:4199 webrtc::VoEBase::ChannelConfig config;
100 config.enable_voice_pacing = true;
101 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 13:28:55102 EXPECT_GE(voe->send_channel_id, 0);
103 voe->receive_channel_id = voe->base->CreateChannel();
104 EXPECT_GE(voe->receive_channel_id, 0);
105}
106
107void DestroyVoiceEngine(VoiceEngineState* voe) {
108 voe->base->DeleteChannel(voe->send_channel_id);
109 voe->send_channel_id = -1;
110 voe->base->DeleteChannel(voe->receive_channel_id);
111 voe->receive_channel_id = -1;
112 voe->base->Release();
113 voe->base = nullptr;
minyue73208662016-08-18 13:28:55114
115 webrtc::VoiceEngine::Delete(voe->voice_engine);
116 voe->voice_engine = nullptr;
117}
118
perkjfa10b552016-10-03 06:45:26119class VideoStreamFactory
120 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
121 public:
122 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
123 : streams_(streams) {}
124
125 private:
126 std::vector<webrtc::VideoStream> CreateEncoderStreams(
127 int width,
128 int height,
129 const webrtc::VideoEncoderConfig& encoder_config) override {
mflodmand79f97b2016-12-15 15:24:33130 // The highest layer must match the incoming resolution.
131 std::vector<webrtc::VideoStream> streams = streams_;
132 streams[streams_.size() - 1].height = height;
133 streams[streams_.size() - 1].width = width;
134 return streams;
perkjfa10b552016-10-03 06:45:26135 }
136
137 std::vector<webrtc::VideoStream> streams_;
138};
139
brandtr504b95e2016-12-21 10:54:35140bool IsFlexfec(int payload_type) {
141 return payload_type == webrtc::VideoQualityTest::kFlexfecPayloadType;
142}
143
minyue73208662016-08-18 13:28:55144} // namespace
ivica5d6a06c2015-09-17 12:30:24145
146namespace webrtc {
147
ivica5d6a06c2015-09-17 12:30:24148class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 16:59:31149 public Transport,
ilnik1e7732c2017-02-23 13:07:56150 public rtc::VideoSinkInterface<VideoFrame> {
ivica5d6a06c2015-09-17 12:30:24151 public:
sprangce4aef12015-11-02 15:23:20152 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 12:30:24153 const std::string& test_label,
154 double avg_psnr_threshold,
155 double avg_ssim_threshold,
156 int duration_frames,
sprangce4aef12015-11-02 15:23:20157 FILE* graph_data_output_file,
158 const std::string& graph_title,
ilnik3dd5ad92017-02-09 12:58:53159 uint32_t ssrc_to_analyze,
ilnik46a00212017-02-10 17:16:05160 uint32_t rtx_ssrc_to_analyze,
ilnikcb8c1462017-03-09 17:23:30161 size_t selected_stream,
ilnik1e7732c2017-02-23 13:07:56162 int selected_sl,
163 int selected_tl,
ilnik6b826ef2017-06-16 13:53:48164 bool is_quick_test_enabled,
ilnik98436952017-07-13 07:47:03165 Clock* clock,
166 std::string rtp_dump_name)
perkja49cbd32016-09-16 14:53:41167 : transport_(transport),
ivica5d6a06c2015-09-17 12:30:24168 receiver_(nullptr),
stefan889d9652017-07-05 10:03:02169 call_(nullptr),
ivica5d6a06c2015-09-17 12:30:24170 send_stream_(nullptr),
philipelfd870db2017-01-23 11:22:15171 receive_stream_(nullptr),
ilnik6b826ef2017-06-16 13:53:48172 captured_frame_forwarder_(this, clock),
ivica5d6a06c2015-09-17 12:30:24173 test_label_(test_label),
174 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 15:23:20175 graph_title_(graph_title),
176 ssrc_to_analyze_(ssrc_to_analyze),
ilnik46a00212017-02-10 17:16:05177 rtx_ssrc_to_analyze_(rtx_ssrc_to_analyze),
ilnikcb8c1462017-03-09 17:23:30178 selected_stream_(selected_stream),
ilnik1e7732c2017-02-23 13:07:56179 selected_sl_(selected_sl),
180 selected_tl_(selected_tl),
pbos14fe7082016-04-20 13:35:56181 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 10:13:28182 encode_timing_proxy_(this),
stefan889d9652017-07-05 10:03:02183 last_fec_bytes_(0),
ivica5d6a06c2015-09-17 12:30:24184 frames_to_process_(duration_frames),
185 frames_recorded_(0),
186 frames_processed_(0),
187 dropped_frames_(0),
pbos14fe7082016-04-20 13:35:56188 dropped_frames_before_first_encode_(0),
189 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 12:30:24190 last_render_time_(0),
191 rtp_timestamp_delta_(0),
ilnik1e7732c2017-02-23 13:07:56192 total_media_bytes_(0),
193 first_sending_time_(0),
194 last_sending_time_(0),
ilnikdf92c5c2017-02-23 10:08:44195 cpu_time_(0),
196 wallclock_time_(0),
ivica5d6a06c2015-09-17 12:30:24197 avg_psnr_threshold_(avg_psnr_threshold),
198 avg_ssim_threshold_(avg_ssim_threshold),
ilnik9ae0d762017-02-15 08:53:12199 is_quick_test_enabled_(is_quick_test_enabled),
Peter Boström8c38e8b2015-11-26 16:45:47200 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 12:02:50201 comparison_available_event_(false, false),
ilnik98436952017-07-13 07:47:03202 done_(true, false),
203 clock_(clock),
204 start_ms_(clock->TimeInMilliseconds()) {
ivica5d6a06c2015-09-17 12:30:24205 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
206
207 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
208 // so that we don't accidentally starve "real" worker threads (codec etc).
209 // Also, don't allocate more than kMaxComparisonThreads, even if there are
210 // spare cores.
211
212 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
kwibergaf476c72016-11-28 23:21:39213 RTC_DCHECK_GE(num_cores, 1);
ivica5d6a06c2015-09-17 12:30:24214 static const uint32_t kMinCoresLeft = 4;
215 static const uint32_t kMaxComparisonThreads = 8;
216
217 if (num_cores <= kMinCoresLeft) {
218 num_cores = 1;
219 } else {
220 num_cores -= kMinCoresLeft;
221 num_cores = std::min(num_cores, kMaxComparisonThreads);
222 }
223
224 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 16:45:47225 rtc::PlatformThread* thread =
226 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
227 thread->Start();
228 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 12:30:24229 }
ilnik98436952017-07-13 07:47:03230
231 if (!rtp_dump_name.empty()) {
232 fprintf(stdout, "Writing rtp dump to %s\n", rtp_dump_name.c_str());
233 rtp_file_writer_.reset(test::RtpFileWriter::Create(
234 test::RtpFileWriter::kRtpDump, rtp_dump_name));
235 }
ivica5d6a06c2015-09-17 12:30:24236 }
237
238 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 16:45:47239 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
240 thread->Stop();
ivica5d6a06c2015-09-17 12:30:24241 delete thread;
242 }
243 }
244
245 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
246
ilnik6b826ef2017-06-16 13:53:48247 void SetSource(test::VideoCapturer* video_capturer, bool respect_sink_wants) {
248 if (respect_sink_wants)
249 captured_frame_forwarder_.SetSource(video_capturer);
250 rtc::VideoSinkWants wants;
251 video_capturer->AddOrUpdateSink(InputInterface(), wants);
252 }
253
stefan889d9652017-07-05 10:03:02254 void SetCall(Call* call) {
255 rtc::CritScope lock(&crit_);
256 RTC_DCHECK(!call_);
257 call_ = call;
258 }
259
perkja49cbd32016-09-16 14:53:41260 void SetSendStream(VideoSendStream* stream) {
261 rtc::CritScope lock(&crit_);
262 RTC_DCHECK(!send_stream_);
263 send_stream_ = stream;
264 }
265
philipelfd870db2017-01-23 11:22:15266 void SetReceiveStream(VideoReceiveStream* stream) {
267 rtc::CritScope lock(&crit_);
268 RTC_DCHECK(!receive_stream_);
269 receive_stream_ = stream;
270 }
271
perkja49cbd32016-09-16 14:53:41272 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
273 return &captured_frame_forwarder_;
274 }
275 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
276 return &captured_frame_forwarder_;
277 }
278
ivica5d6a06c2015-09-17 12:30:24279 DeliveryStatus DeliverPacket(MediaType media_type,
280 const uint8_t* packet,
281 size_t length,
282 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 18:41:03283 // Ignore timestamps of RTCP packets. They're not synchronized with
284 // RTP packet timestamps and so they would confuse wrap_handler_.
285 if (RtpHeaderParser::IsRtcp(packet, length)) {
286 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
287 }
ilnik98436952017-07-13 07:47:03288
289 if (rtp_file_writer_) {
290 test::RtpPacket p;
291 memcpy(p.data, packet, length);
292 p.length = length;
293 p.original_length = length;
294 p.time_ms = clock_->TimeInMilliseconds() - start_ms_;
295 rtp_file_writer_->WritePacket(&p);
296 }
297
sprangce4aef12015-11-02 15:23:20298 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 12:30:24299 RTPHeader header;
danilchapf6975f42015-12-28 18:18:46300 parser.Parse(&header);
ilnik46a00212017-02-10 17:16:05301 if (!IsFlexfec(header.payloadType) &&
302 (header.ssrc == ssrc_to_analyze_ ||
303 header.ssrc == rtx_ssrc_to_analyze_)) {
brandtr504b95e2016-12-21 10:54:35304 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
305 // (FlexFEC and media are sent on different SSRCs, which have different
306 // timestamps spaces.)
ilnik46a00212017-02-10 17:16:05307 // Also ignore packets from wrong SSRC, but include retransmits.
ivica5d6a06c2015-09-17 12:30:24308 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 09:30:24309 int64_t timestamp =
310 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
311 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 12:30:24312 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
313 }
314
315 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
316 }
317
Peter Boströme4499152016-02-05 10:13:28318 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 09:43:12319 rtc::CritScope crit(&comparison_lock_);
320 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
321 }
322
pbos14fe7082016-04-20 13:35:56323 void PreEncodeOnFrame(const VideoFrame& video_frame) {
324 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 12:58:53325 if (!first_encoded_timestamp_) {
pbos14fe7082016-04-20 13:35:56326 while (frames_.front().timestamp() != video_frame.timestamp()) {
327 ++dropped_frames_before_first_encode_;
328 frames_.pop_front();
329 RTC_CHECK(!frames_.empty());
330 }
ilnik3dd5ad92017-02-09 12:58:53331 first_encoded_timestamp_ =
332 rtc::Optional<uint32_t>(video_frame.timestamp());
333 }
334 }
335
336 void PostEncodeFrameCallback(const EncodedFrame& encoded_frame) {
337 rtc::CritScope lock(&crit_);
338 if (!first_sent_timestamp_ &&
ilnikcb8c1462017-03-09 17:23:30339 encoded_frame.stream_id_ == selected_stream_) {
ilnik3dd5ad92017-02-09 12:58:53340 first_sent_timestamp_ = rtc::Optional<uint32_t>(encoded_frame.timestamp_);
pbos14fe7082016-04-20 13:35:56341 }
342 }
343
stefan1d8a5062015-10-02 10:39:33344 bool SendRtp(const uint8_t* packet,
345 size_t length,
346 const PacketOptions& options) override {
sprangce4aef12015-11-02 15:23:20347 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 12:30:24348 RTPHeader header;
danilchapf6975f42015-12-28 18:18:46349 parser.Parse(&header);
ivica5d6a06c2015-09-17 12:30:24350
sprangce4aef12015-11-02 15:23:20351 int64_t current_time =
352 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ilnik2a8c2f52017-02-15 10:23:28353
sprangce4aef12015-11-02 15:23:20354 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 12:30:24355 {
356 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 12:58:53357 if (rtp_timestamp_delta_ == 0 && header.ssrc == ssrc_to_analyze_) {
ilnik1e1c84d2017-02-09 16:32:53358 RTC_CHECK(static_cast<bool>(first_sent_timestamp_));
ilnik3dd5ad92017-02-09 12:58:53359 rtp_timestamp_delta_ = header.timestamp - *first_sent_timestamp_;
ilnike67c59e2017-02-09 12:08:56360 }
ilnik3dd5ad92017-02-09 12:58:53361
362 if (!IsFlexfec(header.payloadType) && header.ssrc == ssrc_to_analyze_) {
brandtr504b95e2016-12-21 10:54:35363 // Ignore FlexFEC timestamps, to avoid collisions with media timestamps.
364 // (FlexFEC and media are sent on different SSRCs, which have different
365 // timestamps spaces.)
ilnik46a00212017-02-10 17:16:05366 // Also ignore packets from wrong SSRC and retransmits.
brandtr504b95e2016-12-21 10:54:35367 int64_t timestamp =
368 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
369 send_times_[timestamp] = current_time;
ilnik1e7732c2017-02-23 13:07:56370
371 if (IsInSelectedSpatialAndTemporalLayer(packet, length, header)) {
brandtr504b95e2016-12-21 10:54:35372 encoded_frame_sizes_[timestamp] +=
373 length - (header.headerLength + header.paddingLength);
ilnik1e7732c2017-02-23 13:07:56374 total_media_bytes_ +=
375 length - (header.headerLength + header.paddingLength);
brandtr504b95e2016-12-21 10:54:35376 }
ilnik1e7732c2017-02-23 13:07:56377 if (first_sending_time_ == 0)
378 first_sending_time_ = current_time;
379 last_sending_time_ = current_time;
sprangce4aef12015-11-02 15:23:20380 }
ivica5d6a06c2015-09-17 12:30:24381 }
sprangce4aef12015-11-02 15:23:20382 return result;
ivica5d6a06c2015-09-17 12:30:24383 }
384
385 bool SendRtcp(const uint8_t* packet, size_t length) override {
386 return transport_->SendRtcp(packet, length);
387 }
388
nisseeb83a1a2016-03-21 08:27:56389 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 12:30:24390 int64_t render_time_ms =
391 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 12:30:24392
393 rtc::CritScope lock(&crit_);
ilnikdf92c5c2017-02-23 10:08:44394
395 StartExcludingCpuThreadTime();
396
Taylor Brandstetter433b95a2016-03-18 18:41:03397 int64_t send_timestamp =
sprang16daaa52016-03-09 09:30:24398 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 12:30:24399
sprang16daaa52016-03-09 09:30:24400 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 16:44:40401 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 13:35:56402 // No previous frame rendered, this one was dropped after sending but
403 // before rendering.
404 ++dropped_frames_before_rendering_;
kthelgason2bc68642017-02-07 15:02:22405 } else {
406 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
407 render_time_ms);
pbos14fe7082016-04-20 13:35:56408 }
ivica5d6a06c2015-09-17 12:30:24409 frames_.pop_front();
pbos14fe7082016-04-20 13:35:56410 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 12:30:24411 }
412
413 VideoFrame reference_frame = frames_.front();
414 frames_.pop_front();
sprang16daaa52016-03-09 09:30:24415 int64_t reference_timestamp =
416 wrap_handler_.Unwrap(reference_frame.timestamp());
417 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 15:23:20418 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 10:13:28419 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 15:23:20420 ++send_timestamp;
421 }
sprang16daaa52016-03-09 09:30:24422 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 12:30:24423
424 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
425
nisse97f0b932016-05-26 16:44:40426 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ilnikdf92c5c2017-02-23 10:08:44427
428 StopExcludingCpuThreadTime();
ivica5d6a06c2015-09-17 12:30:24429 }
430
ivica5d6a06c2015-09-17 12:30:24431 void Wait() {
432 // Frame comparisons can be very expensive. Wait for test to be done, but
433 // at time-out check if frames_processed is going up. If so, give it more
434 // time, otherwise fail. Hopefully this will reduce test flakiness.
435
Peter Boström8c38e8b2015-11-26 16:45:47436 stats_polling_thread_.Start();
sprangce4aef12015-11-02 15:23:20437
ivica5d6a06c2015-09-17 12:30:24438 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 12:30:24439 int iteration = 0;
Peter Boström5811a392015-12-10 12:02:50440 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 12:30:24441 int frames_processed;
442 {
443 rtc::CritScope crit(&comparison_lock_);
444 frames_processed = frames_processed_;
445 }
446
447 // Print some output so test infrastructure won't think we've crashed.
448 const char* kKeepAliveMessages[3] = {
449 "Uh, I'm-I'm not quite dead, sir.",
450 "Uh, I-I think uh, I could pull through, sir.",
451 "Actually, I think I'm all right to come with you--"};
452 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
453
454 if (last_frames_processed == -1) {
455 last_frames_processed = frames_processed;
456 continue;
457 }
Peter Boströmdd45eb62016-01-19 14:22:32458 if (frames_processed == last_frames_processed) {
459 EXPECT_GT(frames_processed, last_frames_processed)
460 << "Analyzer stalled while waiting for test to finish.";
461 done_.Set();
462 break;
463 }
ivica5d6a06c2015-09-17 12:30:24464 last_frames_processed = frames_processed;
465 }
466
467 if (iteration > 0)
468 printf("- Farewell, sweet Concorde!\n");
469
Peter Boström8c38e8b2015-11-26 16:45:47470 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 12:30:24471 }
472
pbos14fe7082016-04-20 13:35:56473 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
474 return &pre_encode_proxy_;
475 }
Peter Boströme4499152016-02-05 10:13:28476 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
477
ilnikdf92c5c2017-02-23 10:08:44478 void StartMeasuringCpuProcessTime() {
479 rtc::CritScope lock(&cpu_measurement_lock_);
480 cpu_time_ -= rtc::GetProcessCpuTimeNanos();
481 wallclock_time_ -= rtc::SystemTimeNanos();
482 }
483
484 void StopMeasuringCpuProcessTime() {
485 rtc::CritScope lock(&cpu_measurement_lock_);
486 cpu_time_ += rtc::GetProcessCpuTimeNanos();
487 wallclock_time_ += rtc::SystemTimeNanos();
488 }
489
490 void StartExcludingCpuThreadTime() {
491 rtc::CritScope lock(&cpu_measurement_lock_);
492 cpu_time_ += rtc::GetThreadCpuTimeNanos();
493 }
494
495 void StopExcludingCpuThreadTime() {
496 rtc::CritScope lock(&cpu_measurement_lock_);
497 cpu_time_ -= rtc::GetThreadCpuTimeNanos();
498 }
499
500 double GetCpuUsagePercent() {
501 rtc::CritScope lock(&cpu_measurement_lock_);
502 return static_cast<double>(cpu_time_) / wallclock_time_ * 100.0;
503 }
504
sprangce4aef12015-11-02 15:23:20505 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 12:30:24506 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 12:30:24507
508 private:
509 struct FrameComparison {
510 FrameComparison()
511 : dropped(false),
nissedf2ceb82016-12-15 14:29:53512 input_time_ms(0),
ivica5d6a06c2015-09-17 12:30:24513 send_time_ms(0),
514 recv_time_ms(0),
515 render_time_ms(0),
516 encoded_frame_size(0) {}
517
518 FrameComparison(const VideoFrame& reference,
519 const VideoFrame& render,
520 bool dropped,
nissedf2ceb82016-12-15 14:29:53521 int64_t input_time_ms,
ivica5d6a06c2015-09-17 12:30:24522 int64_t send_time_ms,
523 int64_t recv_time_ms,
524 int64_t render_time_ms,
525 size_t encoded_frame_size)
526 : reference(reference),
527 render(render),
528 dropped(dropped),
nissedf2ceb82016-12-15 14:29:53529 input_time_ms(input_time_ms),
ivica5d6a06c2015-09-17 12:30:24530 send_time_ms(send_time_ms),
531 recv_time_ms(recv_time_ms),
532 render_time_ms(render_time_ms),
533 encoded_frame_size(encoded_frame_size) {}
534
nissedf2ceb82016-12-15 14:29:53535 FrameComparison(bool dropped,
536 int64_t input_time_ms,
537 int64_t send_time_ms,
538 int64_t recv_time_ms,
539 int64_t render_time_ms,
540 size_t encoded_frame_size)
541 : dropped(dropped),
542 input_time_ms(input_time_ms),
543 send_time_ms(send_time_ms),
544 recv_time_ms(recv_time_ms),
545 render_time_ms(render_time_ms),
546 encoded_frame_size(encoded_frame_size) {}
547
548 rtc::Optional<VideoFrame> reference;
549 rtc::Optional<VideoFrame> render;
ivica5d6a06c2015-09-17 12:30:24550 bool dropped;
nissedf2ceb82016-12-15 14:29:53551 int64_t input_time_ms;
ivica5d6a06c2015-09-17 12:30:24552 int64_t send_time_ms;
553 int64_t recv_time_ms;
554 int64_t render_time_ms;
555 size_t encoded_frame_size;
556 };
557
558 struct Sample {
ivica8d15bd62015-10-07 09:43:12559 Sample(int dropped,
560 int64_t input_time_ms,
561 int64_t send_time_ms,
562 int64_t recv_time_ms,
563 int64_t render_time_ms,
564 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 12:30:24565 double psnr,
ivica8d15bd62015-10-07 09:43:12566 double ssim)
ivica5d6a06c2015-09-17 12:30:24567 : dropped(dropped),
568 input_time_ms(input_time_ms),
569 send_time_ms(send_time_ms),
570 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 09:43:12571 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 12:30:24572 encoded_frame_size(encoded_frame_size),
573 psnr(psnr),
ivica8d15bd62015-10-07 09:43:12574 ssim(ssim) {}
ivica5d6a06c2015-09-17 12:30:24575
ivica8d15bd62015-10-07 09:43:12576 int dropped;
577 int64_t input_time_ms;
578 int64_t send_time_ms;
579 int64_t recv_time_ms;
580 int64_t render_time_ms;
581 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 12:30:24582 double psnr;
583 double ssim;
ivica5d6a06c2015-09-17 12:30:24584 };
585
Peter Boströme4499152016-02-05 10:13:28586 // This class receives the send-side OnEncodeTiming and is provided to not
587 // conflict with the receiver-side pre_decode_callback.
588 class OnEncodeTimingProxy : public EncodedFrameObserver {
589 public:
590 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
591
592 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
593 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
594 }
ilnik3dd5ad92017-02-09 12:58:53595 void EncodedFrameCallback(const EncodedFrame& frame) override {
596 parent_->PostEncodeFrameCallback(frame);
597 }
Peter Boströme4499152016-02-05 10:13:28598
599 private:
600 VideoAnalyzer* const parent_;
601 };
602
pbos14fe7082016-04-20 13:35:56603 // This class receives the send-side OnFrame callback and is provided to not
604 // conflict with the receiver-side renderer callback.
605 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
606 public:
607 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
608
609 void OnFrame(const VideoFrame& video_frame) override {
610 parent_->PreEncodeOnFrame(video_frame);
611 }
612
613 private:
614 VideoAnalyzer* const parent_;
615 };
616
ilnik1e7732c2017-02-23 13:07:56617 bool IsInSelectedSpatialAndTemporalLayer(const uint8_t* packet,
618 size_t length,
619 const RTPHeader& header) {
ilnik863f03b2017-07-11 09:38:36620 if (header.payloadType != test::CallTest::kPayloadTypeVP9 &&
621 header.payloadType != test::CallTest::kPayloadTypeVP8) {
ilnik1e7732c2017-02-23 13:07:56622 return true;
623 } else {
624 // Get VP8 and VP9 specific header to check layers indexes.
625 const uint8_t* payload = packet + header.headerLength;
626 const size_t payload_length = length - header.headerLength;
627 const size_t payload_data_length = payload_length - header.paddingLength;
ilnik863f03b2017-07-11 09:38:36628 const bool is_vp8 = header.payloadType == test::CallTest::kPayloadTypeVP8;
ilnik1e7732c2017-02-23 13:07:56629 std::unique_ptr<RtpDepacketizer> depacketizer(
630 RtpDepacketizer::Create(is_vp8 ? kRtpVideoVp8 : kRtpVideoVp9));
631 RtpDepacketizer::ParsedPayload parsed_payload;
632 bool result =
633 depacketizer->Parse(&parsed_payload, payload, payload_data_length);
634 RTC_DCHECK(result);
635 const int temporal_idx = static_cast<int>(
636 is_vp8 ? parsed_payload.type.Video.codecHeader.VP8.temporalIdx
637 : parsed_payload.type.Video.codecHeader.VP9.temporal_idx);
638 const int spatial_idx = static_cast<int>(
639 is_vp8 ? kNoSpatialIdx
640 : parsed_payload.type.Video.codecHeader.VP9.spatial_idx);
641 return (selected_tl_ < 0 || temporal_idx == kNoTemporalIdx ||
642 temporal_idx <= selected_tl_) &&
643 (selected_sl_ < 0 || spatial_idx == kNoSpatialIdx ||
644 spatial_idx <= selected_sl_);
645 }
646 }
647
ivica5d6a06c2015-09-17 12:30:24648 void AddFrameComparison(const VideoFrame& reference,
649 const VideoFrame& render,
650 bool dropped,
651 int64_t render_time_ms)
danilchapa37de392017-09-09 11:17:22652 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 10:04:52653 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
654 int64_t send_time_ms = send_times_[reference_timestamp];
655 send_times_.erase(reference_timestamp);
656 int64_t recv_time_ms = recv_times_[reference_timestamp];
657 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 12:30:24658
sprangce4aef12015-11-02 15:23:20659 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 10:04:52660 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 15:23:20661 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 10:04:52662 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 15:23:20663 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
664 if (it != encoded_frame_sizes_.end())
665 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 12:30:24666
ivica5d6a06c2015-09-17 12:30:24667 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 14:00:57668 if (comparisons_.size() < kMaxComparisons) {
nissedf2ceb82016-12-15 14:29:53669 comparisons_.push_back(FrameComparison(reference, render, dropped,
670 reference.ntp_time_ms(),
671 send_time_ms, recv_time_ms,
672 render_time_ms, encoded_size));
stefanb1797672016-08-11 14:00:57673 } else {
nissedf2ceb82016-12-15 14:29:53674 comparisons_.push_back(FrameComparison(dropped,
675 reference.ntp_time_ms(),
676 send_time_ms, recv_time_ms,
677 render_time_ms, encoded_size));
stefanb1797672016-08-11 14:00:57678 }
Peter Boström5811a392015-12-10 12:02:50679 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24680 }
681
tommi0f8b4032017-02-22 19:22:05682 static void PollStatsThread(void* obj) {
683 static_cast<VideoAnalyzer*>(obj)->PollStats();
ivica5d6a06c2015-09-17 12:30:24684 }
685
tommi0f8b4032017-02-22 19:22:05686 void PollStats() {
687 while (!done_.Wait(kSendStatsPollingIntervalMs)) {
688 rtc::CritScope crit(&comparison_lock_);
ivica5d6a06c2015-09-17 12:30:24689
stefan889d9652017-07-05 10:03:02690 Call::Stats call_stats = call_->GetStats();
691 send_bandwidth_bps_.AddSample(call_stats.send_bandwidth_bps);
692
tommi0f8b4032017-02-22 19:22:05693 VideoSendStream::Stats send_stats = send_stream_->GetStats();
694 // It's not certain that we yet have estimates for any of these stats.
695 // Check that they are positive before mixing them in.
696 if (send_stats.encode_frame_rate > 0)
697 encode_frame_rate_.AddSample(send_stats.encode_frame_rate);
698 if (send_stats.avg_encode_time_ms > 0)
699 encode_time_ms_.AddSample(send_stats.avg_encode_time_ms);
700 if (send_stats.encode_usage_percent > 0)
701 encode_usage_percent_.AddSample(send_stats.encode_usage_percent);
702 if (send_stats.media_bitrate_bps > 0)
703 media_bitrate_bps_.AddSample(send_stats.media_bitrate_bps);
stefan889d9652017-07-05 10:03:02704 size_t fec_bytes = 0;
705 for (auto kv : send_stats.substreams) {
706 fec_bytes += kv.second.rtp_stats.fec.payload_bytes +
707 kv.second.rtp_stats.fec.padding_bytes;
708 }
709 fec_bitrate_bps_.AddSample((fec_bytes - last_fec_bytes_) * 8);
710 last_fec_bytes_ = fec_bytes;
philipelfd870db2017-01-23 11:22:15711
tommi0f8b4032017-02-22 19:22:05712 if (receive_stream_ != nullptr) {
713 VideoReceiveStream::Stats receive_stats = receive_stream_->GetStats();
714 if (receive_stats.decode_ms > 0)
715 decode_time_ms_.AddSample(receive_stats.decode_ms);
716 if (receive_stats.max_decode_ms > 0)
717 decode_time_max_ms_.AddSample(receive_stats.max_decode_ms);
718 }
ilnikdaa574d2017-03-01 14:46:05719
720 memory_usage_.AddSample(rtc::GetProcessResidentSizeBytes());
philipelfd870db2017-01-23 11:22:15721 }
ivica5d6a06c2015-09-17 12:30:24722 }
723
724 static bool FrameComparisonThread(void* obj) {
725 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
726 }
727
728 bool CompareFrames() {
729 if (AllFramesRecorded())
730 return false;
731
ivica5d6a06c2015-09-17 12:30:24732 FrameComparison comparison;
733
734 if (!PopComparison(&comparison)) {
735 // Wait until new comparison task is available, or test is done.
736 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 12:02:50737 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 12:30:24738 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 12:02:50739 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24740 return false;
741 }
742 return true; // Try again.
743 }
744
ilnikdf92c5c2017-02-23 10:08:44745 StartExcludingCpuThreadTime();
746
ivica5d6a06c2015-09-17 12:30:24747 PerformFrameComparison(comparison);
748
ilnikdf92c5c2017-02-23 10:08:44749 StopExcludingCpuThreadTime();
750
ivica5d6a06c2015-09-17 12:30:24751 if (FrameProcessed()) {
752 PrintResults();
753 if (graph_data_output_file_)
754 PrintSamplesToFile();
Peter Boström5811a392015-12-10 12:02:50755 done_.Set();
756 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24757 return false;
758 }
759
760 return true;
761 }
762
763 bool PopComparison(FrameComparison* comparison) {
764 rtc::CritScope crit(&comparison_lock_);
765 // If AllFramesRecorded() is true, it means we have already popped
766 // frames_to_process_ frames from comparisons_, so there is no more work
767 // for this thread to be done. frames_processed_ might still be lower if
768 // all comparisons are not done, but those frames are currently being
769 // worked on by other threads.
770 if (comparisons_.empty() || AllFramesRecorded())
771 return false;
772
773 *comparison = comparisons_.front();
774 comparisons_.pop_front();
775
776 FrameRecorded();
777 return true;
778 }
779
780 // Increment counter for number of frames received for comparison.
781 void FrameRecorded() {
782 rtc::CritScope crit(&comparison_lock_);
783 ++frames_recorded_;
784 }
785
786 // Returns true if all frames to be compared have been taken from the queue.
787 bool AllFramesRecorded() {
788 rtc::CritScope crit(&comparison_lock_);
789 assert(frames_recorded_ <= frames_to_process_);
790 return frames_recorded_ == frames_to_process_;
791 }
792
793 // Increase count of number of frames processed. Returns true if this was the
794 // last frame to be processed.
795 bool FrameProcessed() {
796 rtc::CritScope crit(&comparison_lock_);
797 ++frames_processed_;
798 assert(frames_processed_ <= frames_to_process_);
799 return frames_processed_ == frames_to_process_;
800 }
801
802 void PrintResults() {
ilnikdf92c5c2017-02-23 10:08:44803 StopMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 12:30:24804 rtc::CritScope crit(&comparison_lock_);
805 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 19:21:51806 PrintResult("ssim", ssim_, " score");
stefan2da7a242017-03-30 08:02:15807 PrintResult("sender_time", sender_time_, " ms");
808 PrintResult("receiver_time", receiver_time_, " ms");
ivica5d6a06c2015-09-17 12:30:24809 PrintResult("total_delay_incl_network", end_to_end_, " ms");
810 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
ivica5d6a06c2015-09-17 12:30:24811 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
philipelfd870db2017-01-23 11:22:15812 PrintResult("encode_time", encode_time_ms_, " ms");
philipelfd870db2017-01-23 11:22:15813 PrintResult("media_bitrate", media_bitrate_bps_, " bps");
stefan889d9652017-07-05 10:03:02814 PrintResult("fec_bitrate", fec_bitrate_bps_, " bps");
815 PrintResult("send_bandwidth", send_bandwidth_bps_, " bps");
philipelfd870db2017-01-23 11:22:15816
ilnik59cac992017-07-25 12:45:03817 if (worst_frame_) {
818 printf("RESULT min_psnr: %s = %lf dB\n", test_label_.c_str(),
819 worst_frame_->psnr);
820 }
821
philipelfd870db2017-01-23 11:22:15822 if (receive_stream_ != nullptr) {
823 PrintResult("decode_time", decode_time_ms_, " ms");
philipelfd870db2017-01-23 11:22:15824 }
ivica5d6a06c2015-09-17 12:30:24825
pbos14fe7082016-04-20 13:35:56826 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
827 dropped_frames_);
ilnikdf92c5c2017-02-23 10:08:44828 printf("RESULT cpu_usage: %s = %lf %%\n", test_label_.c_str(),
829 GetCpuUsagePercent());
ilnikdaa574d2017-03-01 14:46:05830
831#if defined(WEBRTC_WIN)
832 // On Linux and Mac in Resident Set some unused pages may be counted.
833 // Therefore this metric will depend on order in which tests are run and
834 // will be flaky.
835 PrintResult("memory_usage", memory_usage_, " bytes");
836#endif
ilnik59cac992017-07-25 12:45:03837
ilnikee42d192017-08-22 14:16:20838 // Saving only the worst frame for manual analysis. Intention here is to
839 // only detect video corruptions and not to track picture quality. Thus,
840 // jpeg is used here.
oprypin9b2f20c2017-08-29 12:51:57841 if (FLAG_save_worst_frame && worst_frame_) {
ilnikee42d192017-08-22 14:16:20842 std::string output_dir;
Edward Lemuraf8659a2017-09-27 12:46:24843 test::GetTestArtifactsDir(&output_dir);
ilnikee42d192017-08-22 14:16:20844 std::string output_path =
845 rtc::Pathname(output_dir, test_label_ + ".jpg").pathname();
846 LOG(LS_INFO) << "Saving worst frame to " << output_path;
847 test::JpegFrameWriter frame_writer(output_path);
848 RTC_CHECK(frame_writer.WriteFrame(worst_frame_->frame,
849 100 /*best quality*/));
ilnik59cac992017-07-25 12:45:03850 }
ilnikdaa574d2017-03-01 14:46:05851
ilnik9ae0d762017-02-15 08:53:12852 // Disable quality check for quick test, as quality checks may fail
853 // because too few samples were collected.
854 if (!is_quick_test_enabled_) {
855 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
856 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
857 }
ivica5d6a06c2015-09-17 12:30:24858 }
859
860 void PerformFrameComparison(const FrameComparison& comparison) {
861 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 14:00:57862 double psnr = -1.0;
863 double ssim = -1.0;
ilnik6b826ef2017-06-16 13:53:48864 if (comparison.reference && !comparison.dropped) {
nissedf2ceb82016-12-15 14:29:53865 psnr = I420PSNR(&*comparison.reference, &*comparison.render);
866 ssim = I420SSIM(&*comparison.reference, &*comparison.render);
stefanb1797672016-08-11 14:00:57867 }
ivica5d6a06c2015-09-17 12:30:24868
ivica5d6a06c2015-09-17 12:30:24869 rtc::CritScope crit(&comparison_lock_);
ilnik59cac992017-07-25 12:45:03870
871 if (psnr >= 0.0 && (!worst_frame_ || worst_frame_->psnr > psnr)) {
872 worst_frame_.emplace(FrameWithPsnr{psnr, *comparison.render});
873 }
874
ivica5d6a06c2015-09-17 12:30:24875 if (graph_data_output_file_) {
nissedf2ceb82016-12-15 14:29:53876 samples_.push_back(Sample(
877 comparison.dropped, comparison.input_time_ms, comparison.send_time_ms,
878 comparison.recv_time_ms, comparison.render_time_ms,
879 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 12:30:24880 }
stefanb1797672016-08-11 14:00:57881 if (psnr >= 0.0)
882 psnr_.AddSample(psnr);
883 if (ssim >= 0.0)
884 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 12:30:24885
886 if (comparison.dropped) {
887 ++dropped_frames_;
888 return;
889 }
890 if (last_render_time_ != 0)
891 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
892 last_render_time_ = comparison.render_time_ms;
893
nissedf2ceb82016-12-15 14:29:53894 sender_time_.AddSample(comparison.send_time_ms - comparison.input_time_ms);
brandtr504b95e2016-12-21 10:54:35895 if (comparison.recv_time_ms > 0) {
896 // If recv_time_ms == 0, this frame consisted of a packets which were all
897 // lost in the transport. Since we were able to render the frame, however,
898 // the dropped packets were recovered by FlexFEC. The FlexFEC recovery
899 // happens internally in Call, and we can therefore here not know which
900 // FEC packets that protected the lost media packets. Consequently, we
901 // were not able to record a meaningful recv_time_ms. We therefore skip
902 // this sample.
903 //
904 // The reasoning above does not hold for ULPFEC and RTX, as for those
905 // strategies the timestamp of the received packets is set to the
906 // timestamp of the protected/retransmitted media packet. I.e., then
907 // recv_time_ms != 0, even though the media packets were lost.
908 receiver_time_.AddSample(comparison.render_time_ms -
909 comparison.recv_time_ms);
910 }
nissedf2ceb82016-12-15 14:29:53911 end_to_end_.AddSample(comparison.render_time_ms - comparison.input_time_ms);
ivica5d6a06c2015-09-17 12:30:24912 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
913 }
914
915 void PrintResult(const char* result_type,
916 test::Statistics stats,
917 const char* unit) {
918 printf("RESULT %s: %s = {%f, %f}%s\n",
919 result_type,
920 test_label_.c_str(),
921 stats.Mean(),
922 stats.StandardDeviation(),
923 unit);
924 }
925
926 void PrintSamplesToFile(void) {
927 FILE* out = graph_data_output_file_;
928 rtc::CritScope crit(&comparison_lock_);
929 std::sort(samples_.begin(), samples_.end(),
930 [](const Sample& A, const Sample& B) -> bool {
931 return A.input_time_ms < B.input_time_ms;
932 });
933
sprangce4aef12015-11-02 15:23:20934 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 12:30:24935 fprintf(out, "%" PRIuS "\n", samples_.size());
936 fprintf(out,
937 "dropped "
938 "input_time_ms "
939 "send_time_ms "
940 "recv_time_ms "
ivica8d15bd62015-10-07 09:43:12941 "render_time_ms "
ivica5d6a06c2015-09-17 12:30:24942 "encoded_frame_size "
943 "psnr "
944 "ssim "
ivica8d15bd62015-10-07 09:43:12945 "encode_time_ms\n");
946 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 12:30:24947 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 09:43:12948 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
949 int encode_time_ms;
950 if (it != samples_encode_time_ms_.end()) {
951 encode_time_ms = it->second;
952 } else {
953 ++missing_encode_time_samples;
954 encode_time_ms = -1;
955 }
956 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
957 " %lf %lf %d\n",
958 sample.dropped, sample.input_time_ms, sample.send_time_ms,
959 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 12:30:24960 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 09:43:12961 encode_time_ms);
962 }
963 if (missing_encode_time_samples) {
964 fprintf(stderr,
965 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
966 missing_encode_time_samples);
ivica5d6a06c2015-09-17 12:30:24967 }
968 }
969
ilnik1e7732c2017-02-23 13:07:56970 double GetAverageMediaBitrateBps() {
971 if (last_sending_time_ == first_sending_time_) {
972 return 0;
973 } else {
974 return static_cast<double>(total_media_bytes_) * 8 /
975 (last_sending_time_ - first_sending_time_) *
976 rtc::kNumMillisecsPerSec;
977 }
978 }
979
perkja49cbd32016-09-16 14:53:41980 // Implements VideoSinkInterface to receive captured frames from a
981 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
982 // as a source to VideoSendStream.
983 // It forwards all input frames to the VideoAnalyzer for later comparison and
984 // forwards the captured frames to the VideoSendStream.
985 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
986 public rtc::VideoSourceInterface<VideoFrame> {
987 public:
ilnik6b826ef2017-06-16 13:53:48988 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer, Clock* clock)
989 : analyzer_(analyzer),
990 send_stream_input_(nullptr),
991 video_capturer_(nullptr),
992 clock_(clock) {}
993
994 void SetSource(test::VideoCapturer* video_capturer) {
995 video_capturer_ = video_capturer;
996 }
perkja49cbd32016-09-16 14:53:41997
998 private:
999 void OnFrame(const VideoFrame& video_frame) override {
1000 VideoFrame copy = video_frame;
ilnik3dd5ad92017-02-09 12:58:531001 // Frames from the capturer does not have a rtp timestamp.
1002 // Create one so it can be used for comparison.
1003 RTC_DCHECK_EQ(0, video_frame.timestamp());
ilnik6b826ef2017-06-16 13:53:481004 if (video_frame.ntp_time_ms() == 0)
1005 copy.set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
perkja49cbd32016-09-16 14:53:411006 copy.set_timestamp(copy.ntp_time_ms() * 90);
ilnik3dd5ad92017-02-09 12:58:531007 analyzer_->AddCapturedFrameForComparison(copy);
perkja49cbd32016-09-16 14:53:411008 rtc::CritScope lock(&crit_);
1009 if (send_stream_input_)
ilnikb82ac6a2017-05-02 07:48:411010 send_stream_input_->OnFrame(copy);
perkja49cbd32016-09-16 14:53:411011 }
1012
1013 // Called when |send_stream_.SetSource()| is called.
1014 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
1015 const rtc::VideoSinkWants& wants) override {
ilnik267041c2017-06-27 14:21:011016 {
1017 rtc::CritScope lock(&crit_);
1018 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
1019 send_stream_input_ = sink;
1020 }
ilnik6b826ef2017-06-16 13:53:481021 if (video_capturer_) {
1022 video_capturer_->AddOrUpdateSink(this, wants);
1023 }
perkja49cbd32016-09-16 14:53:411024 }
1025
1026 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
1027 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
1028 rtc::CritScope lock(&crit_);
1029 RTC_DCHECK(sink == send_stream_input_);
1030 send_stream_input_ = nullptr;
1031 }
1032
1033 VideoAnalyzer* const analyzer_;
1034 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 11:17:221035 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_
1036 RTC_GUARDED_BY(crit_);
ilnik6b826ef2017-06-16 13:53:481037 test::VideoCapturer* video_capturer_;
1038 Clock* clock_;
perkja49cbd32016-09-16 14:53:411039 };
1040
1041 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
1042 rtc::CritScope lock(&crit_);
ilnik3dd5ad92017-02-09 12:58:531043 frames_.push_back(video_frame);
perkja49cbd32016-09-16 14:53:411044 }
1045
stefan889d9652017-07-05 10:03:021046 Call* call_;
perkja49cbd32016-09-16 14:53:411047 VideoSendStream* send_stream_;
philipelfd870db2017-01-23 11:22:151048 VideoReceiveStream* receive_stream_;
perkja49cbd32016-09-16 14:53:411049 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 12:30:241050 const std::string test_label_;
1051 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 15:23:201052 const std::string graph_title_;
1053 const uint32_t ssrc_to_analyze_;
ilnik46a00212017-02-10 17:16:051054 const uint32_t rtx_ssrc_to_analyze_;
ilnikcb8c1462017-03-09 17:23:301055 const size_t selected_stream_;
ilnik1e7732c2017-02-23 13:07:561056 const int selected_sl_;
1057 const int selected_tl_;
pbos14fe7082016-04-20 13:35:561058 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 10:13:281059 OnEncodeTimingProxy encode_timing_proxy_;
danilchapa37de392017-09-09 11:17:221060 std::vector<Sample> samples_ RTC_GUARDED_BY(comparison_lock_);
1061 std::map<int64_t, int> samples_encode_time_ms_
1062 RTC_GUARDED_BY(comparison_lock_);
1063 test::Statistics sender_time_ RTC_GUARDED_BY(comparison_lock_);
1064 test::Statistics receiver_time_ RTC_GUARDED_BY(comparison_lock_);
1065 test::Statistics psnr_ RTC_GUARDED_BY(comparison_lock_);
1066 test::Statistics ssim_ RTC_GUARDED_BY(comparison_lock_);
1067 test::Statistics end_to_end_ RTC_GUARDED_BY(comparison_lock_);
1068 test::Statistics rendered_delta_ RTC_GUARDED_BY(comparison_lock_);
1069 test::Statistics encoded_frame_size_ RTC_GUARDED_BY(comparison_lock_);
1070 test::Statistics encode_frame_rate_ RTC_GUARDED_BY(comparison_lock_);
1071 test::Statistics encode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1072 test::Statistics encode_usage_percent_ RTC_GUARDED_BY(comparison_lock_);
1073 test::Statistics decode_time_ms_ RTC_GUARDED_BY(comparison_lock_);
1074 test::Statistics decode_time_max_ms_ RTC_GUARDED_BY(comparison_lock_);
1075 test::Statistics media_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1076 test::Statistics fec_bitrate_bps_ RTC_GUARDED_BY(comparison_lock_);
1077 test::Statistics send_bandwidth_bps_ RTC_GUARDED_BY(comparison_lock_);
1078 test::Statistics memory_usage_ RTC_GUARDED_BY(comparison_lock_);
ilnikdaa574d2017-03-01 14:46:051079
ilnik59cac992017-07-25 12:45:031080 struct FrameWithPsnr {
1081 double psnr;
1082 VideoFrame frame;
1083 };
1084
1085 // Rendered frame with worst PSNR is saved for further analysis.
danilchapa37de392017-09-09 11:17:221086 rtc::Optional<FrameWithPsnr> worst_frame_ RTC_GUARDED_BY(comparison_lock_);
ilnik59cac992017-07-25 12:45:031087
stefan889d9652017-07-05 10:03:021088 size_t last_fec_bytes_;
ivica5d6a06c2015-09-17 12:30:241089
1090 const int frames_to_process_;
1091 int frames_recorded_;
1092 int frames_processed_;
1093 int dropped_frames_;
pbos14fe7082016-04-20 13:35:561094 int dropped_frames_before_first_encode_;
1095 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 12:30:241096 int64_t last_render_time_;
1097 uint32_t rtp_timestamp_delta_;
ilnik1e7732c2017-02-23 13:07:561098 int64_t total_media_bytes_;
1099 int64_t first_sending_time_;
1100 int64_t last_sending_time_;
ivica5d6a06c2015-09-17 12:30:241101
danilchapa37de392017-09-09 11:17:221102 int64_t cpu_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
1103 int64_t wallclock_time_ RTC_GUARDED_BY(cpu_measurement_lock_);
ilnikdf92c5c2017-02-23 10:08:441104 rtc::CriticalSection cpu_measurement_lock_;
1105
ivica5d6a06c2015-09-17 12:30:241106 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 11:17:221107 std::deque<VideoFrame> frames_ RTC_GUARDED_BY(crit_);
1108 rtc::Optional<VideoFrame> last_rendered_frame_ RTC_GUARDED_BY(crit_);
1109 rtc::TimestampWrapAroundHandler wrap_handler_ RTC_GUARDED_BY(crit_);
1110 std::map<int64_t, int64_t> send_times_ RTC_GUARDED_BY(crit_);
1111 std::map<int64_t, int64_t> recv_times_ RTC_GUARDED_BY(crit_);
1112 std::map<int64_t, size_t> encoded_frame_sizes_ RTC_GUARDED_BY(crit_);
1113 rtc::Optional<uint32_t> first_encoded_timestamp_ RTC_GUARDED_BY(crit_);
1114 rtc::Optional<uint32_t> first_sent_timestamp_ RTC_GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 12:30:241115 const double avg_psnr_threshold_;
1116 const double avg_ssim_threshold_;
ilnik9ae0d762017-02-15 08:53:121117 bool is_quick_test_enabled_;
ivica5d6a06c2015-09-17 12:30:241118
1119 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 16:45:471120 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
1121 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 12:02:501122 rtc::Event comparison_available_event_;
danilchapa37de392017-09-09 11:17:221123 std::deque<FrameComparison> comparisons_ RTC_GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 12:02:501124 rtc::Event done_;
ilnik98436952017-07-13 07:47:031125
1126 std::unique_ptr<test::RtpFileWriter> rtp_file_writer_;
1127 Clock* const clock_;
1128 const int64_t start_ms_;
ivica5d6a06c2015-09-17 12:30:241129};
1130
Rasmus Brandt31027342017-09-29 13:48:121131class Vp8EncoderFactory : public cricket::WebRtcVideoEncoderFactory {
1132 public:
1133 Vp8EncoderFactory() {
1134 supported_codecs_.push_back(cricket::VideoCodec("VP8"));
1135 }
1136 ~Vp8EncoderFactory() override { RTC_CHECK(live_encoders_.empty()); }
1137
1138 const std::vector<cricket::VideoCodec>& supported_codecs() const override {
1139 return supported_codecs_;
1140 }
1141
1142 VideoEncoder* CreateVideoEncoder(const cricket::VideoCodec& codec) override {
1143 VideoEncoder* encoder = VP8Encoder::Create();
1144 live_encoders_.insert(encoder);
1145 return encoder;
1146 }
1147
1148 void DestroyVideoEncoder(VideoEncoder* encoder) override {
1149 auto it = live_encoders_.find(encoder);
1150 RTC_CHECK(it != live_encoders_.end());
1151 live_encoders_.erase(it);
1152 delete encoder;
1153 }
1154
1155 private:
1156 std::vector<cricket::VideoCodec> supported_codecs_;
1157 std::set<VideoEncoder*> live_encoders_;
1158};
1159
palmkviste75f2042016-09-28 13:19:481160VideoQualityTest::VideoQualityTest()
minyue20c84cc2017-04-10 23:57:571161 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {
1162 payload_type_map_ = test::CallTest::payload_type_map_;
1163 RTC_DCHECK(payload_type_map_.find(kPayloadTypeH264) ==
1164 payload_type_map_.end());
1165 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP8) ==
1166 payload_type_map_.end());
1167 RTC_DCHECK(payload_type_map_.find(kPayloadTypeVP9) ==
1168 payload_type_map_.end());
1169 payload_type_map_[kPayloadTypeH264] = webrtc::MediaType::VIDEO;
1170 payload_type_map_[kPayloadTypeVP8] = webrtc::MediaType::VIDEO;
1171 payload_type_map_[kPayloadTypeVP9] = webrtc::MediaType::VIDEO;
1172}
ivica5d6a06c2015-09-17 12:30:241173
minyue626bc952016-10-31 12:47:021174VideoQualityTest::Params::Params()
ilnik98436952017-07-13 07:47:031175 : call({false, Call::Config::BitrateConfig(), 0}),
Rasmus Brandt31027342017-09-29 13:48:121176 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
1177 false, ""}),
minyue4c8b9422017-03-21 11:11:431178 audio({false, false, false}),
erikvarga579de6f2017-08-29 16:12:571179 screenshare({false, false, 10, 0}),
minyue626bc952016-10-31 12:47:021180 analyzer({"", 0.0, 0.0, 0, "", ""}),
1181 pipe(),
ilnika014cc52017-03-07 12:21:041182 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}),
ilnik98436952017-07-13 07:47:031183 logging({false, "", "", ""}) {}
minyue626bc952016-10-31 12:47:021184
1185VideoQualityTest::Params::~Params() = default;
1186
ivica5d6a06c2015-09-17 12:30:241187void VideoQualityTest::TestBody() {}
1188
sprangce4aef12015-11-02 15:23:201189std::string VideoQualityTest::GenerateGraphTitle() const {
1190 std::stringstream ss;
minyue626bc952016-10-31 12:47:021191 ss << params_.video.codec;
1192 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
1193 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 15:23:201194 if (params_.screenshare.scroll_duration)
1195 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
1196 if (params_.ss.streams.size() > 1)
1197 ss << ", Stream #" << params_.ss.selected_stream;
1198 if (params_.ss.num_spatial_layers > 1)
1199 ss << ", Layer #" << params_.ss.selected_sl;
1200 ss << ")";
1201 return ss.str();
1202}
1203
1204void VideoQualityTest::CheckParams() {
stefan7de8d642017-02-07 15:14:081205 if (!params_.video.enabled)
1206 return;
sprangce4aef12015-11-02 15:23:201207 // Add a default stream in none specified.
1208 if (params_.ss.streams.empty())
1209 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
1210 if (params_.ss.num_spatial_layers == 0)
1211 params_.ss.num_spatial_layers = 1;
1212
1213 if (params_.pipe.loss_percent != 0 ||
1214 params_.pipe.queue_length_packets != 0) {
1215 // Since LayerFilteringTransport changes the sequence numbers, we can't
1216 // use that feature with pack loss, since the NACK request would end up
1217 // retransmitting the wrong packets.
1218 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 14:10:231219 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 12:47:021220 RTC_CHECK(params_.video.selected_tl == -1 ||
1221 params_.video.selected_tl ==
1222 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 15:23:201223 }
1224
1225 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
1226 // does in some parts of the code?
minyue626bc952016-10-31 12:47:021227 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
1228 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
1229 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprang1168fd42017-06-21 16:00:171230 RTC_CHECK_LE(params_.ss.selected_stream, params_.ss.streams.size());
sprangce4aef12015-11-02 15:23:201231 for (const VideoStream& stream : params_.ss.streams) {
1232 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
1233 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
1234 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
sprangce4aef12015-11-02 15:23:201235 }
1236 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
1237 // the total bitrate? We anyway have to update them in the case bitrate
1238 // estimator changes the total bitrates.
1239 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
1240 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
1241 RTC_CHECK(params_.ss.spatial_layers.empty() ||
1242 params_.ss.spatial_layers.size() ==
1243 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 12:47:021244 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 15:23:201245 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 12:47:021246 } else if (params_.video.codec == "VP9") {
kwibergaf476c72016-11-28 23:21:391247 RTC_CHECK_EQ(params_.ss.streams.size(), 1);
sprangce4aef12015-11-02 15:23:201248 }
ilnik98436952017-07-13 07:47:031249 RTC_CHECK_GE(params_.call.num_thumbnails, 0);
1250 if (params_.call.num_thumbnails > 0) {
ilnika014cc52017-03-07 12:21:041251 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
1252 RTC_CHECK_EQ(params_.ss.streams.size(), 3);
1253 RTC_CHECK_EQ(params_.video.num_temporal_layers, 3);
1254 RTC_CHECK_EQ(params_.video.codec, "VP8");
1255 }
sprangce4aef12015-11-02 15:23:201256}
1257
1258// Static.
1259std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
1260 // Parse comma separated nonnegative integers, where some elements may be
1261 // empty. The empty values are replaced with -1.
1262 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
1263 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
1264 std::vector<int> result;
1265 if (str.empty())
1266 return result;
1267
1268 const char* p = str.c_str();
1269 int value = -1;
1270 int pos;
1271 while (*p) {
1272 if (*p == ',') {
1273 result.push_back(value);
1274 value = -1;
1275 ++p;
1276 continue;
1277 }
1278 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
1279 << "Unexpected non-number value.";
1280 p += pos;
1281 }
1282 result.push_back(value);
1283 return result;
1284}
1285
1286// Static.
1287VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
1288 VideoStream stream;
minyue626bc952016-10-31 12:47:021289 stream.width = params.video.width;
1290 stream.height = params.video.height;
1291 stream.max_framerate = params.video.fps;
1292 stream.min_bitrate_bps = params.video.min_bitrate_bps;
1293 stream.target_bitrate_bps = params.video.target_bitrate_bps;
1294 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprang1168fd42017-06-21 16:00:171295 stream.max_qp = kDefaultMaxQp;
sprang6ef1b342017-03-13 09:01:321296 // TODO(sprang): Can we make this less of a hack?
1297 if (params.video.num_temporal_layers == 2) {
1298 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
1299 } else if (params.video.num_temporal_layers == 3) {
1300 stream.temporal_layer_thresholds_bps.push_back(stream.max_bitrate_bps / 4);
1301 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
sprangff19d352017-09-06 14:14:021302 } else {
1303 RTC_CHECK_LE(params.video.num_temporal_layers, kMaxTemporalStreams);
1304 for (int i = 0; i < params.video.num_temporal_layers - 1; ++i) {
1305 stream.temporal_layer_thresholds_bps.push_back(static_cast<int>(
1306 stream.max_bitrate_bps * kVp8LayerRateAlloction[0][i] + 0.5));
1307 }
ilnika014cc52017-03-07 12:21:041308 }
1309 return stream;
1310}
1311
1312// Static.
1313VideoStream VideoQualityTest::DefaultThumbnailStream() {
1314 VideoStream stream;
1315 stream.width = 320;
1316 stream.height = 180;
1317 stream.max_framerate = 7;
1318 stream.min_bitrate_bps = 7500;
1319 stream.target_bitrate_bps = 37500;
1320 stream.max_bitrate_bps = 50000;
sprang1168fd42017-06-21 16:00:171321 stream.max_qp = kDefaultMaxQp;
sprangce4aef12015-11-02 15:23:201322 return stream;
1323}
1324
1325// Static.
1326void VideoQualityTest::FillScalabilitySettings(
1327 Params* params,
1328 const std::vector<std::string>& stream_descriptors,
sprang1168fd42017-06-21 16:00:171329 int num_streams,
sprangce4aef12015-11-02 15:23:201330 size_t selected_stream,
1331 int num_spatial_layers,
1332 int selected_sl,
1333 const std::vector<std::string>& sl_descriptors) {
sprang1168fd42017-06-21 16:00:171334 if (params->ss.streams.empty() && params->ss.infer_streams) {
1335 webrtc::VideoEncoderConfig encoder_config;
1336 encoder_config.content_type =
1337 params->screenshare.enabled
1338 ? webrtc::VideoEncoderConfig::ContentType::kScreen
1339 : webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo;
1340 encoder_config.max_bitrate_bps = params->video.max_bitrate_bps;
1341 encoder_config.min_transmit_bitrate_bps = params->video.min_transmit_bps;
1342 encoder_config.number_of_streams = num_streams;
1343 encoder_config.spatial_layers = params->ss.spatial_layers;
1344 encoder_config.video_stream_factory =
1345 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1346 params->video.codec, kDefaultMaxQp, params->video.fps,
1347 params->screenshare.enabled, true);
1348 params->ss.streams =
1349 encoder_config.video_stream_factory->CreateEncoderStreams(
1350 static_cast<int>(params->video.width),
1351 static_cast<int>(params->video.height), encoder_config);
1352 } else {
1353 // Read VideoStream and SpatialLayer elements from a list of comma separated
1354 // lists. To use a default value for an element, use -1 or leave empty.
1355 // Validity checks performed in CheckParams.
1356 RTC_CHECK(params->ss.streams.empty());
1357 for (auto descriptor : stream_descriptors) {
1358 if (descriptor.empty())
1359 continue;
1360 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
1361 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1362 if (v[0] != -1)
1363 stream.width = static_cast<size_t>(v[0]);
1364 if (v[1] != -1)
1365 stream.height = static_cast<size_t>(v[1]);
1366 if (v[2] != -1)
1367 stream.max_framerate = v[2];
1368 if (v[3] != -1)
1369 stream.min_bitrate_bps = v[3];
1370 if (v[4] != -1)
1371 stream.target_bitrate_bps = v[4];
1372 if (v[5] != -1)
1373 stream.max_bitrate_bps = v[5];
1374 if (v.size() > 6 && v[6] != -1)
1375 stream.max_qp = v[6];
1376 if (v.size() > 7) {
1377 stream.temporal_layer_thresholds_bps.clear();
1378 stream.temporal_layer_thresholds_bps.insert(
1379 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
1380 } else {
1381 // Automatic TL thresholds for more than two layers not supported.
1382 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
1383 }
1384 params->ss.streams.push_back(stream);
sprangce4aef12015-11-02 15:23:201385 }
sprangce4aef12015-11-02 15:23:201386 }
sprangce4aef12015-11-02 15:23:201387
sprang1168fd42017-06-21 16:00:171388 params->ss.num_spatial_layers = std::max(1, num_spatial_layers);
1389 params->ss.selected_stream = selected_stream;
1390
sprangce4aef12015-11-02 15:23:201391 params->ss.selected_sl = selected_sl;
1392 RTC_CHECK(params->ss.spatial_layers.empty());
1393 for (auto descriptor : sl_descriptors) {
1394 if (descriptor.empty())
1395 continue;
1396 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
1397 RTC_CHECK_GT(v[2], 0);
1398
1399 SpatialLayer layer;
1400 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
1401 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
1402 layer.target_bitrate_bps = v[2];
1403 params->ss.spatial_layers.push_back(layer);
1404 }
1405}
1406
minyuea27172d2016-11-01 12:59:291407void VideoQualityTest::SetupVideo(Transport* send_transport,
1408 Transport* recv_transport) {
ilnik98436952017-07-13 07:47:031409 if (params_.logging.logs)
ivica5d6a06c2015-09-17 12:30:241410 trace_to_stderr_.reset(new test::TraceToStderr);
1411
brandtr8313a6f2017-01-13 15:41:191412 size_t num_video_streams = params_.ss.streams.size();
1413 size_t num_flexfec_streams = params_.video.flexfec ? 1 : 0;
1414 CreateSendConfig(num_video_streams, 0, num_flexfec_streams, send_transport);
ivica5d6a06c2015-09-17 12:30:241415
1416 int payload_type;
minyue626bc952016-10-31 12:47:021417 if (params_.video.codec == "H264") {
Rasmus Brandt31027342017-09-29 13:48:121418 video_encoder_.reset(H264Encoder::Create(cricket::VideoCodec("H264")));
hbosbab934b2016-01-27 09:36:031419 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 12:47:021420 } else if (params_.video.codec == "VP8") {
ilnikcb8c1462017-03-09 17:23:301421 if (params_.screenshare.enabled && params_.ss.streams.size() > 1) {
Rasmus Brandt31027342017-09-29 13:48:121422 // Simulcast screenshare needs a simulcast encoder adapter to work, since
1423 // encoders usually can't natively do simulcast with different frame rates
1424 // for the different layers.
ilnikcb8c1462017-03-09 17:23:301425 video_encoder_.reset(
Rasmus Brandt31027342017-09-29 13:48:121426 new SimulcastEncoderAdapter(new Vp8EncoderFactory()));
ilnikcb8c1462017-03-09 17:23:301427 } else {
Rasmus Brandt31027342017-09-29 13:48:121428 video_encoder_.reset(VP8Encoder::Create());
ilnikcb8c1462017-03-09 17:23:301429 }
ivica5d6a06c2015-09-17 12:30:241430 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 12:47:021431 } else if (params_.video.codec == "VP9") {
Rasmus Brandt31027342017-09-29 13:48:121432 video_encoder_.reset(VP9Encoder::Create());
ivica5d6a06c2015-09-17 12:30:241433 payload_type = kPayloadTypeVP9;
1434 } else {
1435 RTC_NOTREACHED() << "Codec not supported!";
1436 return;
1437 }
minyuea27172d2016-11-01 12:59:291438 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 12:47:021439 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 11:14:001440 video_send_config_.encoder_settings.payload_type = payload_type;
1441 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1442 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
brandtr8313a6f2017-01-13 15:41:191443 for (size_t i = 0; i < num_video_streams; ++i)
stefanff483612015-12-21 11:14:001444 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 12:30:241445
stefanff483612015-12-21 11:14:001446 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 12:47:021447 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 11:14:001448 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 18:24:551449 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 14:13:241450 test::kTransportSequenceNumberExtensionId));
1451 } else {
stefanff483612015-12-21 11:14:001452 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 18:24:551453 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik Språng6b8d3552015-09-24 13:06:571454 }
ilnik00d802b2017-04-11 17:34:311455 video_send_config_.rtp.extensions.push_back(RtpExtension(
1456 RtpExtension::kVideoContentTypeUri, test::kVideoContentTypeExtensionId));
ilnik04f4d122017-06-19 14:18:551457 video_send_config_.rtp.extensions.push_back(RtpExtension(
1458 RtpExtension::kVideoTimingUri, test::kVideoTimingExtensionId));
Erik Språng6b8d3552015-09-24 13:06:571459
stefanff483612015-12-21 11:14:001460 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 12:47:021461 params_.video.min_transmit_bps;
perkjfa10b552016-10-03 06:45:261462
brandtr1293aca2016-11-17 06:47:291463 video_send_config_.suspend_below_min_bitrate =
1464 params_.video.suspend_below_min_bitrate;
1465
perkjfa10b552016-10-03 06:45:261466 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1467 video_encoder_config_.max_bitrate_bps = 0;
1468 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1469 video_encoder_config_.max_bitrate_bps +=
1470 params_.ss.streams[i].max_bitrate_bps;
1471 }
ilnik6b826ef2017-06-16 13:53:481472 if (params_.ss.infer_streams) {
1473 video_encoder_config_.video_stream_factory =
1474 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1475 params_.video.codec, params_.ss.streams[0].max_qp,
1476 params_.video.fps, params_.screenshare.enabled, true);
1477 } else {
1478 video_encoder_config_.video_stream_factory =
1479 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1480 }
perkjfa10b552016-10-03 06:45:261481
stefanff483612015-12-21 11:14:001482 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 12:30:241483
1484 CreateMatchingReceiveConfigs(recv_transport);
1485
sprang1168fd42017-06-21 16:00:171486 const bool decode_all_receive_streams =
1487 params_.ss.selected_stream == params_.ss.streams.size();
1488
brandtr8313a6f2017-01-13 15:41:191489 for (size_t i = 0; i < num_video_streams; ++i) {
stefanff483612015-12-21 11:14:001490 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
brandtr14742122017-01-27 12:53:071491 video_receive_configs_[i].rtp.rtx_ssrc = kSendRtxSsrcs[i];
nisse26e3abb2017-08-25 11:44:251492 video_receive_configs_[i]
1493 .rtp.rtx_associated_payload_types[kSendRtxPayloadType] = payload_type;
minyue626bc952016-10-31 12:47:021494 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
Stefan Holmer85d5ac72017-02-09 15:25:161495 video_receive_configs_[i].rtp.remb = !params_.call.send_side_bwe;
ilnik00d802b2017-04-11 17:34:311496 // Enable RTT calculation so NTP time estimator will work.
1497 video_receive_configs_[i].rtp.rtcp_xr.receiver_reference_time_report = true;
ilnik9fd9f6c2017-03-02 16:10:101498 // Force fake decoders on non-selected simulcast streams.
sprang1168fd42017-06-21 16:00:171499 if (!decode_all_receive_streams && i != params_.ss.selected_stream) {
ilnik9fd9f6c2017-03-02 16:10:101500 VideoReceiveStream::Decoder decoder;
1501 decoder.decoder = new test::FakeDecoder();
1502 decoder.payload_type = video_send_config_.encoder_settings.payload_type;
1503 decoder.payload_name = video_send_config_.encoder_settings.payload_name;
1504 video_receive_configs_[i].decoders.clear();
1505 allocated_decoders_.emplace_back(decoder.decoder);
1506 video_receive_configs_[i].decoders.push_back(decoder);
1507 }
sprangce4aef12015-11-02 15:23:201508 }
brandtr1293aca2016-11-17 06:47:291509
1510 if (params_.video.flexfec) {
brandtr8313a6f2017-01-13 15:41:191511 // Override send config constructed by CreateSendConfig.
sprang1168fd42017-06-21 16:00:171512 if (decode_all_receive_streams) {
1513 for (uint32_t media_ssrc : video_send_config_.rtp.ssrcs) {
1514 video_send_config_.rtp.flexfec.protected_media_ssrcs.push_back(
1515 media_ssrc);
1516 }
1517 } else {
1518 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1519 kVideoSendSsrcs[params_.ss.selected_stream]};
1520 }
brandtr1293aca2016-11-17 06:47:291521
brandtr8313a6f2017-01-13 15:41:191522 // The matching receive config is _not_ created by
1523 // CreateMatchingReceiveConfigs, since VideoQualityTest is not a BaseTest.
1524 // Set up the receive config manually instead.
1525 FlexfecReceiveStream::Config flexfec_receive_config(recv_transport);
brandtr1cfbd602016-12-08 12:17:531526 flexfec_receive_config.payload_type =
brandtr3d200bd2017-01-16 14:59:191527 video_send_config_.rtp.flexfec.payload_type;
1528 flexfec_receive_config.remote_ssrc = video_send_config_.rtp.flexfec.ssrc;
brandtr1293aca2016-11-17 06:47:291529 flexfec_receive_config.protected_media_ssrcs =
1530 video_send_config_.rtp.flexfec.protected_media_ssrcs;
brandtrfa5a3682017-01-17 09:33:541531 flexfec_receive_config.local_ssrc = kReceiverLocalVideoSsrc;
brandtrb29e6522016-12-21 14:37:181532 flexfec_receive_config.transport_cc = params_.call.send_side_bwe;
1533 if (params_.call.send_side_bwe) {
1534 flexfec_receive_config.rtp_header_extensions.push_back(
1535 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1536 test::kTransportSequenceNumberExtensionId));
1537 } else {
1538 flexfec_receive_config.rtp_header_extensions.push_back(RtpExtension(
1539 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1540 }
brandtr1293aca2016-11-17 06:47:291541 flexfec_receive_configs_.push_back(flexfec_receive_config);
brandtr7cd28b92017-09-22 07:26:251542 if (num_video_streams > 0) {
1543 video_receive_configs_[0].rtp.protected_by_flexfec = true;
1544 }
brandtr1293aca2016-11-17 06:47:291545 }
1546
1547 if (params_.video.ulpfec) {
1548 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1549 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1550 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1551
sprang1168fd42017-06-21 16:00:171552 if (decode_all_receive_streams) {
1553 for (auto it = video_receive_configs_.begin();
1554 it != video_receive_configs_.end(); ++it) {
nisse3b3622f2017-09-26 09:49:211555 it->rtp.red_payload_type =
sprang1168fd42017-06-21 16:00:171556 video_send_config_.rtp.ulpfec.red_payload_type;
nisse3b3622f2017-09-26 09:49:211557 it->rtp.ulpfec_payload_type =
sprang1168fd42017-06-21 16:00:171558 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
nisseca5706d2017-09-11 09:32:161559 it->rtp.rtx_associated_payload_types[video_send_config_.rtp.ulpfec
1560 .red_rtx_payload_type] =
1561 video_send_config_.rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 16:00:171562 }
1563 } else {
nisse3b3622f2017-09-26 09:49:211564 video_receive_configs_[params_.ss.selected_stream].rtp.red_payload_type =
sprang1168fd42017-06-21 16:00:171565 video_send_config_.rtp.ulpfec.red_payload_type;
1566 video_receive_configs_[params_.ss.selected_stream]
nisse3b3622f2017-09-26 09:49:211567 .rtp.ulpfec_payload_type =
sprang1168fd42017-06-21 16:00:171568 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1569 video_receive_configs_[params_.ss.selected_stream]
nisseca5706d2017-09-11 09:32:161570 .rtp.rtx_associated_payload_types[video_send_config_.rtp.ulpfec
1571 .red_rtx_payload_type] =
1572 video_send_config_.rtp.ulpfec.red_payload_type;
sprang1168fd42017-06-21 16:00:171573 }
brandtr1293aca2016-11-17 06:47:291574 }
ivica5d6a06c2015-09-17 12:30:241575}
1576
ilnika014cc52017-03-07 12:21:041577void VideoQualityTest::SetupThumbnails(Transport* send_transport,
1578 Transport* recv_transport) {
ilnik98436952017-07-13 07:47:031579 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 12:21:041580 thumbnail_encoders_.emplace_back(VP8Encoder::Create());
1581
1582 // Thumbnails will be send in the other way: from receiver_call to
1583 // sender_call.
1584 VideoSendStream::Config thumbnail_send_config(recv_transport);
1585 thumbnail_send_config.rtp.ssrcs.push_back(kThumbnailSendSsrcStart + i);
1586 thumbnail_send_config.encoder_settings.encoder =
1587 thumbnail_encoders_.back().get();
1588 thumbnail_send_config.encoder_settings.payload_name = params_.video.codec;
1589 thumbnail_send_config.encoder_settings.payload_type = kPayloadTypeVP8;
1590 thumbnail_send_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1591 thumbnail_send_config.rtp.rtx.payload_type = kSendRtxPayloadType;
1592 thumbnail_send_config.rtp.rtx.ssrcs.push_back(kThumbnailRtxSsrcStart + i);
1593 thumbnail_send_config.rtp.extensions.clear();
1594 if (params_.call.send_side_bwe) {
1595 thumbnail_send_config.rtp.extensions.push_back(
1596 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
1597 test::kTransportSequenceNumberExtensionId));
1598 } else {
1599 thumbnail_send_config.rtp.extensions.push_back(RtpExtension(
1600 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
1601 }
1602
1603 VideoEncoderConfig thumbnail_encoder_config;
1604 thumbnail_encoder_config.min_transmit_bitrate_bps = 7500;
1605 thumbnail_send_config.suspend_below_min_bitrate =
1606 params_.video.suspend_below_min_bitrate;
1607 thumbnail_encoder_config.number_of_streams = 1;
1608 thumbnail_encoder_config.max_bitrate_bps = 50000;
ilnik6b826ef2017-06-16 13:53:481609 if (params_.ss.infer_streams) {
1610 thumbnail_encoder_config.video_stream_factory =
1611 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1612 } else {
1613 thumbnail_encoder_config.video_stream_factory =
1614 new rtc::RefCountedObject<cricket::EncoderStreamFactory>(
1615 params_.video.codec, params_.ss.streams[0].max_qp,
1616 params_.video.fps, params_.screenshare.enabled, true);
1617 }
ilnika014cc52017-03-07 12:21:041618 thumbnail_encoder_config.spatial_layers = params_.ss.spatial_layers;
1619
1620 VideoReceiveStream::Config thumbnail_receive_config(send_transport);
1621 thumbnail_receive_config.rtp.remb = false;
1622 thumbnail_receive_config.rtp.transport_cc = true;
1623 thumbnail_receive_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
1624 for (const RtpExtension& extension : thumbnail_send_config.rtp.extensions)
1625 thumbnail_receive_config.rtp.extensions.push_back(extension);
1626 thumbnail_receive_config.renderer = &fake_renderer_;
1627
1628 VideoReceiveStream::Decoder decoder =
1629 test::CreateMatchingDecoder(thumbnail_send_config.encoder_settings);
1630 allocated_decoders_.push_back(
1631 std::unique_ptr<VideoDecoder>(decoder.decoder));
1632 thumbnail_receive_config.decoders.clear();
1633 thumbnail_receive_config.decoders.push_back(decoder);
1634 thumbnail_receive_config.rtp.remote_ssrc =
1635 thumbnail_send_config.rtp.ssrcs[0];
1636
1637 thumbnail_receive_config.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1638 thumbnail_receive_config.rtp.rtx_ssrc = kThumbnailRtxSsrcStart + i;
nisse26e3abb2017-08-25 11:44:251639 thumbnail_receive_config.rtp
1640 .rtx_associated_payload_types[kSendRtxPayloadType] = kPayloadTypeVP8;
ilnika014cc52017-03-07 12:21:041641 thumbnail_receive_config.rtp.transport_cc = params_.call.send_side_bwe;
1642 thumbnail_receive_config.rtp.remb = !params_.call.send_side_bwe;
1643
1644 thumbnail_encoder_configs_.push_back(thumbnail_encoder_config.Copy());
1645 thumbnail_send_configs_.push_back(thumbnail_send_config.Copy());
1646 thumbnail_receive_configs_.push_back(thumbnail_receive_config.Copy());
1647 }
1648
ilnik98436952017-07-13 07:47:031649 for (int i = 0; i < params_.call.num_thumbnails; ++i) {
ilnika014cc52017-03-07 12:21:041650 thumbnail_send_streams_.push_back(receiver_call_->CreateVideoSendStream(
1651 thumbnail_send_configs_[i].Copy(),
1652 thumbnail_encoder_configs_[i].Copy()));
1653 thumbnail_receive_streams_.push_back(sender_call_->CreateVideoReceiveStream(
1654 thumbnail_receive_configs_[i].Copy()));
1655 }
1656}
1657
1658void VideoQualityTest::DestroyThumbnailStreams() {
1659 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1660 receiver_call_->DestroyVideoSendStream(thumbnail_send_stream);
1661 thumbnail_send_streams_.clear();
1662 for (VideoReceiveStream* thumbnail_receive_stream :
1663 thumbnail_receive_streams_)
1664 sender_call_->DestroyVideoReceiveStream(thumbnail_receive_stream);
1665 thumbnail_send_streams_.clear();
1666 thumbnail_receive_streams_.clear();
eladalon413ee9a2017-08-22 11:02:521667 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1668 thumbnail_capturers_) {
1669 video_caputurer.reset();
1670 }
ilnika014cc52017-03-07 12:21:041671}
1672
ilnik2a8c2f52017-02-15 10:23:281673void VideoQualityTest::SetupScreenshareOrSVC() {
1674 if (params_.screenshare.enabled) {
1675 // Fill out codec settings.
1676 video_encoder_config_.content_type =
1677 VideoEncoderConfig::ContentType::kScreen;
1678 degradation_preference_ =
1679 VideoSendStream::DegradationPreference::kMaintainResolution;
1680 if (params_.video.codec == "VP8") {
1681 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1682 vp8_settings.denoisingOn = false;
1683 vp8_settings.frameDroppingOn = false;
1684 vp8_settings.numberOfTemporalLayers =
1685 static_cast<unsigned char>(params_.video.num_temporal_layers);
1686 video_encoder_config_.encoder_specific_settings =
1687 new rtc::RefCountedObject<
1688 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
1689 } else if (params_.video.codec == "VP9") {
1690 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1691 vp9_settings.denoisingOn = false;
1692 vp9_settings.frameDroppingOn = false;
1693 vp9_settings.numberOfTemporalLayers =
1694 static_cast<unsigned char>(params_.video.num_temporal_layers);
1695 vp9_settings.numberOfSpatialLayers =
1696 static_cast<unsigned char>(params_.ss.num_spatial_layers);
1697 video_encoder_config_.encoder_specific_settings =
1698 new rtc::RefCountedObject<
1699 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
1700 }
1701 // Setup frame generator.
1702 const size_t kWidth = 1850;
1703 const size_t kHeight = 1110;
erikvarga579de6f2017-08-29 16:12:571704 if (params_.screenshare.generate_slides) {
1705 frame_generator_ = test::FrameGenerator::CreateSlideGenerator(
1706 kWidth, kHeight,
perkja8ba1952017-02-27 14:52:101707 params_.screenshare.slide_change_interval * params_.video.fps);
ilnik2a8c2f52017-02-15 10:23:281708 } else {
erikvarga579de6f2017-08-29 16:12:571709 std::vector<std::string> slides = params_.screenshare.slides;
1710 if (slides.size() == 0) {
1711 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1712 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1713 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1714 slides.push_back(
1715 test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1716 }
1717 if (params_.screenshare.scroll_duration == 0) {
1718 // Cycle image every slide_change_interval seconds.
1719 frame_generator_ = test::FrameGenerator::CreateFromYuvFile(
1720 slides, kWidth, kHeight,
1721 params_.screenshare.slide_change_interval * params_.video.fps);
1722 } else {
1723 RTC_CHECK_LE(params_.video.width, kWidth);
1724 RTC_CHECK_LE(params_.video.height, kHeight);
1725 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1726 const int kPauseDurationMs =
1727 (params_.screenshare.slide_change_interval -
1728 params_.screenshare.scroll_duration) *
1729 1000;
1730 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1731 params_.screenshare.slide_change_interval);
ilnik2a8c2f52017-02-15 10:23:281732
erikvarga579de6f2017-08-29 16:12:571733 frame_generator_ =
1734 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
1735 clock_, slides, kWidth, kHeight, params_.video.width,
1736 params_.video.height,
1737 params_.screenshare.scroll_duration * 1000, kPauseDurationMs);
1738 }
ilnik2a8c2f52017-02-15 10:23:281739 }
1740 } else if (params_.ss.num_spatial_layers > 1) { // For non-screenshare case.
1741 RTC_CHECK(params_.video.codec == "VP9");
kthelgason29a44e32016-09-27 10:52:021742 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
kthelgason29a44e32016-09-27 10:52:021743 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 12:47:021744 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 10:52:021745 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 15:23:201746 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 10:52:021747 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1748 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 12:30:241749 }
ivica5d6a06c2015-09-17 12:30:241750}
1751
ilnika014cc52017-03-07 12:21:041752void VideoQualityTest::SetupThumbnailCapturers(size_t num_thumbnail_streams) {
1753 VideoStream thumbnail = DefaultThumbnailStream();
1754 for (size_t i = 0; i < num_thumbnail_streams; ++i) {
1755 thumbnail_capturers_.emplace_back(test::FrameGeneratorCapturer::Create(
1756 static_cast<int>(thumbnail.width), static_cast<int>(thumbnail.height),
1757 thumbnail.max_framerate, clock_));
ilnikf89a7382017-03-07 14:15:271758 RTC_DCHECK(thumbnail_capturers_.back());
ilnika014cc52017-03-07 12:21:041759 }
1760}
1761
perkja49cbd32016-09-16 14:53:411762void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 15:23:201763 if (params_.screenshare.enabled) {
1764 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja8ba1952017-02-27 14:52:101765 new test::FrameGeneratorCapturer(clock_, std::move(frame_generator_),
minyue626bc952016-10-31 12:47:021766 params_.video.fps);
ivica2d4e6c52015-09-23 08:57:061767 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 12:59:291768 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 12:30:241769 } else {
ilnik6b826ef2017-06-16 13:53:481770 if (params_.video.clip_name == "Generator") {
1771 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
1772 static_cast<int>(params_.video.width),
1773 static_cast<int>(params_.video.height), params_.video.fps, clock_));
1774 } else if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 12:59:291775 video_capturer_.reset(test::VcmCapturer::Create(
Tarun Chawla8e857d12017-05-31 13:50:571776 params_.video.width, params_.video.height, params_.video.fps,
1777 params_.video.capture_device_index));
sprang1bed2e42017-01-23 16:46:511778 if (!video_capturer_) {
1779 // Failed to get actual camera, use chroma generator as backup.
1780 video_capturer_.reset(test::FrameGeneratorCapturer::Create(
perkja8ba1952017-02-27 14:52:101781 static_cast<int>(params_.video.width),
1782 static_cast<int>(params_.video.height), params_.video.fps, clock_));
sprang1bed2e42017-01-23 16:46:511783 }
ivica5d6a06c2015-09-17 12:30:241784 } else {
minyuea27172d2016-11-01 12:59:291785 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 14:53:411786 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 12:47:021787 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 08:57:061788 clock_));
minyuea27172d2016-11-01 12:59:291789 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1790 << params_.video.clip_name
1791 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 12:30:241792 }
1793 }
sprang1bed2e42017-01-23 16:46:511794 RTC_DCHECK(video_capturer_.get());
ivica5d6a06c2015-09-17 12:30:241795}
1796
sprang7a975f72015-10-12 13:33:211797void VideoQualityTest::RunWithAnalyzer(const Params& params) {
eladalon413ee9a2017-08-22 11:02:521798 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1799 std::unique_ptr<test::DirectTransport> recv_transport;
1800 FILE* graph_data_output_file = nullptr;
1801 std::unique_ptr<VideoAnalyzer> analyzer;
1802
sprangce4aef12015-11-02 15:23:201803 params_ = params;
1804
minyue626bc952016-10-31 12:47:021805 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 12:30:241806 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1807 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 15:23:201808 CheckParams();
ivica5d6a06c2015-09-17 12:30:241809
sprangce4aef12015-11-02 15:23:201810 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 12:30:241811 graph_data_output_file =
sprangce4aef12015-11-02 15:23:201812 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 15:56:101813 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 15:23:201814 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1815 << "!";
ivica87f83a92015-10-08 12:13:321816 }
sprang7a975f72015-10-12 13:33:211817
ilnik98436952017-07-13 07:47:031818 if (!params.logging.rtc_event_log_name.empty()) {
1819 event_log_ = RtcEventLog::Create(clock_);
1820 bool event_log_started =
1821 event_log_->StartLogging(params.logging.rtc_event_log_name, -1);
1822 RTC_DCHECK(event_log_started);
1823 }
1824
philipel4fb651d2017-04-10 10:54:051825 Call::Config call_config(event_log_.get());
minyue626bc952016-10-31 12:47:021826 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 15:29:421827
eladalon413ee9a2017-08-22 11:02:521828 task_queue_.SendTask([this, &call_config, &send_transport,
1829 &recv_transport]() {
1830 CreateCalls(call_config, call_config);
minyue20c84cc2017-04-10 23:57:571831
eladalon413ee9a2017-08-22 11:02:521832 send_transport = rtc::MakeUnique<test::LayerFilteringTransport>(
1833 &task_queue_, params_.pipe, sender_call_.get(), kPayloadTypeVP8,
1834 kPayloadTypeVP9, params_.video.selected_tl, params_.ss.selected_sl,
1835 payload_type_map_);
1836
1837 recv_transport = rtc::MakeUnique<test::DirectTransport>(
1838 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
1839 });
stefanf116bd02015-10-27 15:29:421840
sprangce4aef12015-11-02 15:23:201841 std::string graph_title = params_.analyzer.graph_title;
1842 if (graph_title.empty())
1843 graph_title = VideoQualityTest::GenerateGraphTitle();
sprangc1b57a12017-02-28 16:50:471844 bool is_quick_test_enabled = field_trial::IsEnabled("WebRTC-QuickPerfTest");
eladalon413ee9a2017-08-22 11:02:521845 analyzer = rtc::MakeUnique<VideoAnalyzer>(
1846 send_transport.get(), params_.analyzer.test_label,
ilnik2a8c2f52017-02-15 10:23:281847 params_.analyzer.avg_psnr_threshold, params_.analyzer.avg_ssim_threshold,
ilnik9ae0d762017-02-15 08:53:121848 is_quick_test_enabled
1849 ? kFramesSentInQuickTest
1850 : params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 15:23:201851 graph_data_output_file, graph_title,
ilnik3dd5ad92017-02-09 12:58:531852 kVideoSendSsrcs[params_.ss.selected_stream],
ilnik46a00212017-02-10 17:16:051853 kSendRtxSsrcs[params_.ss.selected_stream],
ilnikcb8c1462017-03-09 17:23:301854 static_cast<size_t>(params_.ss.selected_stream), params.ss.selected_sl,
ilnik98436952017-07-13 07:47:031855 params_.video.selected_tl, is_quick_test_enabled, clock_,
1856 params_.logging.rtp_dump_name);
ivica5d6a06c2015-09-17 12:30:241857
eladalon413ee9a2017-08-22 11:02:521858 task_queue_.SendTask([&]() {
1859 analyzer->SetCall(sender_call_.get());
1860 analyzer->SetReceiver(receiver_call_->Receiver());
1861 send_transport->SetReceiver(analyzer.get());
1862 recv_transport->SetReceiver(sender_call_->Receiver());
ivica5d6a06c2015-09-17 12:30:241863
eladalon413ee9a2017-08-22 11:02:521864 SetupVideo(analyzer.get(), recv_transport.get());
1865 SetupThumbnails(analyzer.get(), recv_transport.get());
1866 video_receive_configs_[params_.ss.selected_stream].renderer =
1867 analyzer.get();
1868 video_send_config_.pre_encode_callback = analyzer->pre_encode_proxy();
1869 RTC_DCHECK(!video_send_config_.post_encode_callback);
1870 video_send_config_.post_encode_callback = analyzer->encode_timing_proxy();
ivica5d6a06c2015-09-17 12:30:241871
eladalon413ee9a2017-08-22 11:02:521872 SetupScreenshareOrSVC();
kthelgason2bc68642017-02-07 15:02:221873
eladalon413ee9a2017-08-22 11:02:521874 CreateFlexfecStreams();
1875 CreateVideoStreams();
1876 analyzer->SetSendStream(video_send_stream_);
1877 if (video_receive_streams_.size() == 1)
1878 analyzer->SetReceiveStream(video_receive_streams_[0]);
ivica5d6a06c2015-09-17 12:30:241879
eladalon413ee9a2017-08-22 11:02:521880 video_send_stream_->SetSource(analyzer->OutputInterface(),
1881 degradation_preference_);
ilnika014cc52017-03-07 12:21:041882
eladalon413ee9a2017-08-22 11:02:521883 SetupThumbnailCapturers(params_.call.num_thumbnails);
1884 for (size_t i = 0; i < thumbnail_send_streams_.size(); ++i) {
1885 thumbnail_send_streams_[i]->SetSource(thumbnail_capturers_[i].get(),
1886 degradation_preference_);
1887 }
ilnika014cc52017-03-07 12:21:041888
eladalon413ee9a2017-08-22 11:02:521889 CreateCapturer();
ivicac1cc8542015-10-08 10:44:061890
eladalon413ee9a2017-08-22 11:02:521891 analyzer->SetSource(video_capturer_.get(), params_.ss.infer_streams);
ilnika014cc52017-03-07 12:21:041892
eladalon413ee9a2017-08-22 11:02:521893 StartEncodedFrameLogs(video_send_stream_);
1894 StartEncodedFrameLogs(video_receive_streams_[params_.ss.selected_stream]);
1895 video_send_stream_->Start();
1896 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1897 thumbnail_send_stream->Start();
1898 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1899 receive_stream->Start();
1900 for (VideoReceiveStream* thumbnail_receive_stream :
1901 thumbnail_receive_streams_)
1902 thumbnail_receive_stream->Start();
ilnika014cc52017-03-07 12:21:041903
eladalon413ee9a2017-08-22 11:02:521904 analyzer->StartMeasuringCpuProcessTime();
ivica5d6a06c2015-09-17 12:30:241905
eladalon413ee9a2017-08-22 11:02:521906 video_capturer_->Start();
1907 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1908 thumbnail_capturers_) {
1909 video_caputurer->Start();
1910 }
1911 });
ivica5d6a06c2015-09-17 12:30:241912
eladalon413ee9a2017-08-22 11:02:521913 analyzer->Wait();
ivica5d6a06c2015-09-17 12:30:241914
Ilya Nikolaevskiy2c72fe82017-10-02 11:01:041915 event_log_->StopLogging();
1916
eladalon413ee9a2017-08-22 11:02:521917 task_queue_.SendTask([&]() {
1918 for (std::unique_ptr<test::VideoCapturer>& video_caputurer :
1919 thumbnail_capturers_)
1920 video_caputurer->Stop();
1921 video_capturer_->Stop();
1922 for (VideoReceiveStream* thumbnail_receive_stream :
1923 thumbnail_receive_streams_)
1924 thumbnail_receive_stream->Stop();
1925 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1926 receive_stream->Stop();
1927 for (VideoSendStream* thumbnail_send_stream : thumbnail_send_streams_)
1928 thumbnail_send_stream->Stop();
1929 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 12:30:241930
eladalon413ee9a2017-08-22 11:02:521931 DestroyStreams();
1932 DestroyThumbnailStreams();
ivica5d6a06c2015-09-17 12:30:241933
eladalon413ee9a2017-08-22 11:02:521934 if (graph_data_output_file)
1935 fclose(graph_data_output_file);
1936
1937 video_capturer_.reset();
1938 send_transport.reset();
1939 recv_transport.reset();
1940
1941 DestroyCalls();
1942 });
ivica5d6a06c2015-09-17 12:30:241943}
1944
minyuea27172d2016-11-01 12:59:291945void VideoQualityTest::SetupAudio(int send_channel_id,
1946 int receive_channel_id,
minyuea27172d2016-11-01 12:59:291947 Transport* transport,
1948 AudioReceiveStream** audio_receive_stream) {
1949 audio_send_config_ = AudioSendStream::Config(transport);
1950 audio_send_config_.voe_channel_id = send_channel_id;
1951 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1952
1953 // Add extension to enable audio send side BWE, and allow audio bit rate
1954 // adaptation.
1955 audio_send_config_.rtp.extensions.clear();
1956 if (params_.call.send_side_bwe) {
1957 audio_send_config_.rtp.extensions.push_back(
1958 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1959 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 17:29:221960 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1961 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 12:59:291962 }
ossu20a4b3f2017-04-27 09:08:521963 audio_send_config_.send_codec_spec =
1964 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
1965 {kAudioSendPayloadType,
1966 {"OPUS", 48000, 2,
1967 {{"usedtx", (params_.audio.dtx ? "1" : "0")},
1968 {"stereo", "1"}}}});
Rasmus Brandt31027342017-09-29 13:48:121969 audio_send_config_.encoder_factory = encoder_factory_;
sprang1168fd42017-06-21 16:00:171970 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
minyuea27172d2016-11-01 12:59:291971
1972 AudioReceiveStream::Config audio_config;
1973 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1974 audio_config.rtcp_send_transport = transport;
1975 audio_config.voe_channel_id = receive_channel_id;
1976 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1977 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1978 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
Rasmus Brandt31027342017-09-29 13:48:121979 audio_config.decoder_factory = decoder_factory_;
minyue20c84cc2017-04-10 23:57:571980 audio_config.decoder_map = {{kAudioSendPayloadType, {"OPUS", 48000, 2}}};
minyuea27172d2016-11-01 12:59:291981 if (params_.video.enabled && params_.audio.sync_video)
1982 audio_config.sync_group = kSyncGroup;
1983
sprang1168fd42017-06-21 16:00:171984 *audio_receive_stream =
1985 receiver_call_->CreateAudioReceiveStream(audio_config);
minyuea27172d2016-11-01 12:59:291986}
1987
minyue73208662016-08-18 13:28:551988void VideoQualityTest::RunWithRenderers(const Params& params) {
eladalon413ee9a2017-08-22 11:02:521989 std::unique_ptr<test::LayerFilteringTransport> send_transport;
1990 std::unique_ptr<test::DirectTransport> recv_transport;
minyue73208662016-08-18 13:28:551991 ::VoiceEngineState voe;
minyuea27172d2016-11-01 12:59:291992 std::unique_ptr<test::VideoRenderer> local_preview;
eladalon413ee9a2017-08-22 11:02:521993 std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
minyue73208662016-08-18 13:28:551994 AudioReceiveStream* audio_receive_stream = nullptr;
minyue73208662016-08-18 13:28:551995
eladalon413ee9a2017-08-22 11:02:521996 task_queue_.SendTask([&]() {
1997 params_ = params;
1998 CheckParams();
palmkviste75f2042016-09-28 13:19:481999
eladalon413ee9a2017-08-22 11:02:522000 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
2001 // match the full stack tests.
2002 Call::Config call_config(event_log_.get());
2003 call_config.bitrate_config = params_.call.call_bitrate_config;
Niels Möller2bf9e732017-08-14 09:26:162004
eladalon413ee9a2017-08-22 11:02:522005 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
2006 webrtc::AudioProcessing::Create());
ivica5d6a06c2015-09-17 12:30:242007
eladalon413ee9a2017-08-22 11:02:522008 if (params_.audio.enabled) {
Rasmus Brandt31027342017-09-29 13:48:122009 CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
eladalon413ee9a2017-08-22 11:02:522010 AudioState::Config audio_state_config;
2011 audio_state_config.voice_engine = voe.voice_engine;
2012 audio_state_config.audio_mixer = AudioMixerImpl::Create();
2013 audio_state_config.audio_processing = audio_processing;
2014 call_config.audio_state = AudioState::Create(audio_state_config);
2015 }
minyue73208662016-08-18 13:28:552016
eladalon413ee9a2017-08-22 11:02:522017 CreateCalls(call_config, call_config);
2018
2019 // TODO(minyue): consider if this is a good transport even for audio only
2020 // calls.
2021 send_transport = rtc::MakeUnique<test::LayerFilteringTransport>(
2022 &task_queue_, params.pipe, sender_call_.get(), kPayloadTypeVP8,
2023 kPayloadTypeVP9, params.video.selected_tl, params_.ss.selected_sl,
2024 payload_type_map_);
2025
2026 recv_transport = rtc::MakeUnique<test::DirectTransport>(
2027 &task_queue_, params_.pipe, receiver_call_.get(), payload_type_map_);
2028
2029 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
2030 // least share as much code as possible. That way this test would also match
2031 // the full stack tests better.
2032 send_transport->SetReceiver(receiver_call_->Receiver());
2033 recv_transport->SetReceiver(sender_call_->Receiver());
2034
2035 if (params_.video.enabled) {
2036 // Create video renderers.
2037 local_preview.reset(test::VideoRenderer::Create(
2038 "Local Preview", params_.video.width, params_.video.height));
2039
2040 const size_t selected_stream_id = params_.ss.selected_stream;
2041 const size_t num_streams = params_.ss.streams.size();
2042
2043 if (selected_stream_id == num_streams) {
2044 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2045 std::ostringstream oss;
2046 oss << "Loopback Video - Stream #" << static_cast<int>(stream_id);
2047 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2048 oss.str().c_str(), params_.ss.streams[stream_id].width,
2049 params_.ss.streams[stream_id].height));
2050 }
2051 } else {
2052 loopback_renderers.emplace_back(test::VideoRenderer::Create(
2053 "Loopback Video", params_.ss.streams[selected_stream_id].width,
2054 params_.ss.streams[selected_stream_id].height));
2055 }
2056
2057 SetupVideo(send_transport.get(), recv_transport.get());
2058
2059 video_send_config_.pre_encode_callback = local_preview.get();
2060 if (selected_stream_id == num_streams) {
2061 for (size_t stream_id = 0; stream_id < num_streams; ++stream_id) {
2062 video_receive_configs_[stream_id].renderer =
2063 loopback_renderers[stream_id].get();
2064 if (params_.audio.enabled && params_.audio.sync_video)
2065 video_receive_configs_[stream_id].sync_group = kSyncGroup;
2066 }
2067 } else {
2068 video_receive_configs_[selected_stream_id].renderer =
2069 loopback_renderers.back().get();
2070 if (params_.audio.enabled && params_.audio.sync_video)
2071 video_receive_configs_[selected_stream_id].sync_group = kSyncGroup;
2072 }
2073
2074 if (params_.screenshare.enabled)
2075 SetupScreenshareOrSVC();
2076
2077 CreateFlexfecStreams();
2078 CreateVideoStreams();
2079
2080 CreateCapturer();
2081 video_send_stream_->SetSource(video_capturer_.get(),
2082 degradation_preference_);
2083 }
2084
2085 if (params_.audio.enabled) {
2086 SetupAudio(voe.send_channel_id, voe.receive_channel_id,
2087 send_transport.get(), &audio_receive_stream);
2088 }
2089
2090 for (VideoReceiveStream* receive_stream : video_receive_streams_)
2091 StartEncodedFrameLogs(receive_stream);
2092 StartEncodedFrameLogs(video_send_stream_);
2093
2094 // Start sending and receiving video.
2095 if (params_.video.enabled) {
2096 for (VideoReceiveStream* video_receive_stream : video_receive_streams_)
2097 video_receive_stream->Start();
2098
2099 video_send_stream_->Start();
2100 video_capturer_->Start();
2101 }
2102
2103 if (params_.audio.enabled) {
2104 // Start receiving audio.
2105 audio_receive_stream->Start();
2106 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
2107
2108 // Start sending audio.
2109 audio_send_stream_->Start();
2110 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
2111 }
2112 });
minyue73208662016-08-18 13:28:552113
ivica5d6a06c2015-09-17 12:30:242114 test::PressEnterToContinue();
2115
eladalon413ee9a2017-08-22 11:02:522116 task_queue_.SendTask([&]() {
2117 if (params_.audio.enabled) {
2118 // Stop sending audio.
2119 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
2120 audio_send_stream_->Stop();
minyue73208662016-08-18 13:28:552121
eladalon413ee9a2017-08-22 11:02:522122 // Stop receiving audio.
2123 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
2124 audio_receive_stream->Stop();
2125 sender_call_->DestroyAudioSendStream(audio_send_stream_);
2126 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
2127 }
minyue73208662016-08-18 13:28:552128
eladalon413ee9a2017-08-22 11:02:522129 // Stop receiving and sending video.
2130 if (params_.video.enabled) {
2131 video_capturer_->Stop();
2132 video_send_stream_->Stop();
2133 for (FlexfecReceiveStream* flexfec_receive_stream :
2134 flexfec_receive_streams_) {
2135 for (VideoReceiveStream* video_receive_stream :
2136 video_receive_streams_) {
2137 video_receive_stream->RemoveSecondarySink(flexfec_receive_stream);
2138 }
2139 receiver_call_->DestroyFlexfecReceiveStream(flexfec_receive_stream);
eladalonc0d481a2017-08-02 14:39:072140 }
eladalon413ee9a2017-08-22 11:02:522141 for (VideoReceiveStream* receive_stream : video_receive_streams_) {
2142 receive_stream->Stop();
2143 receiver_call_->DestroyVideoReceiveStream(receive_stream);
2144 }
2145 sender_call_->DestroyVideoSendStream(video_send_stream_);
brandtr1293aca2016-11-17 06:47:292146 }
minyue73208662016-08-18 13:28:552147
eladalon413ee9a2017-08-22 11:02:522148 video_capturer_.reset();
2149 send_transport.reset();
2150 recv_transport.reset();
sprang1168fd42017-06-21 16:00:172151
eladalon413ee9a2017-08-22 11:02:522152 if (params_.audio.enabled)
2153 DestroyVoiceEngine(&voe);
2154
2155 local_preview.reset();
2156 loopback_renderers.clear();
2157
2158 DestroyCalls();
2159 });
ivica5d6a06c2015-09-17 12:30:242160}
2161
palmkviste75f2042016-09-28 13:19:482162void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
ilnik98436952017-07-13 07:47:032163 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 13:19:482164 std::ostringstream str;
2165 str << send_logs_++;
2166 std::string prefix =
ilnik98436952017-07-13 07:47:032167 params_.logging.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 13:19:482168 stream->EnableEncodedFrameRecording(
2169 std::vector<rtc::PlatformFile>(
2170 {rtc::CreatePlatformFile(prefix + "1.ivf"),
2171 rtc::CreatePlatformFile(prefix + "2.ivf"),
2172 rtc::CreatePlatformFile(prefix + "3.ivf")}),
ilnik98436952017-07-13 07:47:032173 100000000);
palmkviste75f2042016-09-28 13:19:482174 }
2175}
ilnikcb8c1462017-03-09 17:23:302176
palmkviste75f2042016-09-28 13:19:482177void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
ilnik98436952017-07-13 07:47:032178 if (!params_.logging.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 13:19:482179 std::ostringstream str;
2180 str << receive_logs_++;
2181 std::string path =
ilnik98436952017-07-13 07:47:032182 params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 13:19:482183 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
ilnik98436952017-07-13 07:47:032184 100000000);
palmkviste75f2042016-09-28 13:19:482185 }
2186}
ivica5d6a06c2015-09-17 12:30:242187} // namespace webrtc