blob: b48c69237695f6c5e31213113cf731014599e875 [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 */
perkja49cbd32016-09-16 14:53:4110#include "webrtc/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>
sprangce4aef12015-11-02 15:23:2016#include <sstream>
mflodmand1590b22015-12-09 15:07:5917#include <string>
ivica5d6a06c2015-09-17 12:30:2418#include <vector>
19
ivica5d6a06c2015-09-17 12:30:2420#include "webrtc/base/checks.h"
Peter Boström5811a392015-12-10 12:02:5021#include "webrtc/base/event.h"
ivica5d6a06c2015-09-17 12:30:2422#include "webrtc/base/format_macros.h"
nissec7fe3c22016-04-20 10:25:3623#include "webrtc/base/optional.h"
palmkviste75f2042016-09-28 13:19:4824#include "webrtc/base/platform_file.h"
sprange1f2f1f2016-02-01 10:04:5225#include "webrtc/base/timeutils.h"
ivica5d6a06c2015-09-17 12:30:2426#include "webrtc/call.h"
27#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
skvlad11a9cbf2016-10-07 18:53:0528#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
Henrik Kjellanderff761fb2015-11-04 07:31:5229#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprangce4aef12015-11-02 15:23:2030#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Henrik Kjellander98f53512015-10-28 17:17:4031#include "webrtc/system_wrappers/include/cpu_info.h"
kwibergac9f8762016-10-01 05:29:4332#include "webrtc/test/gtest.h"
ivica5d6a06c2015-09-17 12:30:2433#include "webrtc/test/layer_filtering_transport.h"
34#include "webrtc/test/run_loop.h"
35#include "webrtc/test/statistics.h"
36#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 14:53:4137#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 12:30:2438#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 13:28:5539#include "webrtc/voice_engine/include/voe_base.h"
minyue73208662016-08-18 13:28:5540
41namespace {
42
43constexpr int kSendStatsPollingIntervalMs = 1000;
44constexpr int kPayloadTypeH264 = 122;
45constexpr int kPayloadTypeVP8 = 123;
46constexpr int kPayloadTypeVP9 = 124;
47constexpr size_t kMaxComparisons = 10;
48constexpr char kSyncGroup[] = "av_sync";
minyue10cbb462016-11-07 17:29:2249constexpr int kOpusMinBitrateBps = 6000;
50constexpr int kOpusBitrateFbBps = 32000;
minyue73208662016-08-18 13:28:5551
52struct VoiceEngineState {
53 VoiceEngineState()
54 : voice_engine(nullptr),
55 base(nullptr),
minyue73208662016-08-18 13:28:5556 send_channel_id(-1),
57 receive_channel_id(-1) {}
58
59 webrtc::VoiceEngine* voice_engine;
60 webrtc::VoEBase* base;
minyue73208662016-08-18 13:28:5561 int send_channel_id;
62 int receive_channel_id;
63};
64
65void CreateVoiceEngine(VoiceEngineState* voe,
66 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
67 decoder_factory) {
68 voe->voice_engine = webrtc::VoiceEngine::Create();
69 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
minyue73208662016-08-18 13:28:5570 EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory));
solenberg88499ec2016-09-07 14:34:4171 webrtc::VoEBase::ChannelConfig config;
72 config.enable_voice_pacing = true;
73 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 13:28:5574 EXPECT_GE(voe->send_channel_id, 0);
75 voe->receive_channel_id = voe->base->CreateChannel();
76 EXPECT_GE(voe->receive_channel_id, 0);
77}
78
79void DestroyVoiceEngine(VoiceEngineState* voe) {
80 voe->base->DeleteChannel(voe->send_channel_id);
81 voe->send_channel_id = -1;
82 voe->base->DeleteChannel(voe->receive_channel_id);
83 voe->receive_channel_id = -1;
84 voe->base->Release();
85 voe->base = nullptr;
minyue73208662016-08-18 13:28:5586
87 webrtc::VoiceEngine::Delete(voe->voice_engine);
88 voe->voice_engine = nullptr;
89}
90
perkjfa10b552016-10-03 06:45:2691class VideoStreamFactory
92 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
93 public:
94 explicit VideoStreamFactory(const std::vector<webrtc::VideoStream>& streams)
95 : streams_(streams) {}
96
97 private:
98 std::vector<webrtc::VideoStream> CreateEncoderStreams(
99 int width,
100 int height,
101 const webrtc::VideoEncoderConfig& encoder_config) override {
102 return streams_;
103 }
104
105 std::vector<webrtc::VideoStream> streams_;
106};
107
minyue73208662016-08-18 13:28:55108} // namespace
ivica5d6a06c2015-09-17 12:30:24109
110namespace webrtc {
111
ivica5d6a06c2015-09-17 12:30:24112class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 16:59:31113 public Transport,
nisse7ade7b32016-03-23 11:48:10114 public rtc::VideoSinkInterface<VideoFrame>,
Peter Boströme4499152016-02-05 10:13:28115 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 12:30:24116 public:
sprangce4aef12015-11-02 15:23:20117 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 12:30:24118 const std::string& test_label,
119 double avg_psnr_threshold,
120 double avg_ssim_threshold,
121 int duration_frames,
sprangce4aef12015-11-02 15:23:20122 FILE* graph_data_output_file,
123 const std::string& graph_title,
124 uint32_t ssrc_to_analyze)
perkja49cbd32016-09-16 14:53:41125 : transport_(transport),
ivica5d6a06c2015-09-17 12:30:24126 receiver_(nullptr),
127 send_stream_(nullptr),
perkja49cbd32016-09-16 14:53:41128 captured_frame_forwarder_(this),
ivica5d6a06c2015-09-17 12:30:24129 test_label_(test_label),
130 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 15:23:20131 graph_title_(graph_title),
132 ssrc_to_analyze_(ssrc_to_analyze),
pbos14fe7082016-04-20 13:35:56133 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 10:13:28134 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 12:30:24135 frames_to_process_(duration_frames),
136 frames_recorded_(0),
137 frames_processed_(0),
138 dropped_frames_(0),
pbos14fe7082016-04-20 13:35:56139 dropped_frames_before_first_encode_(0),
140 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 12:30:24141 last_render_time_(0),
142 rtp_timestamp_delta_(0),
143 avg_psnr_threshold_(avg_psnr_threshold),
144 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 16:45:47145 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 12:02:50146 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 14:22:32147 done_(true, false) {
ivica5d6a06c2015-09-17 12:30:24148 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
149
150 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
151 // so that we don't accidentally starve "real" worker threads (codec etc).
152 // Also, don't allocate more than kMaxComparisonThreads, even if there are
153 // spare cores.
154
155 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
156 RTC_DCHECK_GE(num_cores, 1u);
157 static const uint32_t kMinCoresLeft = 4;
158 static const uint32_t kMaxComparisonThreads = 8;
159
160 if (num_cores <= kMinCoresLeft) {
161 num_cores = 1;
162 } else {
163 num_cores -= kMinCoresLeft;
164 num_cores = std::min(num_cores, kMaxComparisonThreads);
165 }
166
167 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 16:45:47168 rtc::PlatformThread* thread =
169 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
170 thread->Start();
171 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 12:30:24172 }
ivica5d6a06c2015-09-17 12:30:24173 }
174
175 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 16:45:47176 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
177 thread->Stop();
ivica5d6a06c2015-09-17 12:30:24178 delete thread;
179 }
180 }
181
182 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
183
perkja49cbd32016-09-16 14:53:41184 void SetSendStream(VideoSendStream* stream) {
185 rtc::CritScope lock(&crit_);
186 RTC_DCHECK(!send_stream_);
187 send_stream_ = stream;
188 }
189
190 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
191 return &captured_frame_forwarder_;
192 }
193 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
194 return &captured_frame_forwarder_;
195 }
196
ivica5d6a06c2015-09-17 12:30:24197 DeliveryStatus DeliverPacket(MediaType media_type,
198 const uint8_t* packet,
199 size_t length,
200 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 18:41:03201 // Ignore timestamps of RTCP packets. They're not synchronized with
202 // RTP packet timestamps and so they would confuse wrap_handler_.
203 if (RtpHeaderParser::IsRtcp(packet, length)) {
204 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
205 }
206
sprangce4aef12015-11-02 15:23:20207 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 12:30:24208 RTPHeader header;
danilchapf6975f42015-12-28 18:18:46209 parser.Parse(&header);
ivica5d6a06c2015-09-17 12:30:24210 {
211 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 09:30:24212 int64_t timestamp =
213 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
214 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 12:30:24215 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
216 }
217
218 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
219 }
220
Peter Boströme4499152016-02-05 10:13:28221 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 09:43:12222 rtc::CritScope crit(&comparison_lock_);
223 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
224 }
225
pbos14fe7082016-04-20 13:35:56226 void PreEncodeOnFrame(const VideoFrame& video_frame) {
227 rtc::CritScope lock(&crit_);
228 if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0) {
229 while (frames_.front().timestamp() != video_frame.timestamp()) {
230 ++dropped_frames_before_first_encode_;
231 frames_.pop_front();
232 RTC_CHECK(!frames_.empty());
233 }
234 first_send_timestamp_ = rtc::Optional<uint32_t>(video_frame.timestamp());
235 }
236 }
237
stefan1d8a5062015-10-02 10:39:33238 bool SendRtp(const uint8_t* packet,
239 size_t length,
240 const PacketOptions& options) override {
sprangce4aef12015-11-02 15:23:20241 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 12:30:24242 RTPHeader header;
danilchapf6975f42015-12-28 18:18:46243 parser.Parse(&header);
ivica5d6a06c2015-09-17 12:30:24244
sprangce4aef12015-11-02 15:23:20245 int64_t current_time =
246 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
247 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 12:30:24248 {
249 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 10:04:52250
Peter Boström81cbd9242016-03-22 11:19:07251 if (rtp_timestamp_delta_ == 0) {
nissec7fe3c22016-04-20 10:25:36252 rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_;
253 first_send_timestamp_ = rtc::Optional<uint32_t>();
ivica5d6a06c2015-09-17 12:30:24254 }
sprang1b3530b2016-03-10 09:32:53255 int64_t timestamp =
256 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 15:23:20257 send_times_[timestamp] = current_time;
258 if (!transport_->DiscardedLastPacket() &&
259 header.ssrc == ssrc_to_analyze_) {
260 encoded_frame_sizes_[timestamp] +=
261 length - (header.headerLength + header.paddingLength);
262 }
ivica5d6a06c2015-09-17 12:30:24263 }
sprangce4aef12015-11-02 15:23:20264 return result;
ivica5d6a06c2015-09-17 12:30:24265 }
266
267 bool SendRtcp(const uint8_t* packet, size_t length) override {
268 return transport_->SendRtcp(packet, length);
269 }
270
271 void EncodedFrameCallback(const EncodedFrame& frame) override {
272 rtc::CritScope lock(&comparison_lock_);
273 if (frames_recorded_ < frames_to_process_)
274 encoded_frame_size_.AddSample(frame.length_);
275 }
276
nisseeb83a1a2016-03-21 08:27:56277 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 12:30:24278 int64_t render_time_ms =
279 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 12:30:24280
281 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 18:41:03282 int64_t send_timestamp =
sprang16daaa52016-03-09 09:30:24283 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 12:30:24284
sprang16daaa52016-03-09 09:30:24285 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 16:44:40286 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 13:35:56287 // No previous frame rendered, this one was dropped after sending but
288 // before rendering.
289 ++dropped_frames_before_rendering_;
290 frames_.pop_front();
291 RTC_CHECK(!frames_.empty());
292 continue;
293 }
nisse97f0b932016-05-26 16:44:40294 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
ivica5d6a06c2015-09-17 12:30:24295 render_time_ms);
296 frames_.pop_front();
pbos14fe7082016-04-20 13:35:56297 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 12:30:24298 }
299
300 VideoFrame reference_frame = frames_.front();
301 frames_.pop_front();
sprang16daaa52016-03-09 09:30:24302 int64_t reference_timestamp =
303 wrap_handler_.Unwrap(reference_frame.timestamp());
304 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 15:23:20305 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 10:13:28306 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 15:23:20307 ++send_timestamp;
308 }
sprang16daaa52016-03-09 09:30:24309 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 12:30:24310
311 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
312
nisse97f0b932016-05-26 16:44:40313 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ivica5d6a06c2015-09-17 12:30:24314 }
315
ivica5d6a06c2015-09-17 12:30:24316 void Wait() {
317 // Frame comparisons can be very expensive. Wait for test to be done, but
318 // at time-out check if frames_processed is going up. If so, give it more
319 // time, otherwise fail. Hopefully this will reduce test flakiness.
320
Peter Boström8c38e8b2015-11-26 16:45:47321 stats_polling_thread_.Start();
sprangce4aef12015-11-02 15:23:20322
ivica5d6a06c2015-09-17 12:30:24323 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 12:30:24324 int iteration = 0;
Peter Boström5811a392015-12-10 12:02:50325 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 12:30:24326 int frames_processed;
327 {
328 rtc::CritScope crit(&comparison_lock_);
329 frames_processed = frames_processed_;
330 }
331
332 // Print some output so test infrastructure won't think we've crashed.
333 const char* kKeepAliveMessages[3] = {
334 "Uh, I'm-I'm not quite dead, sir.",
335 "Uh, I-I think uh, I could pull through, sir.",
336 "Actually, I think I'm all right to come with you--"};
337 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
338
339 if (last_frames_processed == -1) {
340 last_frames_processed = frames_processed;
341 continue;
342 }
Peter Boströmdd45eb62016-01-19 14:22:32343 if (frames_processed == last_frames_processed) {
344 EXPECT_GT(frames_processed, last_frames_processed)
345 << "Analyzer stalled while waiting for test to finish.";
346 done_.Set();
347 break;
348 }
ivica5d6a06c2015-09-17 12:30:24349 last_frames_processed = frames_processed;
350 }
351
352 if (iteration > 0)
353 printf("- Farewell, sweet Concorde!\n");
354
Peter Boström8c38e8b2015-11-26 16:45:47355 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 12:30:24356 }
357
pbos14fe7082016-04-20 13:35:56358 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
359 return &pre_encode_proxy_;
360 }
Peter Boströme4499152016-02-05 10:13:28361 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
362
sprangce4aef12015-11-02 15:23:20363 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 12:30:24364 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 12:30:24365
366 private:
367 struct FrameComparison {
368 FrameComparison()
369 : dropped(false),
370 send_time_ms(0),
371 recv_time_ms(0),
372 render_time_ms(0),
373 encoded_frame_size(0) {}
374
375 FrameComparison(const VideoFrame& reference,
376 const VideoFrame& render,
377 bool dropped,
378 int64_t send_time_ms,
379 int64_t recv_time_ms,
380 int64_t render_time_ms,
381 size_t encoded_frame_size)
382 : reference(reference),
383 render(render),
384 dropped(dropped),
385 send_time_ms(send_time_ms),
386 recv_time_ms(recv_time_ms),
387 render_time_ms(render_time_ms),
388 encoded_frame_size(encoded_frame_size) {}
389
390 VideoFrame reference;
391 VideoFrame render;
392 bool dropped;
393 int64_t send_time_ms;
394 int64_t recv_time_ms;
395 int64_t render_time_ms;
396 size_t encoded_frame_size;
397 };
398
399 struct Sample {
ivica8d15bd62015-10-07 09:43:12400 Sample(int dropped,
401 int64_t input_time_ms,
402 int64_t send_time_ms,
403 int64_t recv_time_ms,
404 int64_t render_time_ms,
405 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 12:30:24406 double psnr,
ivica8d15bd62015-10-07 09:43:12407 double ssim)
ivica5d6a06c2015-09-17 12:30:24408 : dropped(dropped),
409 input_time_ms(input_time_ms),
410 send_time_ms(send_time_ms),
411 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 09:43:12412 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 12:30:24413 encoded_frame_size(encoded_frame_size),
414 psnr(psnr),
ivica8d15bd62015-10-07 09:43:12415 ssim(ssim) {}
ivica5d6a06c2015-09-17 12:30:24416
ivica8d15bd62015-10-07 09:43:12417 int dropped;
418 int64_t input_time_ms;
419 int64_t send_time_ms;
420 int64_t recv_time_ms;
421 int64_t render_time_ms;
422 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 12:30:24423 double psnr;
424 double ssim;
ivica5d6a06c2015-09-17 12:30:24425 };
426
Peter Boströme4499152016-02-05 10:13:28427 // This class receives the send-side OnEncodeTiming and is provided to not
428 // conflict with the receiver-side pre_decode_callback.
429 class OnEncodeTimingProxy : public EncodedFrameObserver {
430 public:
431 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
432
433 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
434 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
435 }
436 void EncodedFrameCallback(const EncodedFrame& frame) override {}
437
438 private:
439 VideoAnalyzer* const parent_;
440 };
441
pbos14fe7082016-04-20 13:35:56442 // This class receives the send-side OnFrame callback and is provided to not
443 // conflict with the receiver-side renderer callback.
444 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
445 public:
446 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
447
448 void OnFrame(const VideoFrame& video_frame) override {
449 parent_->PreEncodeOnFrame(video_frame);
450 }
451
452 private:
453 VideoAnalyzer* const parent_;
454 };
455
ivica5d6a06c2015-09-17 12:30:24456 void AddFrameComparison(const VideoFrame& reference,
457 const VideoFrame& render,
458 bool dropped,
459 int64_t render_time_ms)
460 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 10:04:52461 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
462 int64_t send_time_ms = send_times_[reference_timestamp];
463 send_times_.erase(reference_timestamp);
464 int64_t recv_time_ms = recv_times_[reference_timestamp];
465 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 12:30:24466
sprangce4aef12015-11-02 15:23:20467 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 10:04:52468 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 15:23:20469 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 10:04:52470 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 15:23:20471 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
472 if (it != encoded_frame_sizes_.end())
473 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 12:30:24474
475 VideoFrame reference_copy;
476 VideoFrame render_copy;
ivica5d6a06c2015-09-17 12:30:24477
478 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 14:00:57479 if (comparisons_.size() < kMaxComparisons) {
nisse67dca9f2016-10-31 15:05:50480 reference_copy = reference;
481 render_copy = render;
stefanb1797672016-08-11 14:00:57482 } else {
483 // Copy the time to ensure that delay calculations can still be made.
484 reference_copy.set_ntp_time_ms(reference.ntp_time_ms());
485 render_copy.set_ntp_time_ms(render.ntp_time_ms());
486 }
ivica5d6a06c2015-09-17 12:30:24487 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
488 send_time_ms, recv_time_ms,
489 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 12:02:50490 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24491 }
492
493 static bool PollStatsThread(void* obj) {
494 return static_cast<VideoAnalyzer*>(obj)->PollStats();
495 }
496
497 bool PollStats() {
Peter Boströmdd45eb62016-01-19 14:22:32498 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 12:02:50499 return false;
ivica5d6a06c2015-09-17 12:30:24500
501 VideoSendStream::Stats stats = send_stream_->GetStats();
502
503 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 16:30:49504 // It's not certain that we yet have estimates for any of these stats. Check
505 // that they are positive before mixing them in.
506 if (stats.encode_frame_rate > 0)
507 encode_frame_rate_.AddSample(stats.encode_frame_rate);
508 if (stats.avg_encode_time_ms > 0)
509 encode_time_ms.AddSample(stats.avg_encode_time_ms);
510 if (stats.encode_usage_percent > 0)
511 encode_usage_percent.AddSample(stats.encode_usage_percent);
512 if (stats.media_bitrate_bps > 0)
513 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 12:30:24514
515 return true;
516 }
517
518 static bool FrameComparisonThread(void* obj) {
519 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
520 }
521
522 bool CompareFrames() {
523 if (AllFramesRecorded())
524 return false;
525
526 VideoFrame reference;
527 VideoFrame render;
528 FrameComparison comparison;
529
530 if (!PopComparison(&comparison)) {
531 // Wait until new comparison task is available, or test is done.
532 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 12:02:50533 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 12:30:24534 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 12:02:50535 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24536 return false;
537 }
538 return true; // Try again.
539 }
540
541 PerformFrameComparison(comparison);
542
543 if (FrameProcessed()) {
544 PrintResults();
545 if (graph_data_output_file_)
546 PrintSamplesToFile();
Peter Boström5811a392015-12-10 12:02:50547 done_.Set();
548 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 12:30:24549 return false;
550 }
551
552 return true;
553 }
554
555 bool PopComparison(FrameComparison* comparison) {
556 rtc::CritScope crit(&comparison_lock_);
557 // If AllFramesRecorded() is true, it means we have already popped
558 // frames_to_process_ frames from comparisons_, so there is no more work
559 // for this thread to be done. frames_processed_ might still be lower if
560 // all comparisons are not done, but those frames are currently being
561 // worked on by other threads.
562 if (comparisons_.empty() || AllFramesRecorded())
563 return false;
564
565 *comparison = comparisons_.front();
566 comparisons_.pop_front();
567
568 FrameRecorded();
569 return true;
570 }
571
572 // Increment counter for number of frames received for comparison.
573 void FrameRecorded() {
574 rtc::CritScope crit(&comparison_lock_);
575 ++frames_recorded_;
576 }
577
578 // Returns true if all frames to be compared have been taken from the queue.
579 bool AllFramesRecorded() {
580 rtc::CritScope crit(&comparison_lock_);
581 assert(frames_recorded_ <= frames_to_process_);
582 return frames_recorded_ == frames_to_process_;
583 }
584
585 // Increase count of number of frames processed. Returns true if this was the
586 // last frame to be processed.
587 bool FrameProcessed() {
588 rtc::CritScope crit(&comparison_lock_);
589 ++frames_processed_;
590 assert(frames_processed_ <= frames_to_process_);
591 return frames_processed_ == frames_to_process_;
592 }
593
594 void PrintResults() {
595 rtc::CritScope crit(&comparison_lock_);
596 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 19:21:51597 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 12:30:24598 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 12:30:24599 PrintResult("receiver_time", receiver_time_, " ms");
600 PrintResult("total_delay_incl_network", end_to_end_, " ms");
601 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
602 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
603 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
604 PrintResult("encode_time", encode_time_ms, " ms");
605 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
606 PrintResult("media_bitrate", media_bitrate_bps, " bps");
607
pbos14fe7082016-04-20 13:35:56608 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
609 dropped_frames_);
610 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
611 test_label_.c_str(), dropped_frames_before_first_encode_);
612 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
613 test_label_.c_str(), dropped_frames_before_rendering_);
614
ivica5d6a06c2015-09-17 12:30:24615 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
616 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
617 }
618
619 void PerformFrameComparison(const FrameComparison& comparison) {
620 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 14:00:57621 double psnr = -1.0;
622 double ssim = -1.0;
623 if (!comparison.reference.IsZeroSize()) {
624 psnr = I420PSNR(&comparison.reference, &comparison.render);
625 ssim = I420SSIM(&comparison.reference, &comparison.render);
626 }
ivica5d6a06c2015-09-17 12:30:24627
628 int64_t input_time_ms = comparison.reference.ntp_time_ms();
629
630 rtc::CritScope crit(&comparison_lock_);
631 if (graph_data_output_file_) {
632 samples_.push_back(
633 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 09:43:12634 comparison.recv_time_ms, comparison.render_time_ms,
635 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 12:30:24636 }
stefanb1797672016-08-11 14:00:57637 if (psnr >= 0.0)
638 psnr_.AddSample(psnr);
639 if (ssim >= 0.0)
640 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 12:30:24641
642 if (comparison.dropped) {
643 ++dropped_frames_;
644 return;
645 }
646 if (last_render_time_ != 0)
647 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
648 last_render_time_ = comparison.render_time_ms;
649
650 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
651 receiver_time_.AddSample(comparison.render_time_ms -
652 comparison.recv_time_ms);
653 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
654 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
655 }
656
657 void PrintResult(const char* result_type,
658 test::Statistics stats,
659 const char* unit) {
660 printf("RESULT %s: %s = {%f, %f}%s\n",
661 result_type,
662 test_label_.c_str(),
663 stats.Mean(),
664 stats.StandardDeviation(),
665 unit);
666 }
667
668 void PrintSamplesToFile(void) {
669 FILE* out = graph_data_output_file_;
670 rtc::CritScope crit(&comparison_lock_);
671 std::sort(samples_.begin(), samples_.end(),
672 [](const Sample& A, const Sample& B) -> bool {
673 return A.input_time_ms < B.input_time_ms;
674 });
675
sprangce4aef12015-11-02 15:23:20676 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 12:30:24677 fprintf(out, "%" PRIuS "\n", samples_.size());
678 fprintf(out,
679 "dropped "
680 "input_time_ms "
681 "send_time_ms "
682 "recv_time_ms "
ivica8d15bd62015-10-07 09:43:12683 "render_time_ms "
ivica5d6a06c2015-09-17 12:30:24684 "encoded_frame_size "
685 "psnr "
686 "ssim "
ivica8d15bd62015-10-07 09:43:12687 "encode_time_ms\n");
688 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 12:30:24689 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 09:43:12690 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
691 int encode_time_ms;
692 if (it != samples_encode_time_ms_.end()) {
693 encode_time_ms = it->second;
694 } else {
695 ++missing_encode_time_samples;
696 encode_time_ms = -1;
697 }
698 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
699 " %lf %lf %d\n",
700 sample.dropped, sample.input_time_ms, sample.send_time_ms,
701 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 12:30:24702 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 09:43:12703 encode_time_ms);
704 }
705 if (missing_encode_time_samples) {
706 fprintf(stderr,
707 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
708 missing_encode_time_samples);
ivica5d6a06c2015-09-17 12:30:24709 }
710 }
711
perkja49cbd32016-09-16 14:53:41712 // Implements VideoSinkInterface to receive captured frames from a
713 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
714 // as a source to VideoSendStream.
715 // It forwards all input frames to the VideoAnalyzer for later comparison and
716 // forwards the captured frames to the VideoSendStream.
717 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
718 public rtc::VideoSourceInterface<VideoFrame> {
719 public:
720 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
721 : analyzer_(analyzer), send_stream_input_(nullptr) {}
722
723 private:
724 void OnFrame(const VideoFrame& video_frame) override {
725 VideoFrame copy = video_frame;
726 copy.set_timestamp(copy.ntp_time_ms() * 90);
727
728 analyzer_->AddCapturedFrameForComparison(video_frame);
729 rtc::CritScope lock(&crit_);
730 if (send_stream_input_)
731 send_stream_input_->OnFrame(video_frame);
732 }
733
734 // Called when |send_stream_.SetSource()| is called.
735 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
736 const rtc::VideoSinkWants& wants) override {
737 rtc::CritScope lock(&crit_);
738 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
739 send_stream_input_ = sink;
740 }
741
742 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
743 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
744 rtc::CritScope lock(&crit_);
745 RTC_DCHECK(sink == send_stream_input_);
746 send_stream_input_ = nullptr;
747 }
748
749 VideoAnalyzer* const analyzer_;
750 rtc::CriticalSection crit_;
751 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
752 };
753
754 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
755 rtc::CritScope lock(&crit_);
756 RTC_DCHECK_EQ(0u, video_frame.timestamp());
757 // Frames from the capturer does not have a rtp timestamp. Create one so it
758 // can be used for comparison.
759 VideoFrame copy = video_frame;
760 copy.set_timestamp(copy.ntp_time_ms() * 90);
761 frames_.push_back(copy);
762 }
763
764 VideoSendStream* send_stream_;
765 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 12:30:24766 const std::string test_label_;
767 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 15:23:20768 const std::string graph_title_;
769 const uint32_t ssrc_to_analyze_;
pbos14fe7082016-04-20 13:35:56770 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 10:13:28771 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 12:30:24772 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 09:43:12773 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 12:30:24774 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
775 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
776 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
777 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
778 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
779 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
780 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
781 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
782 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
783 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
784 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
785
786 const int frames_to_process_;
787 int frames_recorded_;
788 int frames_processed_;
789 int dropped_frames_;
pbos14fe7082016-04-20 13:35:56790 int dropped_frames_before_first_encode_;
791 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 12:30:24792 int64_t last_render_time_;
793 uint32_t rtp_timestamp_delta_;
794
795 rtc::CriticalSection crit_;
796 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 16:44:40797 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 10:04:52798 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
799 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
800 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
801 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 10:25:36802 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 12:30:24803 const double avg_psnr_threshold_;
804 const double avg_ssim_threshold_;
805
806 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 16:45:47807 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
808 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 12:02:50809 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 12:30:24810 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 12:02:50811 rtc::Event done_;
ivica5d6a06c2015-09-17 12:30:24812};
813
palmkviste75f2042016-09-28 13:19:48814VideoQualityTest::VideoQualityTest()
815 : clock_(Clock::GetRealTimeClock()), receive_logs_(0), send_logs_(0) {}
ivica5d6a06c2015-09-17 12:30:24816
minyue626bc952016-10-31 12:47:02817VideoQualityTest::Params::Params()
818 : call({false, Call::Config::BitrateConfig()}),
819 video({false, 640, 480, 30, 50, 800, 800, false, "VP8", 1, -1, 0, false,
brandtr1293aca2016-11-17 06:47:29820 false, "", ""}),
minyue626bc952016-10-31 12:47:02821 audio({false, false}),
822 screenshare({false, 10, 0}),
823 analyzer({"", 0.0, 0.0, 0, "", ""}),
824 pipe(),
825 logs(false),
826 ss({std::vector<VideoStream>(), 0, 0, -1, std::vector<SpatialLayer>()}) {}
827
828VideoQualityTest::Params::~Params() = default;
829
ivica5d6a06c2015-09-17 12:30:24830void VideoQualityTest::TestBody() {}
831
sprangce4aef12015-11-02 15:23:20832std::string VideoQualityTest::GenerateGraphTitle() const {
833 std::stringstream ss;
minyue626bc952016-10-31 12:47:02834 ss << params_.video.codec;
835 ss << " (" << params_.video.target_bitrate_bps / 1000 << "kbps";
836 ss << ", " << params_.video.fps << " FPS";
sprangce4aef12015-11-02 15:23:20837 if (params_.screenshare.scroll_duration)
838 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
839 if (params_.ss.streams.size() > 1)
840 ss << ", Stream #" << params_.ss.selected_stream;
841 if (params_.ss.num_spatial_layers > 1)
842 ss << ", Layer #" << params_.ss.selected_sl;
843 ss << ")";
844 return ss.str();
845}
846
847void VideoQualityTest::CheckParams() {
848 // Add a default stream in none specified.
849 if (params_.ss.streams.empty())
850 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
851 if (params_.ss.num_spatial_layers == 0)
852 params_.ss.num_spatial_layers = 1;
853
854 if (params_.pipe.loss_percent != 0 ||
855 params_.pipe.queue_length_packets != 0) {
856 // Since LayerFilteringTransport changes the sequence numbers, we can't
857 // use that feature with pack loss, since the NACK request would end up
858 // retransmitting the wrong packets.
859 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 14:10:23860 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
minyue626bc952016-10-31 12:47:02861 RTC_CHECK(params_.video.selected_tl == -1 ||
862 params_.video.selected_tl ==
863 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 15:23:20864 }
865
866 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
867 // does in some parts of the code?
minyue626bc952016-10-31 12:47:02868 RTC_CHECK_GE(params_.video.max_bitrate_bps, params_.video.target_bitrate_bps);
869 RTC_CHECK_GE(params_.video.target_bitrate_bps, params_.video.min_bitrate_bps);
870 RTC_CHECK_LT(params_.video.selected_tl, params_.video.num_temporal_layers);
sprangce4aef12015-11-02 15:23:20871 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
872 for (const VideoStream& stream : params_.ss.streams) {
873 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
874 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
875 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
876 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
minyue626bc952016-10-31 12:47:02877 params_.video.num_temporal_layers - 1);
sprangce4aef12015-11-02 15:23:20878 }
879 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
880 // the total bitrate? We anyway have to update them in the case bitrate
881 // estimator changes the total bitrates.
882 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
883 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
884 RTC_CHECK(params_.ss.spatial_layers.empty() ||
885 params_.ss.spatial_layers.size() ==
886 static_cast<size_t>(params_.ss.num_spatial_layers));
minyue626bc952016-10-31 12:47:02887 if (params_.video.codec == "VP8") {
sprangce4aef12015-11-02 15:23:20888 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
minyue626bc952016-10-31 12:47:02889 } else if (params_.video.codec == "VP9") {
sprangce4aef12015-11-02 15:23:20890 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
891 }
892}
893
894// Static.
895std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
896 // Parse comma separated nonnegative integers, where some elements may be
897 // empty. The empty values are replaced with -1.
898 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
899 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
900 std::vector<int> result;
901 if (str.empty())
902 return result;
903
904 const char* p = str.c_str();
905 int value = -1;
906 int pos;
907 while (*p) {
908 if (*p == ',') {
909 result.push_back(value);
910 value = -1;
911 ++p;
912 continue;
913 }
914 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
915 << "Unexpected non-number value.";
916 p += pos;
917 }
918 result.push_back(value);
919 return result;
920}
921
922// Static.
923VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
924 VideoStream stream;
minyue626bc952016-10-31 12:47:02925 stream.width = params.video.width;
926 stream.height = params.video.height;
927 stream.max_framerate = params.video.fps;
928 stream.min_bitrate_bps = params.video.min_bitrate_bps;
929 stream.target_bitrate_bps = params.video.target_bitrate_bps;
930 stream.max_bitrate_bps = params.video.max_bitrate_bps;
sprangce4aef12015-11-02 15:23:20931 stream.max_qp = 52;
minyue626bc952016-10-31 12:47:02932 if (params.video.num_temporal_layers == 2)
sprangce4aef12015-11-02 15:23:20933 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
934 return stream;
935}
936
937// Static.
938void VideoQualityTest::FillScalabilitySettings(
939 Params* params,
940 const std::vector<std::string>& stream_descriptors,
941 size_t selected_stream,
942 int num_spatial_layers,
943 int selected_sl,
944 const std::vector<std::string>& sl_descriptors) {
945 // Read VideoStream and SpatialLayer elements from a list of comma separated
946 // lists. To use a default value for an element, use -1 or leave empty.
947 // Validity checks performed in CheckParams.
948
949 RTC_CHECK(params->ss.streams.empty());
950 for (auto descriptor : stream_descriptors) {
951 if (descriptor.empty())
952 continue;
953 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
954 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
955 if (v[0] != -1)
956 stream.width = static_cast<size_t>(v[0]);
957 if (v[1] != -1)
958 stream.height = static_cast<size_t>(v[1]);
959 if (v[2] != -1)
960 stream.max_framerate = v[2];
961 if (v[3] != -1)
962 stream.min_bitrate_bps = v[3];
963 if (v[4] != -1)
964 stream.target_bitrate_bps = v[4];
965 if (v[5] != -1)
966 stream.max_bitrate_bps = v[5];
967 if (v.size() > 6 && v[6] != -1)
968 stream.max_qp = v[6];
969 if (v.size() > 7) {
970 stream.temporal_layer_thresholds_bps.clear();
971 stream.temporal_layer_thresholds_bps.insert(
972 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
973 } else {
974 // Automatic TL thresholds for more than two layers not supported.
minyue626bc952016-10-31 12:47:02975 RTC_CHECK_LE(params->video.num_temporal_layers, 2);
sprangce4aef12015-11-02 15:23:20976 }
977 params->ss.streams.push_back(stream);
978 }
979 params->ss.selected_stream = selected_stream;
980
981 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
982 params->ss.selected_sl = selected_sl;
983 RTC_CHECK(params->ss.spatial_layers.empty());
984 for (auto descriptor : sl_descriptors) {
985 if (descriptor.empty())
986 continue;
987 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
988 RTC_CHECK_GT(v[2], 0);
989
990 SpatialLayer layer;
991 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
992 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
993 layer.target_bitrate_bps = v[2];
994 params->ss.spatial_layers.push_back(layer);
995 }
996}
997
minyuea27172d2016-11-01 12:59:29998void VideoQualityTest::SetupVideo(Transport* send_transport,
999 Transport* recv_transport) {
sprangce4aef12015-11-02 15:23:201000 if (params_.logs)
ivica5d6a06c2015-09-17 12:30:241001 trace_to_stderr_.reset(new test::TraceToStderr);
1002
sprangce4aef12015-11-02 15:23:201003 size_t num_streams = params_.ss.streams.size();
brandtr841de6a2016-11-15 15:10:521004 CreateSendConfig(num_streams, 0, 0, send_transport);
ivica5d6a06c2015-09-17 12:30:241005
1006 int payload_type;
minyue626bc952016-10-31 12:47:021007 if (params_.video.codec == "H264") {
minyuea27172d2016-11-01 12:59:291008 video_encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
hbosbab934b2016-01-27 09:36:031009 payload_type = kPayloadTypeH264;
minyue626bc952016-10-31 12:47:021010 } else if (params_.video.codec == "VP8") {
minyuea27172d2016-11-01 12:59:291011 video_encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
ivica5d6a06c2015-09-17 12:30:241012 payload_type = kPayloadTypeVP8;
minyue626bc952016-10-31 12:47:021013 } else if (params_.video.codec == "VP9") {
minyuea27172d2016-11-01 12:59:291014 video_encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
ivica5d6a06c2015-09-17 12:30:241015 payload_type = kPayloadTypeVP9;
1016 } else {
1017 RTC_NOTREACHED() << "Codec not supported!";
1018 return;
1019 }
minyuea27172d2016-11-01 12:59:291020 video_send_config_.encoder_settings.encoder = video_encoder_.get();
minyue626bc952016-10-31 12:47:021021 video_send_config_.encoder_settings.payload_name = params_.video.codec;
stefanff483612015-12-21 11:14:001022 video_send_config_.encoder_settings.payload_type = payload_type;
1023 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
1024 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 15:23:201025 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 11:14:001026 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 12:30:241027
stefanff483612015-12-21 11:14:001028 video_send_config_.rtp.extensions.clear();
minyue626bc952016-10-31 12:47:021029 if (params_.call.send_side_bwe) {
stefanff483612015-12-21 11:14:001030 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 18:24:551031 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 14:13:241032 test::kTransportSequenceNumberExtensionId));
1033 } else {
stefanff483612015-12-21 11:14:001034 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 18:24:551035 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 13:06:571036 }
1037
stefanff483612015-12-21 11:14:001038 video_encoder_config_.min_transmit_bitrate_bps =
minyue626bc952016-10-31 12:47:021039 params_.video.min_transmit_bps;
perkjfa10b552016-10-03 06:45:261040
brandtr1293aca2016-11-17 06:47:291041 video_send_config_.suspend_below_min_bitrate =
1042 params_.video.suspend_below_min_bitrate;
1043
perkjfa10b552016-10-03 06:45:261044 video_encoder_config_.number_of_streams = params_.ss.streams.size();
1045 video_encoder_config_.max_bitrate_bps = 0;
1046 for (size_t i = 0; i < params_.ss.streams.size(); ++i) {
1047 video_encoder_config_.max_bitrate_bps +=
1048 params_.ss.streams[i].max_bitrate_bps;
1049 }
1050 video_encoder_config_.video_stream_factory =
1051 new rtc::RefCountedObject<VideoStreamFactory>(params_.ss.streams);
1052
stefanff483612015-12-21 11:14:001053 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 12:30:241054
1055 CreateMatchingReceiveConfigs(recv_transport);
1056
sprangce4aef12015-11-02 15:23:201057 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 11:14:001058 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 12:29:591059 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
1060 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 15:23:201061 kSendRtxPayloadType;
minyue626bc952016-10-31 12:47:021062 video_receive_configs_[i].rtp.transport_cc = params_.call.send_side_bwe;
sprangce4aef12015-11-02 15:23:201063 }
brandtr1293aca2016-11-17 06:47:291064
1065 if (params_.video.flexfec) {
1066 video_send_config_.rtp.flexfec.flexfec_payload_type = kFlexfecPayloadType;
1067 video_send_config_.rtp.flexfec.flexfec_ssrc = kFlexfecSendSsrc;
1068 video_send_config_.rtp.flexfec.protected_media_ssrcs = {
1069 kVideoSendSsrcs[params_.ss.selected_stream]};
1070
1071 FlexfecReceiveStream::Config flexfec_receive_config;
1072 flexfec_receive_config.flexfec_payload_type =
1073 video_send_config_.rtp.flexfec.flexfec_payload_type;
1074 flexfec_receive_config.flexfec_ssrc =
1075 video_send_config_.rtp.flexfec.flexfec_ssrc;
1076 flexfec_receive_config.protected_media_ssrcs =
1077 video_send_config_.rtp.flexfec.protected_media_ssrcs;
1078 flexfec_receive_configs_.push_back(flexfec_receive_config);
1079 }
1080
1081 if (params_.video.ulpfec) {
1082 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
1083 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
1084 video_send_config_.rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
1085
1086 video_receive_configs_[params_.ss.selected_stream]
1087 .rtp.ulpfec.red_payload_type =
1088 video_send_config_.rtp.ulpfec.red_payload_type;
1089 video_receive_configs_[params_.ss.selected_stream]
1090 .rtp.ulpfec.ulpfec_payload_type =
1091 video_send_config_.rtp.ulpfec.ulpfec_payload_type;
1092 video_receive_configs_[params_.ss.selected_stream]
1093 .rtp.ulpfec.red_rtx_payload_type =
1094 video_send_config_.rtp.ulpfec.red_rtx_payload_type;
1095 }
ivica5d6a06c2015-09-17 12:30:241096}
1097
sprangce4aef12015-11-02 15:23:201098void VideoQualityTest::SetupScreenshare() {
1099 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 12:30:241100
1101 // Fill out codec settings.
stefanff483612015-12-21 11:14:001102 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
minyue626bc952016-10-31 12:47:021103 if (params_.video.codec == "VP8") {
kthelgason29a44e32016-09-27 10:52:021104 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1105 vp8_settings.denoisingOn = false;
1106 vp8_settings.frameDroppingOn = false;
1107 vp8_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 12:47:021108 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 10:52:021109 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1110 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
minyue626bc952016-10-31 12:47:021111 } else if (params_.video.codec == "VP9") {
kthelgason29a44e32016-09-27 10:52:021112 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1113 vp9_settings.denoisingOn = false;
1114 vp9_settings.frameDroppingOn = false;
1115 vp9_settings.numberOfTemporalLayers =
minyue626bc952016-10-31 12:47:021116 static_cast<unsigned char>(params_.video.num_temporal_layers);
kthelgason29a44e32016-09-27 10:52:021117 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 15:23:201118 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 10:52:021119 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1120 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 12:30:241121 }
1122
1123 // Setup frame generator.
1124 const size_t kWidth = 1850;
1125 const size_t kHeight = 1110;
1126 std::vector<std::string> slides;
1127 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1128 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1129 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1130 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1131
sprangce4aef12015-11-02 15:23:201132 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 12:30:241133 // Cycle image every slide_change_interval seconds.
1134 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
1135 slides, kWidth, kHeight,
minyue626bc952016-10-31 12:47:021136 params_.screenshare.slide_change_interval * params_.video.fps));
ivica5d6a06c2015-09-17 12:30:241137 } else {
minyue626bc952016-10-31 12:47:021138 RTC_CHECK_LE(params_.video.width, kWidth);
1139 RTC_CHECK_LE(params_.video.height, kHeight);
sprangce4aef12015-11-02 15:23:201140 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1141 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1142 params_.screenshare.scroll_duration) *
1143 1000;
1144 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1145 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 12:30:241146
sprangce4aef12015-11-02 15:23:201147 frame_generator_.reset(
1148 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
minyue626bc952016-10-31 12:47:021149 clock_, slides, kWidth, kHeight, params_.video.width,
1150 params_.video.height, params_.screenshare.scroll_duration * 1000,
sprangce4aef12015-11-02 15:23:201151 kPauseDurationMs));
ivica5d6a06c2015-09-17 12:30:241152 }
1153}
1154
perkja49cbd32016-09-16 14:53:411155void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 15:23:201156 if (params_.screenshare.enabled) {
1157 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja49cbd32016-09-16 14:53:411158 new test::FrameGeneratorCapturer(clock_, frame_generator_.release(),
minyue626bc952016-10-31 12:47:021159 params_.video.fps);
ivica2d4e6c52015-09-23 08:57:061160 EXPECT_TRUE(frame_generator_capturer->Init());
minyuea27172d2016-11-01 12:59:291161 video_capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 12:30:241162 } else {
sprangce4aef12015-11-02 15:23:201163 if (params_.video.clip_name.empty()) {
minyuea27172d2016-11-01 12:59:291164 video_capturer_.reset(test::VcmCapturer::Create(
minyue626bc952016-10-31 12:47:021165 params_.video.width, params_.video.height, params_.video.fps));
ivica5d6a06c2015-09-17 12:30:241166 } else {
minyuea27172d2016-11-01 12:59:291167 video_capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 14:53:411168 test::ResourcePath(params_.video.clip_name, "yuv"),
minyue626bc952016-10-31 12:47:021169 params_.video.width, params_.video.height, params_.video.fps,
ivica2d4e6c52015-09-23 08:57:061170 clock_));
minyuea27172d2016-11-01 12:59:291171 ASSERT_TRUE(video_capturer_) << "Could not create capturer for "
1172 << params_.video.clip_name
1173 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 12:30:241174 }
1175 }
1176}
1177
sprang7a975f72015-10-12 13:33:211178void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 15:23:201179 params_ = params;
1180
minyue626bc952016-10-31 12:47:021181 RTC_CHECK(!params_.audio.enabled);
ivica5d6a06c2015-09-17 12:30:241182 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1183 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 15:23:201184 CheckParams();
ivica5d6a06c2015-09-17 12:30:241185
1186 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 15:23:201187 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 12:30:241188 graph_data_output_file =
sprangce4aef12015-11-02 15:23:201189 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 15:56:101190 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 15:23:201191 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1192 << "!";
ivica87f83a92015-10-08 12:13:321193 }
sprang7a975f72015-10-12 13:33:211194
skvlad11a9cbf2016-10-07 18:53:051195 webrtc::RtcEventLogNullImpl event_log;
1196 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 12:47:021197 call_config.bitrate_config = params.call.call_bitrate_config;
stefanf116bd02015-10-27 15:29:421198 CreateCalls(call_config, call_config);
1199
ivica87f83a92015-10-08 12:13:321200 test::LayerFilteringTransport send_transport(
minyue626bc952016-10-31 12:47:021201 params_.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1202 params_.video.selected_tl, params_.ss.selected_sl);
1203 test::DirectTransport recv_transport(params_.pipe, receiver_call_.get());
stefanf116bd02015-10-27 15:29:421204
sprangce4aef12015-11-02 15:23:201205 std::string graph_title = params_.analyzer.graph_title;
1206 if (graph_title.empty())
1207 graph_title = VideoQualityTest::GenerateGraphTitle();
1208
1209 // In the case of different resolutions, the functions calculating PSNR and
1210 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1211 // aborts if the average psnr/ssim are below the given threshold, which is
1212 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1213 // abort.
1214 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1215 int selected_sl = params_.ss.selected_sl != -1
1216 ? params_.ss.selected_sl
1217 : params_.ss.num_spatial_layers - 1;
1218 bool disable_quality_check =
minyue626bc952016-10-31 12:47:021219 selected_stream.width != params_.video.width ||
1220 selected_stream.height != params_.video.height ||
sprangce4aef12015-11-02 15:23:201221 (!params_.ss.spatial_layers.empty() &&
1222 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1223 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1224 if (disable_quality_check) {
1225 fprintf(stderr,
1226 "Warning: Calculating PSNR and SSIM for downsized resolution "
1227 "not implemented yet! Skipping PSNR and SSIM calculations!");
1228 }
1229
ivica5d6a06c2015-09-17 12:30:241230 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 15:23:201231 &send_transport, params_.analyzer.test_label,
1232 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1233 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
minyue626bc952016-10-31 12:47:021234 params_.analyzer.test_durations_secs * params_.video.fps,
sprangce4aef12015-11-02 15:23:201235 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 16:43:181236 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 12:30:241237
ivica5d6a06c2015-09-17 12:30:241238 analyzer.SetReceiver(receiver_call_->Receiver());
1239 send_transport.SetReceiver(&analyzer);
1240 recv_transport.SetReceiver(sender_call_->Receiver());
1241
minyuea27172d2016-11-01 12:59:291242 SetupVideo(&analyzer, &recv_transport);
stefanff483612015-12-21 11:14:001243 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 13:35:561244 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
stefanff483612015-12-21 11:14:001245 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 12:30:241246 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 10:13:281247 RTC_DCHECK(!video_send_config_.post_encode_callback);
1248 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 12:30:241249
sprangce4aef12015-11-02 15:23:201250 if (params_.screenshare.enabled)
1251 SetupScreenshare();
ivica5d6a06c2015-09-17 12:30:241252
brandtr1293aca2016-11-17 06:47:291253 CreateFlexfecStreams();
Stefan Holmer9fea80f2016-01-07 16:43:181254 CreateVideoStreams();
perkja49cbd32016-09-16 14:53:411255 analyzer.SetSendStream(video_send_stream_);
perkj803d97f2016-11-01 18:45:461256 video_send_stream_->SetSource(
1257 analyzer.OutputInterface(),
1258 VideoSendStream::DegradationPreference::kBalanced);
ivica5d6a06c2015-09-17 12:30:241259
perkja49cbd32016-09-16 14:53:411260 CreateCapturer();
1261 rtc::VideoSinkWants wants;
minyuea27172d2016-11-01 12:59:291262 video_capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 10:44:061263
palmkviste75f2042016-09-28 13:19:481264 StartEncodedFrameLogs(video_send_stream_);
1265 StartEncodedFrameLogs(video_receive_streams_[0]);
stefanff483612015-12-21 11:14:001266 video_send_stream_->Start();
1267 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1268 receive_stream->Start();
brandtr1293aca2016-11-17 06:47:291269 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1270 receive_stream->Start();
minyuea27172d2016-11-01 12:59:291271 video_capturer_->Start();
ivica5d6a06c2015-09-17 12:30:241272
1273 analyzer.Wait();
1274
1275 send_transport.StopSending();
1276 recv_transport.StopSending();
1277
minyuea27172d2016-11-01 12:59:291278 video_capturer_->Stop();
brandtr1293aca2016-11-17 06:47:291279 for (FlexfecReceiveStream* receive_stream : flexfec_receive_streams_)
1280 receive_stream->Stop();
stefanff483612015-12-21 11:14:001281 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1282 receive_stream->Stop();
1283 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 12:30:241284
1285 DestroyStreams();
1286
1287 if (graph_data_output_file)
1288 fclose(graph_data_output_file);
1289}
1290
minyuea27172d2016-11-01 12:59:291291void VideoQualityTest::SetupAudio(int send_channel_id,
1292 int receive_channel_id,
1293 Call* call,
1294 Transport* transport,
1295 AudioReceiveStream** audio_receive_stream) {
1296 audio_send_config_ = AudioSendStream::Config(transport);
1297 audio_send_config_.voe_channel_id = send_channel_id;
1298 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1299
1300 // Add extension to enable audio send side BWE, and allow audio bit rate
1301 // adaptation.
1302 audio_send_config_.rtp.extensions.clear();
1303 if (params_.call.send_side_bwe) {
1304 audio_send_config_.rtp.extensions.push_back(
1305 webrtc::RtpExtension(webrtc::RtpExtension::kTransportSequenceNumberUri,
1306 test::kTransportSequenceNumberExtensionId));
minyue10cbb462016-11-07 17:29:221307 audio_send_config_.min_bitrate_bps = kOpusMinBitrateBps;
1308 audio_send_config_.max_bitrate_bps = kOpusBitrateFbBps;
minyuea27172d2016-11-01 12:59:291309 }
1310 audio_send_config_.send_codec_spec.codec_inst =
1311 CodecInst{120, "OPUS", 48000, 960, 2, 64000};
1312
1313 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1314
1315 AudioReceiveStream::Config audio_config;
1316 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1317 audio_config.rtcp_send_transport = transport;
1318 audio_config.voe_channel_id = receive_channel_id;
1319 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
1320 audio_config.rtp.transport_cc = params_.call.send_side_bwe;
1321 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1322 audio_config.decoder_factory = decoder_factory_;
1323 if (params_.video.enabled && params_.audio.sync_video)
1324 audio_config.sync_group = kSyncGroup;
1325
1326 *audio_receive_stream = call->CreateAudioReceiveStream(audio_config);
1327}
1328
minyue73208662016-08-18 13:28:551329void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 15:23:201330 params_ = params;
1331 CheckParams();
ivica5d6a06c2015-09-17 12:30:241332
ivica5d6a06c2015-09-17 12:30:241333 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1334 // match the full stack tests.
skvlad11a9cbf2016-10-07 18:53:051335 webrtc::RtcEventLogNullImpl event_log;
1336 Call::Config call_config(&event_log_);
minyue626bc952016-10-31 12:47:021337 call_config.bitrate_config = params_.call.call_bitrate_config;
minyue73208662016-08-18 13:28:551338
1339 ::VoiceEngineState voe;
minyue626bc952016-10-31 12:47:021340 if (params_.audio.enabled) {
minyue73208662016-08-18 13:28:551341 CreateVoiceEngine(&voe, decoder_factory_);
1342 AudioState::Config audio_state_config;
1343 audio_state_config.voice_engine = voe.voice_engine;
1344 call_config.audio_state = AudioState::Create(audio_state_config);
1345 }
1346
kwiberg27f982b2016-03-01 19:52:331347 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 12:30:241348
minyuea27172d2016-11-01 12:59:291349 // TODO(minyue): consider if this is a good transport even for audio only
1350 // calls.
ivica5d6a06c2015-09-17 12:30:241351 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 15:29:421352 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
minyue626bc952016-10-31 12:47:021353 params.video.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 12:30:241354 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1355 // least share as much code as possible. That way this test would also match
1356 // the full stack tests better.
1357 transport.SetReceiver(call->Receiver());
1358
minyuea27172d2016-11-01 12:59:291359 VideoReceiveStream* video_receive_stream = nullptr;
brandtr1293aca2016-11-17 06:47:291360 FlexfecReceiveStream* flexfec_receive_stream = nullptr;
minyuea27172d2016-11-01 12:59:291361 std::unique_ptr<test::VideoRenderer> local_preview;
1362 std::unique_ptr<test::VideoRenderer> loopback_video;
1363 if (params_.video.enabled) {
1364 // Create video renderers.
1365 local_preview.reset(test::VideoRenderer::Create(
1366 "Local Preview", params_.video.width, params_.video.height));
ivica87f83a92015-10-08 12:13:321367
minyuea27172d2016-11-01 12:59:291368 size_t stream_id = params_.ss.selected_stream;
1369 std::string title = "Loopback Video";
1370 if (params_.ss.streams.size() > 1) {
1371 std::ostringstream s;
1372 s << stream_id;
1373 title += " - Stream #" + s.str();
1374 }
sprangce4aef12015-11-02 15:23:201375
minyuea27172d2016-11-01 12:59:291376 loopback_video.reset(test::VideoRenderer::Create(
1377 title.c_str(), params_.ss.streams[stream_id].width,
1378 params_.ss.streams[stream_id].height));
mflodman48a4beb2016-07-01 11:03:591379
minyuea27172d2016-11-01 12:59:291380 SetupVideo(&transport, &transport);
minyuea27172d2016-11-01 12:59:291381 video_send_config_.pre_encode_callback = local_preview.get();
1382 video_receive_configs_[stream_id].renderer = loopback_video.get();
1383 if (params_.audio.enabled && params_.audio.sync_video)
1384 video_receive_configs_[stream_id].sync_group = kSyncGroup;
1385
minyuea27172d2016-11-01 12:59:291386 if (params_.screenshare.enabled)
1387 SetupScreenshare();
1388
1389 video_send_stream_ = call->CreateVideoSendStream(
1390 video_send_config_.Copy(), video_encoder_config_.Copy());
brandtr1293aca2016-11-17 06:47:291391 if (params_.video.flexfec) {
1392 RTC_DCHECK(!flexfec_receive_configs_.empty());
1393 flexfec_receive_stream =
1394 call->CreateFlexfecReceiveStream(flexfec_receive_configs_[0]);
1395 }
minyuea27172d2016-11-01 12:59:291396 video_receive_stream = call->CreateVideoReceiveStream(
1397 video_receive_configs_[stream_id].Copy());
1398 CreateCapturer();
perkj803d97f2016-11-01 18:45:461399 video_send_stream_->SetSource(
1400 video_capturer_.get(),
1401 VideoSendStream::DegradationPreference::kBalanced);
philipel274c1dc2016-05-04 13:21:011402 }
1403
minyue73208662016-08-18 13:28:551404 AudioReceiveStream* audio_receive_stream = nullptr;
minyue626bc952016-10-31 12:47:021405 if (params_.audio.enabled) {
minyuea27172d2016-11-01 12:59:291406 SetupAudio(voe.send_channel_id, voe.receive_channel_id, call.get(),
1407 &transport, &audio_receive_stream);
minyue73208662016-08-18 13:28:551408 }
1409
palmkviste75f2042016-09-28 13:19:481410 StartEncodedFrameLogs(video_receive_stream);
1411 StartEncodedFrameLogs(video_send_stream_);
1412
minyue73208662016-08-18 13:28:551413 // Start sending and receiving video.
minyuea27172d2016-11-01 12:59:291414 if (params_.video.enabled) {
brandtr1293aca2016-11-17 06:47:291415 if (flexfec_receive_stream)
1416 flexfec_receive_stream->Start();
minyuea27172d2016-11-01 12:59:291417 video_receive_stream->Start();
1418 video_send_stream_->Start();
1419 video_capturer_->Start();
1420 }
ivica5d6a06c2015-09-17 12:30:241421
minyue626bc952016-10-31 12:47:021422 if (params_.audio.enabled) {
minyue73208662016-08-18 13:28:551423 // Start receiving audio.
1424 audio_receive_stream->Start();
1425 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
minyue73208662016-08-18 13:28:551426
1427 // Start sending audio.
1428 audio_send_stream_->Start();
1429 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1430 }
1431
ivica5d6a06c2015-09-17 12:30:241432 test::PressEnterToContinue();
1433
minyue626bc952016-10-31 12:47:021434 if (params_.audio.enabled) {
minyue73208662016-08-18 13:28:551435 // Stop sending audio.
1436 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1437 audio_send_stream_->Stop();
1438
1439 // Stop receiving audio.
minyue73208662016-08-18 13:28:551440 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1441 audio_receive_stream->Stop();
minyuea27172d2016-11-01 12:59:291442 call->DestroyAudioSendStream(audio_send_stream_);
1443 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 13:28:551444 }
1445
1446 // Stop receiving and sending video.
minyuea27172d2016-11-01 12:59:291447 if (params_.video.enabled) {
1448 video_capturer_->Stop();
1449 video_send_stream_->Stop();
1450 video_receive_stream->Stop();
brandtr1293aca2016-11-17 06:47:291451 if (flexfec_receive_stream) {
1452 flexfec_receive_stream->Stop();
1453 call->DestroyFlexfecReceiveStream(flexfec_receive_stream);
1454 }
minyuea27172d2016-11-01 12:59:291455 call->DestroyVideoReceiveStream(video_receive_stream);
1456 call->DestroyVideoSendStream(video_send_stream_);
minyue73208662016-08-18 13:28:551457 }
1458
ivica5d6a06c2015-09-17 12:30:241459 transport.StopSending();
minyue626bc952016-10-31 12:47:021460 if (params_.audio.enabled)
minyue73208662016-08-18 13:28:551461 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 12:30:241462}
1463
palmkviste75f2042016-09-28 13:19:481464void VideoQualityTest::StartEncodedFrameLogs(VideoSendStream* stream) {
minyue626bc952016-10-31 12:47:021465 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 13:19:481466 std::ostringstream str;
1467 str << send_logs_++;
1468 std::string prefix =
minyue626bc952016-10-31 12:47:021469 params_.video.encoded_frame_base_path + "." + str.str() + ".send.";
palmkviste75f2042016-09-28 13:19:481470 stream->EnableEncodedFrameRecording(
1471 std::vector<rtc::PlatformFile>(
1472 {rtc::CreatePlatformFile(prefix + "1.ivf"),
1473 rtc::CreatePlatformFile(prefix + "2.ivf"),
1474 rtc::CreatePlatformFile(prefix + "3.ivf")}),
1475 10000000);
1476 }
1477}
1478void VideoQualityTest::StartEncodedFrameLogs(VideoReceiveStream* stream) {
minyue626bc952016-10-31 12:47:021479 if (!params_.video.encoded_frame_base_path.empty()) {
palmkviste75f2042016-09-28 13:19:481480 std::ostringstream str;
1481 str << receive_logs_++;
1482 std::string path =
minyue626bc952016-10-31 12:47:021483 params_.video.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
palmkviste75f2042016-09-28 13:19:481484 stream->EnableEncodedFrameRecording(rtc::CreatePlatformFile(path),
1485 10000000);
1486 }
1487}
1488
ivica5d6a06c2015-09-17 12:30:241489} // namespace webrtc