blob: f9440ad86c9441e41cf4dcc7758b1c1bfdc36ff5 [file] [log] [blame]
ivica5d6a06c2015-09-17 05:30:24 -07001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
perkja49cbd32016-09-16 07:53:41 -070010#include "webrtc/video/video_quality_test.h"
perkj9fdbda62016-09-15 09:19:20 -070011
perkja49cbd32016-09-16 07:53:41 -070012#include <stdio.h>
ivica5d6a06c2015-09-17 05:30:24 -070013#include <algorithm>
14#include <deque>
15#include <map>
sprangce4aef12015-11-02 07:23:20 -080016#include <sstream>
mflodmand1590b22015-12-09 07:07:59 -080017#include <string>
ivica5d6a06c2015-09-17 05:30:24 -070018#include <vector>
19
20#include "testing/gtest/include/gtest/gtest.h"
ivica5d6a06c2015-09-17 05:30:24 -070021#include "webrtc/base/checks.h"
Peter Boström5811a392015-12-10 13:02:50 +010022#include "webrtc/base/event.h"
ivica5d6a06c2015-09-17 05:30:24 -070023#include "webrtc/base/format_macros.h"
nissec7fe3c22016-04-20 03:25:36 -070024#include "webrtc/base/optional.h"
sprange1f2f1f2016-02-01 02:04:52 -080025#include "webrtc/base/timeutils.h"
ivica5d6a06c2015-09-17 05:30:24 -070026#include "webrtc/call.h"
27#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
sprangce4aef12015-11-02 07:23:20 -080029#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/cpu_info.h"
ivica5d6a06c2015-09-17 05:30:24 -070031#include "webrtc/test/layer_filtering_transport.h"
32#include "webrtc/test/run_loop.h"
33#include "webrtc/test/statistics.h"
34#include "webrtc/test/testsupport/fileutils.h"
perkja49cbd32016-09-16 07:53:41 -070035#include "webrtc/test/vcm_capturer.h"
ivica5d6a06c2015-09-17 05:30:24 -070036#include "webrtc/test/video_renderer.h"
minyue73208662016-08-18 06:28:55 -070037#include "webrtc/voice_engine/include/voe_base.h"
38#include "webrtc/voice_engine/include/voe_codec.h"
39
40namespace {
41
42constexpr int kSendStatsPollingIntervalMs = 1000;
43constexpr int kPayloadTypeH264 = 122;
44constexpr int kPayloadTypeVP8 = 123;
45constexpr int kPayloadTypeVP9 = 124;
46constexpr size_t kMaxComparisons = 10;
47constexpr char kSyncGroup[] = "av_sync";
48constexpr int kOpusMinBitrate = 6000;
49constexpr int kOpusBitrateFb = 32000;
50
51struct VoiceEngineState {
52 VoiceEngineState()
53 : voice_engine(nullptr),
54 base(nullptr),
55 codec(nullptr),
56 send_channel_id(-1),
57 receive_channel_id(-1) {}
58
59 webrtc::VoiceEngine* voice_engine;
60 webrtc::VoEBase* base;
61 webrtc::VoECodec* codec;
62 int send_channel_id;
63 int receive_channel_id;
64};
65
66void CreateVoiceEngine(VoiceEngineState* voe,
67 rtc::scoped_refptr<webrtc::AudioDecoderFactory>
68 decoder_factory) {
69 voe->voice_engine = webrtc::VoiceEngine::Create();
70 voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
71 voe->codec = webrtc::VoECodec::GetInterface(voe->voice_engine);
72 EXPECT_EQ(0, voe->base->Init(nullptr, nullptr, decoder_factory));
solenberg88499ec2016-09-07 07:34:41 -070073 webrtc::VoEBase::ChannelConfig config;
74 config.enable_voice_pacing = true;
75 voe->send_channel_id = voe->base->CreateChannel(config);
minyue73208662016-08-18 06:28:55 -070076 EXPECT_GE(voe->send_channel_id, 0);
77 voe->receive_channel_id = voe->base->CreateChannel();
78 EXPECT_GE(voe->receive_channel_id, 0);
79}
80
81void DestroyVoiceEngine(VoiceEngineState* voe) {
82 voe->base->DeleteChannel(voe->send_channel_id);
83 voe->send_channel_id = -1;
84 voe->base->DeleteChannel(voe->receive_channel_id);
85 voe->receive_channel_id = -1;
86 voe->base->Release();
87 voe->base = nullptr;
88 voe->codec->Release();
89 voe->codec = nullptr;
90
91 webrtc::VoiceEngine::Delete(voe->voice_engine);
92 voe->voice_engine = nullptr;
93}
94
95} // namespace
ivica5d6a06c2015-09-17 05:30:24 -070096
97namespace webrtc {
98
ivica5d6a06c2015-09-17 05:30:24 -070099class VideoAnalyzer : public PacketReceiver,
pbos2d566682015-09-28 09:59:31 -0700100 public Transport,
nisse7ade7b32016-03-23 04:48:10 -0700101 public rtc::VideoSinkInterface<VideoFrame>,
Peter Boströme4499152016-02-05 11:13:28 +0100102 public EncodedFrameObserver {
ivica5d6a06c2015-09-17 05:30:24 -0700103 public:
sprangce4aef12015-11-02 07:23:20 -0800104 VideoAnalyzer(test::LayerFilteringTransport* transport,
ivica5d6a06c2015-09-17 05:30:24 -0700105 const std::string& test_label,
106 double avg_psnr_threshold,
107 double avg_ssim_threshold,
108 int duration_frames,
sprangce4aef12015-11-02 07:23:20 -0800109 FILE* graph_data_output_file,
110 const std::string& graph_title,
111 uint32_t ssrc_to_analyze)
perkja49cbd32016-09-16 07:53:41 -0700112 : transport_(transport),
ivica5d6a06c2015-09-17 05:30:24 -0700113 receiver_(nullptr),
114 send_stream_(nullptr),
perkja49cbd32016-09-16 07:53:41 -0700115 captured_frame_forwarder_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700116 test_label_(test_label),
117 graph_data_output_file_(graph_data_output_file),
sprangce4aef12015-11-02 07:23:20 -0800118 graph_title_(graph_title),
119 ssrc_to_analyze_(ssrc_to_analyze),
pbos14fe7082016-04-20 06:35:56 -0700120 pre_encode_proxy_(this),
Peter Boströme4499152016-02-05 11:13:28 +0100121 encode_timing_proxy_(this),
ivica5d6a06c2015-09-17 05:30:24 -0700122 frames_to_process_(duration_frames),
123 frames_recorded_(0),
124 frames_processed_(0),
125 dropped_frames_(0),
pbos14fe7082016-04-20 06:35:56 -0700126 dropped_frames_before_first_encode_(0),
127 dropped_frames_before_rendering_(0),
ivica5d6a06c2015-09-17 05:30:24 -0700128 last_render_time_(0),
129 rtp_timestamp_delta_(0),
130 avg_psnr_threshold_(avg_psnr_threshold),
131 avg_ssim_threshold_(avg_ssim_threshold),
Peter Boström8c38e8b2015-11-26 17:45:47 +0100132 stats_polling_thread_(&PollStatsThread, this, "StatsPoller"),
Peter Boström5811a392015-12-10 13:02:50 +0100133 comparison_available_event_(false, false),
Peter Boströmdd45eb62016-01-19 15:22:32 +0100134 done_(true, false) {
ivica5d6a06c2015-09-17 05:30:24 -0700135 // Create thread pool for CPU-expensive PSNR/SSIM calculations.
136
137 // Try to use about as many threads as cores, but leave kMinCoresLeft alone,
138 // so that we don't accidentally starve "real" worker threads (codec etc).
139 // Also, don't allocate more than kMaxComparisonThreads, even if there are
140 // spare cores.
141
142 uint32_t num_cores = CpuInfo::DetectNumberOfCores();
143 RTC_DCHECK_GE(num_cores, 1u);
144 static const uint32_t kMinCoresLeft = 4;
145 static const uint32_t kMaxComparisonThreads = 8;
146
147 if (num_cores <= kMinCoresLeft) {
148 num_cores = 1;
149 } else {
150 num_cores -= kMinCoresLeft;
151 num_cores = std::min(num_cores, kMaxComparisonThreads);
152 }
153
154 for (uint32_t i = 0; i < num_cores; ++i) {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100155 rtc::PlatformThread* thread =
156 new rtc::PlatformThread(&FrameComparisonThread, this, "Analyzer");
157 thread->Start();
158 comparison_thread_pool_.push_back(thread);
ivica5d6a06c2015-09-17 05:30:24 -0700159 }
ivica5d6a06c2015-09-17 05:30:24 -0700160 }
161
162 ~VideoAnalyzer() {
Peter Boström8c38e8b2015-11-26 17:45:47 +0100163 for (rtc::PlatformThread* thread : comparison_thread_pool_) {
164 thread->Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700165 delete thread;
166 }
167 }
168
169 virtual void SetReceiver(PacketReceiver* receiver) { receiver_ = receiver; }
170
perkja49cbd32016-09-16 07:53:41 -0700171 void SetSendStream(VideoSendStream* stream) {
172 rtc::CritScope lock(&crit_);
173 RTC_DCHECK(!send_stream_);
174 send_stream_ = stream;
175 }
176
177 rtc::VideoSinkInterface<VideoFrame>* InputInterface() {
178 return &captured_frame_forwarder_;
179 }
180 rtc::VideoSourceInterface<VideoFrame>* OutputInterface() {
181 return &captured_frame_forwarder_;
182 }
183
ivica5d6a06c2015-09-17 05:30:24 -0700184 DeliveryStatus DeliverPacket(MediaType media_type,
185 const uint8_t* packet,
186 size_t length,
187 const PacketTime& packet_time) override {
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700188 // Ignore timestamps of RTCP packets. They're not synchronized with
189 // RTP packet timestamps and so they would confuse wrap_handler_.
190 if (RtpHeaderParser::IsRtcp(packet, length)) {
191 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
192 }
193
sprangce4aef12015-11-02 07:23:20 -0800194 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700195 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800196 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700197 {
198 rtc::CritScope lock(&crit_);
sprang16daaa52016-03-09 01:30:24 -0800199 int64_t timestamp =
200 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
201 recv_times_[timestamp] =
ivica5d6a06c2015-09-17 05:30:24 -0700202 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
203 }
204
205 return receiver_->DeliverPacket(media_type, packet, length, packet_time);
206 }
207
Peter Boströme4499152016-02-05 11:13:28 +0100208 void MeasuredEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) {
ivica8d15bd62015-10-07 02:43:12 -0700209 rtc::CritScope crit(&comparison_lock_);
210 samples_encode_time_ms_[ntp_time_ms] = encode_time_ms;
211 }
212
pbos14fe7082016-04-20 06:35:56 -0700213 void PreEncodeOnFrame(const VideoFrame& video_frame) {
214 rtc::CritScope lock(&crit_);
215 if (!first_send_timestamp_ && rtp_timestamp_delta_ == 0) {
216 while (frames_.front().timestamp() != video_frame.timestamp()) {
217 ++dropped_frames_before_first_encode_;
218 frames_.pop_front();
219 RTC_CHECK(!frames_.empty());
220 }
221 first_send_timestamp_ = rtc::Optional<uint32_t>(video_frame.timestamp());
222 }
223 }
224
stefan1d8a5062015-10-02 03:39:33 -0700225 bool SendRtp(const uint8_t* packet,
226 size_t length,
227 const PacketOptions& options) override {
sprangce4aef12015-11-02 07:23:20 -0800228 RtpUtility::RtpHeaderParser parser(packet, length);
ivica5d6a06c2015-09-17 05:30:24 -0700229 RTPHeader header;
danilchapf6975f42015-12-28 10:18:46 -0800230 parser.Parse(&header);
ivica5d6a06c2015-09-17 05:30:24 -0700231
sprangce4aef12015-11-02 07:23:20 -0800232 int64_t current_time =
233 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
234 bool result = transport_->SendRtp(packet, length, options);
ivica5d6a06c2015-09-17 05:30:24 -0700235 {
236 rtc::CritScope lock(&crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800237
Peter Boström81cbd922016-03-22 12:19:07 +0100238 if (rtp_timestamp_delta_ == 0) {
nissec7fe3c22016-04-20 03:25:36 -0700239 rtp_timestamp_delta_ = header.timestamp - *first_send_timestamp_;
240 first_send_timestamp_ = rtc::Optional<uint32_t>();
ivica5d6a06c2015-09-17 05:30:24 -0700241 }
sprang1b3530b2016-03-10 01:32:53 -0800242 int64_t timestamp =
243 wrap_handler_.Unwrap(header.timestamp - rtp_timestamp_delta_);
sprangce4aef12015-11-02 07:23:20 -0800244 send_times_[timestamp] = current_time;
245 if (!transport_->DiscardedLastPacket() &&
246 header.ssrc == ssrc_to_analyze_) {
247 encoded_frame_sizes_[timestamp] +=
248 length - (header.headerLength + header.paddingLength);
249 }
ivica5d6a06c2015-09-17 05:30:24 -0700250 }
sprangce4aef12015-11-02 07:23:20 -0800251 return result;
ivica5d6a06c2015-09-17 05:30:24 -0700252 }
253
254 bool SendRtcp(const uint8_t* packet, size_t length) override {
255 return transport_->SendRtcp(packet, length);
256 }
257
258 void EncodedFrameCallback(const EncodedFrame& frame) override {
259 rtc::CritScope lock(&comparison_lock_);
260 if (frames_recorded_ < frames_to_process_)
261 encoded_frame_size_.AddSample(frame.length_);
262 }
263
nisseeb83a1a2016-03-21 01:27:56 -0700264 void OnFrame(const VideoFrame& video_frame) override {
ivica5d6a06c2015-09-17 05:30:24 -0700265 int64_t render_time_ms =
266 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds();
ivica5d6a06c2015-09-17 05:30:24 -0700267
268 rtc::CritScope lock(&crit_);
Taylor Brandstetter433b95a2016-03-18 11:41:03 -0700269 int64_t send_timestamp =
sprang16daaa52016-03-09 01:30:24 -0800270 wrap_handler_.Unwrap(video_frame.timestamp() - rtp_timestamp_delta_);
ivica5d6a06c2015-09-17 05:30:24 -0700271
sprang16daaa52016-03-09 01:30:24 -0800272 while (wrap_handler_.Unwrap(frames_.front().timestamp()) < send_timestamp) {
nisse97f0b932016-05-26 09:44:40 -0700273 if (!last_rendered_frame_) {
pbos14fe7082016-04-20 06:35:56 -0700274 // No previous frame rendered, this one was dropped after sending but
275 // before rendering.
276 ++dropped_frames_before_rendering_;
277 frames_.pop_front();
278 RTC_CHECK(!frames_.empty());
279 continue;
280 }
nisse97f0b932016-05-26 09:44:40 -0700281 AddFrameComparison(frames_.front(), *last_rendered_frame_, true,
ivica5d6a06c2015-09-17 05:30:24 -0700282 render_time_ms);
283 frames_.pop_front();
pbos14fe7082016-04-20 06:35:56 -0700284 RTC_DCHECK(!frames_.empty());
ivica5d6a06c2015-09-17 05:30:24 -0700285 }
286
287 VideoFrame reference_frame = frames_.front();
288 frames_.pop_front();
sprang16daaa52016-03-09 01:30:24 -0800289 int64_t reference_timestamp =
290 wrap_handler_.Unwrap(reference_frame.timestamp());
291 if (send_timestamp == reference_timestamp - 1) {
sprangce4aef12015-11-02 07:23:20 -0800292 // TODO(ivica): Make this work for > 2 streams.
Peter Boströme4499152016-02-05 11:13:28 +0100293 // Look at RTPSender::BuildRTPHeader.
sprangce4aef12015-11-02 07:23:20 -0800294 ++send_timestamp;
295 }
sprang16daaa52016-03-09 01:30:24 -0800296 ASSERT_EQ(reference_timestamp, send_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700297
298 AddFrameComparison(reference_frame, video_frame, false, render_time_ms);
299
nisse97f0b932016-05-26 09:44:40 -0700300 last_rendered_frame_ = rtc::Optional<VideoFrame>(video_frame);
ivica5d6a06c2015-09-17 05:30:24 -0700301 }
302
ivica5d6a06c2015-09-17 05:30:24 -0700303 void Wait() {
304 // Frame comparisons can be very expensive. Wait for test to be done, but
305 // at time-out check if frames_processed is going up. If so, give it more
306 // time, otherwise fail. Hopefully this will reduce test flakiness.
307
Peter Boström8c38e8b2015-11-26 17:45:47 +0100308 stats_polling_thread_.Start();
sprangce4aef12015-11-02 07:23:20 -0800309
ivica5d6a06c2015-09-17 05:30:24 -0700310 int last_frames_processed = -1;
ivica5d6a06c2015-09-17 05:30:24 -0700311 int iteration = 0;
Peter Boström5811a392015-12-10 13:02:50 +0100312 while (!done_.Wait(VideoQualityTest::kDefaultTimeoutMs)) {
ivica5d6a06c2015-09-17 05:30:24 -0700313 int frames_processed;
314 {
315 rtc::CritScope crit(&comparison_lock_);
316 frames_processed = frames_processed_;
317 }
318
319 // Print some output so test infrastructure won't think we've crashed.
320 const char* kKeepAliveMessages[3] = {
321 "Uh, I'm-I'm not quite dead, sir.",
322 "Uh, I-I think uh, I could pull through, sir.",
323 "Actually, I think I'm all right to come with you--"};
324 printf("- %s\n", kKeepAliveMessages[iteration++ % 3]);
325
326 if (last_frames_processed == -1) {
327 last_frames_processed = frames_processed;
328 continue;
329 }
Peter Boströmdd45eb62016-01-19 15:22:32 +0100330 if (frames_processed == last_frames_processed) {
331 EXPECT_GT(frames_processed, last_frames_processed)
332 << "Analyzer stalled while waiting for test to finish.";
333 done_.Set();
334 break;
335 }
ivica5d6a06c2015-09-17 05:30:24 -0700336 last_frames_processed = frames_processed;
337 }
338
339 if (iteration > 0)
340 printf("- Farewell, sweet Concorde!\n");
341
Peter Boström8c38e8b2015-11-26 17:45:47 +0100342 stats_polling_thread_.Stop();
ivica5d6a06c2015-09-17 05:30:24 -0700343 }
344
pbos14fe7082016-04-20 06:35:56 -0700345 rtc::VideoSinkInterface<VideoFrame>* pre_encode_proxy() {
346 return &pre_encode_proxy_;
347 }
Peter Boströme4499152016-02-05 11:13:28 +0100348 EncodedFrameObserver* encode_timing_proxy() { return &encode_timing_proxy_; }
349
sprangce4aef12015-11-02 07:23:20 -0800350 test::LayerFilteringTransport* const transport_;
ivica5d6a06c2015-09-17 05:30:24 -0700351 PacketReceiver* receiver_;
ivica5d6a06c2015-09-17 05:30:24 -0700352
353 private:
354 struct FrameComparison {
355 FrameComparison()
356 : dropped(false),
357 send_time_ms(0),
358 recv_time_ms(0),
359 render_time_ms(0),
360 encoded_frame_size(0) {}
361
362 FrameComparison(const VideoFrame& reference,
363 const VideoFrame& render,
364 bool dropped,
365 int64_t send_time_ms,
366 int64_t recv_time_ms,
367 int64_t render_time_ms,
368 size_t encoded_frame_size)
369 : reference(reference),
370 render(render),
371 dropped(dropped),
372 send_time_ms(send_time_ms),
373 recv_time_ms(recv_time_ms),
374 render_time_ms(render_time_ms),
375 encoded_frame_size(encoded_frame_size) {}
376
377 VideoFrame reference;
378 VideoFrame render;
379 bool dropped;
380 int64_t send_time_ms;
381 int64_t recv_time_ms;
382 int64_t render_time_ms;
383 size_t encoded_frame_size;
384 };
385
386 struct Sample {
ivica8d15bd62015-10-07 02:43:12 -0700387 Sample(int dropped,
388 int64_t input_time_ms,
389 int64_t send_time_ms,
390 int64_t recv_time_ms,
391 int64_t render_time_ms,
392 size_t encoded_frame_size,
ivica5d6a06c2015-09-17 05:30:24 -0700393 double psnr,
ivica8d15bd62015-10-07 02:43:12 -0700394 double ssim)
ivica5d6a06c2015-09-17 05:30:24 -0700395 : dropped(dropped),
396 input_time_ms(input_time_ms),
397 send_time_ms(send_time_ms),
398 recv_time_ms(recv_time_ms),
ivica8d15bd62015-10-07 02:43:12 -0700399 render_time_ms(render_time_ms),
ivica5d6a06c2015-09-17 05:30:24 -0700400 encoded_frame_size(encoded_frame_size),
401 psnr(psnr),
ivica8d15bd62015-10-07 02:43:12 -0700402 ssim(ssim) {}
ivica5d6a06c2015-09-17 05:30:24 -0700403
ivica8d15bd62015-10-07 02:43:12 -0700404 int dropped;
405 int64_t input_time_ms;
406 int64_t send_time_ms;
407 int64_t recv_time_ms;
408 int64_t render_time_ms;
409 size_t encoded_frame_size;
ivica5d6a06c2015-09-17 05:30:24 -0700410 double psnr;
411 double ssim;
ivica5d6a06c2015-09-17 05:30:24 -0700412 };
413
Peter Boströme4499152016-02-05 11:13:28 +0100414 // This class receives the send-side OnEncodeTiming and is provided to not
415 // conflict with the receiver-side pre_decode_callback.
416 class OnEncodeTimingProxy : public EncodedFrameObserver {
417 public:
418 explicit OnEncodeTimingProxy(VideoAnalyzer* parent) : parent_(parent) {}
419
420 void OnEncodeTiming(int64_t ntp_time_ms, int encode_time_ms) override {
421 parent_->MeasuredEncodeTiming(ntp_time_ms, encode_time_ms);
422 }
423 void EncodedFrameCallback(const EncodedFrame& frame) override {}
424
425 private:
426 VideoAnalyzer* const parent_;
427 };
428
pbos14fe7082016-04-20 06:35:56 -0700429 // This class receives the send-side OnFrame callback and is provided to not
430 // conflict with the receiver-side renderer callback.
431 class PreEncodeProxy : public rtc::VideoSinkInterface<VideoFrame> {
432 public:
433 explicit PreEncodeProxy(VideoAnalyzer* parent) : parent_(parent) {}
434
435 void OnFrame(const VideoFrame& video_frame) override {
436 parent_->PreEncodeOnFrame(video_frame);
437 }
438
439 private:
440 VideoAnalyzer* const parent_;
441 };
442
ivica5d6a06c2015-09-17 05:30:24 -0700443 void AddFrameComparison(const VideoFrame& reference,
444 const VideoFrame& render,
445 bool dropped,
446 int64_t render_time_ms)
447 EXCLUSIVE_LOCKS_REQUIRED(crit_) {
sprange1f2f1f2016-02-01 02:04:52 -0800448 int64_t reference_timestamp = wrap_handler_.Unwrap(reference.timestamp());
449 int64_t send_time_ms = send_times_[reference_timestamp];
450 send_times_.erase(reference_timestamp);
451 int64_t recv_time_ms = recv_times_[reference_timestamp];
452 recv_times_.erase(reference_timestamp);
ivica5d6a06c2015-09-17 05:30:24 -0700453
sprangce4aef12015-11-02 07:23:20 -0800454 // TODO(ivica): Make this work for > 2 streams.
sprange1f2f1f2016-02-01 02:04:52 -0800455 auto it = encoded_frame_sizes_.find(reference_timestamp);
sprangce4aef12015-11-02 07:23:20 -0800456 if (it == encoded_frame_sizes_.end())
sprange1f2f1f2016-02-01 02:04:52 -0800457 it = encoded_frame_sizes_.find(reference_timestamp - 1);
sprangce4aef12015-11-02 07:23:20 -0800458 size_t encoded_size = it == encoded_frame_sizes_.end() ? 0 : it->second;
459 if (it != encoded_frame_sizes_.end())
460 encoded_frame_sizes_.erase(it);
ivica5d6a06c2015-09-17 05:30:24 -0700461
462 VideoFrame reference_copy;
463 VideoFrame render_copy;
ivica5d6a06c2015-09-17 05:30:24 -0700464
465 rtc::CritScope crit(&comparison_lock_);
stefanb1797672016-08-11 07:00:57 -0700466 if (comparisons_.size() < kMaxComparisons) {
467 reference_copy.CopyFrame(reference);
468 render_copy.CopyFrame(render);
469 } else {
470 // Copy the time to ensure that delay calculations can still be made.
471 reference_copy.set_ntp_time_ms(reference.ntp_time_ms());
472 render_copy.set_ntp_time_ms(render.ntp_time_ms());
473 }
ivica5d6a06c2015-09-17 05:30:24 -0700474 comparisons_.push_back(FrameComparison(reference_copy, render_copy, dropped,
475 send_time_ms, recv_time_ms,
476 render_time_ms, encoded_size));
Peter Boström5811a392015-12-10 13:02:50 +0100477 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700478 }
479
480 static bool PollStatsThread(void* obj) {
481 return static_cast<VideoAnalyzer*>(obj)->PollStats();
482 }
483
484 bool PollStats() {
Peter Boströmdd45eb62016-01-19 15:22:32 +0100485 if (done_.Wait(kSendStatsPollingIntervalMs))
Peter Boström5811a392015-12-10 13:02:50 +0100486 return false;
ivica5d6a06c2015-09-17 05:30:24 -0700487
488 VideoSendStream::Stats stats = send_stream_->GetStats();
489
490 rtc::CritScope crit(&comparison_lock_);
Peter Boströmb1eaa8d2016-02-23 17:30:49 +0100491 // It's not certain that we yet have estimates for any of these stats. Check
492 // that they are positive before mixing them in.
493 if (stats.encode_frame_rate > 0)
494 encode_frame_rate_.AddSample(stats.encode_frame_rate);
495 if (stats.avg_encode_time_ms > 0)
496 encode_time_ms.AddSample(stats.avg_encode_time_ms);
497 if (stats.encode_usage_percent > 0)
498 encode_usage_percent.AddSample(stats.encode_usage_percent);
499 if (stats.media_bitrate_bps > 0)
500 media_bitrate_bps.AddSample(stats.media_bitrate_bps);
ivica5d6a06c2015-09-17 05:30:24 -0700501
502 return true;
503 }
504
505 static bool FrameComparisonThread(void* obj) {
506 return static_cast<VideoAnalyzer*>(obj)->CompareFrames();
507 }
508
509 bool CompareFrames() {
510 if (AllFramesRecorded())
511 return false;
512
513 VideoFrame reference;
514 VideoFrame render;
515 FrameComparison comparison;
516
517 if (!PopComparison(&comparison)) {
518 // Wait until new comparison task is available, or test is done.
519 // If done, wake up remaining threads waiting.
Peter Boström5811a392015-12-10 13:02:50 +0100520 comparison_available_event_.Wait(1000);
ivica5d6a06c2015-09-17 05:30:24 -0700521 if (AllFramesRecorded()) {
Peter Boström5811a392015-12-10 13:02:50 +0100522 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700523 return false;
524 }
525 return true; // Try again.
526 }
527
528 PerformFrameComparison(comparison);
529
530 if (FrameProcessed()) {
531 PrintResults();
532 if (graph_data_output_file_)
533 PrintSamplesToFile();
Peter Boström5811a392015-12-10 13:02:50 +0100534 done_.Set();
535 comparison_available_event_.Set();
ivica5d6a06c2015-09-17 05:30:24 -0700536 return false;
537 }
538
539 return true;
540 }
541
542 bool PopComparison(FrameComparison* comparison) {
543 rtc::CritScope crit(&comparison_lock_);
544 // If AllFramesRecorded() is true, it means we have already popped
545 // frames_to_process_ frames from comparisons_, so there is no more work
546 // for this thread to be done. frames_processed_ might still be lower if
547 // all comparisons are not done, but those frames are currently being
548 // worked on by other threads.
549 if (comparisons_.empty() || AllFramesRecorded())
550 return false;
551
552 *comparison = comparisons_.front();
553 comparisons_.pop_front();
554
555 FrameRecorded();
556 return true;
557 }
558
559 // Increment counter for number of frames received for comparison.
560 void FrameRecorded() {
561 rtc::CritScope crit(&comparison_lock_);
562 ++frames_recorded_;
563 }
564
565 // Returns true if all frames to be compared have been taken from the queue.
566 bool AllFramesRecorded() {
567 rtc::CritScope crit(&comparison_lock_);
568 assert(frames_recorded_ <= frames_to_process_);
569 return frames_recorded_ == frames_to_process_;
570 }
571
572 // Increase count of number of frames processed. Returns true if this was the
573 // last frame to be processed.
574 bool FrameProcessed() {
575 rtc::CritScope crit(&comparison_lock_);
576 ++frames_processed_;
577 assert(frames_processed_ <= frames_to_process_);
578 return frames_processed_ == frames_to_process_;
579 }
580
581 void PrintResults() {
582 rtc::CritScope crit(&comparison_lock_);
583 PrintResult("psnr", psnr_, " dB");
tnakamura3123cbc2016-02-10 11:21:51 -0800584 PrintResult("ssim", ssim_, " score");
ivica5d6a06c2015-09-17 05:30:24 -0700585 PrintResult("sender_time", sender_time_, " ms");
ivica5d6a06c2015-09-17 05:30:24 -0700586 PrintResult("receiver_time", receiver_time_, " ms");
587 PrintResult("total_delay_incl_network", end_to_end_, " ms");
588 PrintResult("time_between_rendered_frames", rendered_delta_, " ms");
589 PrintResult("encoded_frame_size", encoded_frame_size_, " bytes");
590 PrintResult("encode_frame_rate", encode_frame_rate_, " fps");
591 PrintResult("encode_time", encode_time_ms, " ms");
592 PrintResult("encode_usage_percent", encode_usage_percent, " percent");
593 PrintResult("media_bitrate", media_bitrate_bps, " bps");
594
pbos14fe7082016-04-20 06:35:56 -0700595 printf("RESULT dropped_frames: %s = %d frames\n", test_label_.c_str(),
596 dropped_frames_);
597 printf("RESULT dropped_frames_before_first_encode: %s = %d frames\n",
598 test_label_.c_str(), dropped_frames_before_first_encode_);
599 printf("RESULT dropped_frames_before_rendering: %s = %d frames\n",
600 test_label_.c_str(), dropped_frames_before_rendering_);
601
ivica5d6a06c2015-09-17 05:30:24 -0700602 EXPECT_GT(psnr_.Mean(), avg_psnr_threshold_);
603 EXPECT_GT(ssim_.Mean(), avg_ssim_threshold_);
604 }
605
606 void PerformFrameComparison(const FrameComparison& comparison) {
607 // Perform expensive psnr and ssim calculations while not holding lock.
stefanb1797672016-08-11 07:00:57 -0700608 double psnr = -1.0;
609 double ssim = -1.0;
610 if (!comparison.reference.IsZeroSize()) {
611 psnr = I420PSNR(&comparison.reference, &comparison.render);
612 ssim = I420SSIM(&comparison.reference, &comparison.render);
613 }
ivica5d6a06c2015-09-17 05:30:24 -0700614
615 int64_t input_time_ms = comparison.reference.ntp_time_ms();
616
617 rtc::CritScope crit(&comparison_lock_);
618 if (graph_data_output_file_) {
619 samples_.push_back(
620 Sample(comparison.dropped, input_time_ms, comparison.send_time_ms,
ivica8d15bd62015-10-07 02:43:12 -0700621 comparison.recv_time_ms, comparison.render_time_ms,
622 comparison.encoded_frame_size, psnr, ssim));
ivica5d6a06c2015-09-17 05:30:24 -0700623 }
stefanb1797672016-08-11 07:00:57 -0700624 if (psnr >= 0.0)
625 psnr_.AddSample(psnr);
626 if (ssim >= 0.0)
627 ssim_.AddSample(ssim);
ivica5d6a06c2015-09-17 05:30:24 -0700628
629 if (comparison.dropped) {
630 ++dropped_frames_;
631 return;
632 }
633 if (last_render_time_ != 0)
634 rendered_delta_.AddSample(comparison.render_time_ms - last_render_time_);
635 last_render_time_ = comparison.render_time_ms;
636
637 sender_time_.AddSample(comparison.send_time_ms - input_time_ms);
638 receiver_time_.AddSample(comparison.render_time_ms -
639 comparison.recv_time_ms);
640 end_to_end_.AddSample(comparison.render_time_ms - input_time_ms);
641 encoded_frame_size_.AddSample(comparison.encoded_frame_size);
642 }
643
644 void PrintResult(const char* result_type,
645 test::Statistics stats,
646 const char* unit) {
647 printf("RESULT %s: %s = {%f, %f}%s\n",
648 result_type,
649 test_label_.c_str(),
650 stats.Mean(),
651 stats.StandardDeviation(),
652 unit);
653 }
654
655 void PrintSamplesToFile(void) {
656 FILE* out = graph_data_output_file_;
657 rtc::CritScope crit(&comparison_lock_);
658 std::sort(samples_.begin(), samples_.end(),
659 [](const Sample& A, const Sample& B) -> bool {
660 return A.input_time_ms < B.input_time_ms;
661 });
662
sprangce4aef12015-11-02 07:23:20 -0800663 fprintf(out, "%s\n", graph_title_.c_str());
ivica5d6a06c2015-09-17 05:30:24 -0700664 fprintf(out, "%" PRIuS "\n", samples_.size());
665 fprintf(out,
666 "dropped "
667 "input_time_ms "
668 "send_time_ms "
669 "recv_time_ms "
ivica8d15bd62015-10-07 02:43:12 -0700670 "render_time_ms "
ivica5d6a06c2015-09-17 05:30:24 -0700671 "encoded_frame_size "
672 "psnr "
673 "ssim "
ivica8d15bd62015-10-07 02:43:12 -0700674 "encode_time_ms\n");
675 int missing_encode_time_samples = 0;
ivica5d6a06c2015-09-17 05:30:24 -0700676 for (const Sample& sample : samples_) {
ivica8d15bd62015-10-07 02:43:12 -0700677 auto it = samples_encode_time_ms_.find(sample.input_time_ms);
678 int encode_time_ms;
679 if (it != samples_encode_time_ms_.end()) {
680 encode_time_ms = it->second;
681 } else {
682 ++missing_encode_time_samples;
683 encode_time_ms = -1;
684 }
685 fprintf(out, "%d %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRIuS
686 " %lf %lf %d\n",
687 sample.dropped, sample.input_time_ms, sample.send_time_ms,
688 sample.recv_time_ms, sample.render_time_ms,
ivica5d6a06c2015-09-17 05:30:24 -0700689 sample.encoded_frame_size, sample.psnr, sample.ssim,
ivica8d15bd62015-10-07 02:43:12 -0700690 encode_time_ms);
691 }
692 if (missing_encode_time_samples) {
693 fprintf(stderr,
694 "Warning: Missing encode_time_ms samples for %d frame(s).\n",
695 missing_encode_time_samples);
ivica5d6a06c2015-09-17 05:30:24 -0700696 }
697 }
698
perkja49cbd32016-09-16 07:53:41 -0700699 // Implements VideoSinkInterface to receive captured frames from a
700 // FrameGeneratorCapturer. Implements VideoSourceInterface to be able to act
701 // as a source to VideoSendStream.
702 // It forwards all input frames to the VideoAnalyzer for later comparison and
703 // forwards the captured frames to the VideoSendStream.
704 class CapturedFrameForwarder : public rtc::VideoSinkInterface<VideoFrame>,
705 public rtc::VideoSourceInterface<VideoFrame> {
706 public:
707 explicit CapturedFrameForwarder(VideoAnalyzer* analyzer)
708 : analyzer_(analyzer), send_stream_input_(nullptr) {}
709
710 private:
711 void OnFrame(const VideoFrame& video_frame) override {
712 VideoFrame copy = video_frame;
713 copy.set_timestamp(copy.ntp_time_ms() * 90);
714
715 analyzer_->AddCapturedFrameForComparison(video_frame);
716 rtc::CritScope lock(&crit_);
717 if (send_stream_input_)
718 send_stream_input_->OnFrame(video_frame);
719 }
720
721 // Called when |send_stream_.SetSource()| is called.
722 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
723 const rtc::VideoSinkWants& wants) override {
724 rtc::CritScope lock(&crit_);
725 RTC_DCHECK(!send_stream_input_ || send_stream_input_ == sink);
726 send_stream_input_ = sink;
727 }
728
729 // Called by |send_stream_| when |send_stream_.SetSource()| is called.
730 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override {
731 rtc::CritScope lock(&crit_);
732 RTC_DCHECK(sink == send_stream_input_);
733 send_stream_input_ = nullptr;
734 }
735
736 VideoAnalyzer* const analyzer_;
737 rtc::CriticalSection crit_;
738 rtc::VideoSinkInterface<VideoFrame>* send_stream_input_ GUARDED_BY(crit_);
739 };
740
741 void AddCapturedFrameForComparison(const VideoFrame& video_frame) {
742 rtc::CritScope lock(&crit_);
743 RTC_DCHECK_EQ(0u, video_frame.timestamp());
744 // Frames from the capturer does not have a rtp timestamp. Create one so it
745 // can be used for comparison.
746 VideoFrame copy = video_frame;
747 copy.set_timestamp(copy.ntp_time_ms() * 90);
748 frames_.push_back(copy);
749 }
750
751 VideoSendStream* send_stream_;
752 CapturedFrameForwarder captured_frame_forwarder_;
ivica5d6a06c2015-09-17 05:30:24 -0700753 const std::string test_label_;
754 FILE* const graph_data_output_file_;
sprangce4aef12015-11-02 07:23:20 -0800755 const std::string graph_title_;
756 const uint32_t ssrc_to_analyze_;
pbos14fe7082016-04-20 06:35:56 -0700757 PreEncodeProxy pre_encode_proxy_;
Peter Boströme4499152016-02-05 11:13:28 +0100758 OnEncodeTimingProxy encode_timing_proxy_;
ivica5d6a06c2015-09-17 05:30:24 -0700759 std::vector<Sample> samples_ GUARDED_BY(comparison_lock_);
ivica8d15bd62015-10-07 02:43:12 -0700760 std::map<int64_t, int> samples_encode_time_ms_ GUARDED_BY(comparison_lock_);
ivica5d6a06c2015-09-17 05:30:24 -0700761 test::Statistics sender_time_ GUARDED_BY(comparison_lock_);
762 test::Statistics receiver_time_ GUARDED_BY(comparison_lock_);
763 test::Statistics psnr_ GUARDED_BY(comparison_lock_);
764 test::Statistics ssim_ GUARDED_BY(comparison_lock_);
765 test::Statistics end_to_end_ GUARDED_BY(comparison_lock_);
766 test::Statistics rendered_delta_ GUARDED_BY(comparison_lock_);
767 test::Statistics encoded_frame_size_ GUARDED_BY(comparison_lock_);
768 test::Statistics encode_frame_rate_ GUARDED_BY(comparison_lock_);
769 test::Statistics encode_time_ms GUARDED_BY(comparison_lock_);
770 test::Statistics encode_usage_percent GUARDED_BY(comparison_lock_);
771 test::Statistics media_bitrate_bps GUARDED_BY(comparison_lock_);
772
773 const int frames_to_process_;
774 int frames_recorded_;
775 int frames_processed_;
776 int dropped_frames_;
pbos14fe7082016-04-20 06:35:56 -0700777 int dropped_frames_before_first_encode_;
778 int dropped_frames_before_rendering_;
ivica5d6a06c2015-09-17 05:30:24 -0700779 int64_t last_render_time_;
780 uint32_t rtp_timestamp_delta_;
781
782 rtc::CriticalSection crit_;
783 std::deque<VideoFrame> frames_ GUARDED_BY(crit_);
nisse97f0b932016-05-26 09:44:40 -0700784 rtc::Optional<VideoFrame> last_rendered_frame_ GUARDED_BY(crit_);
sprange1f2f1f2016-02-01 02:04:52 -0800785 rtc::TimestampWrapAroundHandler wrap_handler_ GUARDED_BY(crit_);
786 std::map<int64_t, int64_t> send_times_ GUARDED_BY(crit_);
787 std::map<int64_t, int64_t> recv_times_ GUARDED_BY(crit_);
788 std::map<int64_t, size_t> encoded_frame_sizes_ GUARDED_BY(crit_);
nissec7fe3c22016-04-20 03:25:36 -0700789 rtc::Optional<uint32_t> first_send_timestamp_ GUARDED_BY(crit_);
ivica5d6a06c2015-09-17 05:30:24 -0700790 const double avg_psnr_threshold_;
791 const double avg_ssim_threshold_;
792
793 rtc::CriticalSection comparison_lock_;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100794 std::vector<rtc::PlatformThread*> comparison_thread_pool_;
795 rtc::PlatformThread stats_polling_thread_;
Peter Boström5811a392015-12-10 13:02:50 +0100796 rtc::Event comparison_available_event_;
ivica5d6a06c2015-09-17 05:30:24 -0700797 std::deque<FrameComparison> comparisons_ GUARDED_BY(comparison_lock_);
Peter Boström5811a392015-12-10 13:02:50 +0100798 rtc::Event done_;
ivica5d6a06c2015-09-17 05:30:24 -0700799};
800
ivica87f83a92015-10-08 05:13:32 -0700801VideoQualityTest::VideoQualityTest() : clock_(Clock::GetRealTimeClock()) {}
ivica5d6a06c2015-09-17 05:30:24 -0700802
803void VideoQualityTest::TestBody() {}
804
sprangce4aef12015-11-02 07:23:20 -0800805std::string VideoQualityTest::GenerateGraphTitle() const {
806 std::stringstream ss;
kjellander5865f482016-09-08 10:52:38 -0700807 ss << params_.common.codec;
808 ss << " (" << params_.common.target_bitrate_bps / 1000 << "kbps";
809 ss << ", " << params_.common.fps << " FPS";
sprangce4aef12015-11-02 07:23:20 -0800810 if (params_.screenshare.scroll_duration)
811 ss << ", " << params_.screenshare.scroll_duration << "s scroll";
812 if (params_.ss.streams.size() > 1)
813 ss << ", Stream #" << params_.ss.selected_stream;
814 if (params_.ss.num_spatial_layers > 1)
815 ss << ", Layer #" << params_.ss.selected_sl;
816 ss << ")";
817 return ss.str();
818}
819
820void VideoQualityTest::CheckParams() {
821 // Add a default stream in none specified.
822 if (params_.ss.streams.empty())
823 params_.ss.streams.push_back(VideoQualityTest::DefaultVideoStream(params_));
824 if (params_.ss.num_spatial_layers == 0)
825 params_.ss.num_spatial_layers = 1;
826
827 if (params_.pipe.loss_percent != 0 ||
828 params_.pipe.queue_length_packets != 0) {
829 // Since LayerFilteringTransport changes the sequence numbers, we can't
830 // use that feature with pack loss, since the NACK request would end up
831 // retransmitting the wrong packets.
832 RTC_CHECK(params_.ss.selected_sl == -1 ||
sprangee37de32015-11-23 06:10:23 -0800833 params_.ss.selected_sl == params_.ss.num_spatial_layers - 1);
kjellander5865f482016-09-08 10:52:38 -0700834 RTC_CHECK(params_.common.selected_tl == -1 ||
835 params_.common.selected_tl ==
836 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800837 }
838
839 // TODO(ivica): Should max_bitrate_bps == -1 represent inf max bitrate, as it
840 // does in some parts of the code?
kjellander5865f482016-09-08 10:52:38 -0700841 RTC_CHECK_GE(params_.common.max_bitrate_bps,
842 params_.common.target_bitrate_bps);
843 RTC_CHECK_GE(params_.common.target_bitrate_bps,
844 params_.common.min_bitrate_bps);
845 RTC_CHECK_LT(params_.common.selected_tl, params_.common.num_temporal_layers);
sprangce4aef12015-11-02 07:23:20 -0800846 RTC_CHECK_LT(params_.ss.selected_stream, params_.ss.streams.size());
847 for (const VideoStream& stream : params_.ss.streams) {
848 RTC_CHECK_GE(stream.min_bitrate_bps, 0);
849 RTC_CHECK_GE(stream.target_bitrate_bps, stream.min_bitrate_bps);
850 RTC_CHECK_GE(stream.max_bitrate_bps, stream.target_bitrate_bps);
851 RTC_CHECK_EQ(static_cast<int>(stream.temporal_layer_thresholds_bps.size()),
kjellander5865f482016-09-08 10:52:38 -0700852 params_.common.num_temporal_layers - 1);
sprangce4aef12015-11-02 07:23:20 -0800853 }
854 // TODO(ivica): Should we check if the sum of all streams/layers is equal to
855 // the total bitrate? We anyway have to update them in the case bitrate
856 // estimator changes the total bitrates.
857 RTC_CHECK_GE(params_.ss.num_spatial_layers, 1);
858 RTC_CHECK_LE(params_.ss.selected_sl, params_.ss.num_spatial_layers);
859 RTC_CHECK(params_.ss.spatial_layers.empty() ||
860 params_.ss.spatial_layers.size() ==
861 static_cast<size_t>(params_.ss.num_spatial_layers));
kjellander5865f482016-09-08 10:52:38 -0700862 if (params_.common.codec == "VP8") {
sprangce4aef12015-11-02 07:23:20 -0800863 RTC_CHECK_EQ(params_.ss.num_spatial_layers, 1);
kjellander5865f482016-09-08 10:52:38 -0700864 } else if (params_.common.codec == "VP9") {
sprangce4aef12015-11-02 07:23:20 -0800865 RTC_CHECK_EQ(params_.ss.streams.size(), 1u);
866 }
867}
868
869// Static.
870std::vector<int> VideoQualityTest::ParseCSV(const std::string& str) {
871 // Parse comma separated nonnegative integers, where some elements may be
872 // empty. The empty values are replaced with -1.
873 // E.g. "10,-20,,30,40" --> {10, 20, -1, 30,40}
874 // E.g. ",,10,,20," --> {-1, -1, 10, -1, 20, -1}
875 std::vector<int> result;
876 if (str.empty())
877 return result;
878
879 const char* p = str.c_str();
880 int value = -1;
881 int pos;
882 while (*p) {
883 if (*p == ',') {
884 result.push_back(value);
885 value = -1;
886 ++p;
887 continue;
888 }
889 RTC_CHECK_EQ(sscanf(p, "%d%n", &value, &pos), 1)
890 << "Unexpected non-number value.";
891 p += pos;
892 }
893 result.push_back(value);
894 return result;
895}
896
897// Static.
898VideoStream VideoQualityTest::DefaultVideoStream(const Params& params) {
899 VideoStream stream;
kjellander5865f482016-09-08 10:52:38 -0700900 stream.width = params.common.width;
901 stream.height = params.common.height;
902 stream.max_framerate = params.common.fps;
903 stream.min_bitrate_bps = params.common.min_bitrate_bps;
904 stream.target_bitrate_bps = params.common.target_bitrate_bps;
905 stream.max_bitrate_bps = params.common.max_bitrate_bps;
sprangce4aef12015-11-02 07:23:20 -0800906 stream.max_qp = 52;
kjellander5865f482016-09-08 10:52:38 -0700907 if (params.common.num_temporal_layers == 2)
sprangce4aef12015-11-02 07:23:20 -0800908 stream.temporal_layer_thresholds_bps.push_back(stream.target_bitrate_bps);
909 return stream;
910}
911
912// Static.
913void VideoQualityTest::FillScalabilitySettings(
914 Params* params,
915 const std::vector<std::string>& stream_descriptors,
916 size_t selected_stream,
917 int num_spatial_layers,
918 int selected_sl,
919 const std::vector<std::string>& sl_descriptors) {
920 // Read VideoStream and SpatialLayer elements from a list of comma separated
921 // lists. To use a default value for an element, use -1 or leave empty.
922 // Validity checks performed in CheckParams.
923
924 RTC_CHECK(params->ss.streams.empty());
925 for (auto descriptor : stream_descriptors) {
926 if (descriptor.empty())
927 continue;
928 VideoStream stream = VideoQualityTest::DefaultVideoStream(*params);
929 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
930 if (v[0] != -1)
931 stream.width = static_cast<size_t>(v[0]);
932 if (v[1] != -1)
933 stream.height = static_cast<size_t>(v[1]);
934 if (v[2] != -1)
935 stream.max_framerate = v[2];
936 if (v[3] != -1)
937 stream.min_bitrate_bps = v[3];
938 if (v[4] != -1)
939 stream.target_bitrate_bps = v[4];
940 if (v[5] != -1)
941 stream.max_bitrate_bps = v[5];
942 if (v.size() > 6 && v[6] != -1)
943 stream.max_qp = v[6];
944 if (v.size() > 7) {
945 stream.temporal_layer_thresholds_bps.clear();
946 stream.temporal_layer_thresholds_bps.insert(
947 stream.temporal_layer_thresholds_bps.end(), v.begin() + 7, v.end());
948 } else {
949 // Automatic TL thresholds for more than two layers not supported.
kjellander5865f482016-09-08 10:52:38 -0700950 RTC_CHECK_LE(params->common.num_temporal_layers, 2);
sprangce4aef12015-11-02 07:23:20 -0800951 }
952 params->ss.streams.push_back(stream);
953 }
954 params->ss.selected_stream = selected_stream;
955
956 params->ss.num_spatial_layers = num_spatial_layers ? num_spatial_layers : 1;
957 params->ss.selected_sl = selected_sl;
958 RTC_CHECK(params->ss.spatial_layers.empty());
959 for (auto descriptor : sl_descriptors) {
960 if (descriptor.empty())
961 continue;
962 std::vector<int> v = VideoQualityTest::ParseCSV(descriptor);
963 RTC_CHECK_GT(v[2], 0);
964
965 SpatialLayer layer;
966 layer.scaling_factor_num = v[0] == -1 ? 1 : v[0];
967 layer.scaling_factor_den = v[1] == -1 ? 1 : v[1];
968 layer.target_bitrate_bps = v[2];
969 params->ss.spatial_layers.push_back(layer);
970 }
971}
972
973void VideoQualityTest::SetupCommon(Transport* send_transport,
974 Transport* recv_transport) {
975 if (params_.logs)
ivica5d6a06c2015-09-17 05:30:24 -0700976 trace_to_stderr_.reset(new test::TraceToStderr);
977
sprangce4aef12015-11-02 07:23:20 -0800978 size_t num_streams = params_.ss.streams.size();
Stefan Holmer9fea80f2016-01-07 17:43:18 +0100979 CreateSendConfig(num_streams, 0, send_transport);
ivica5d6a06c2015-09-17 05:30:24 -0700980
981 int payload_type;
kjellander5865f482016-09-08 10:52:38 -0700982 if (params_.common.codec == "H264") {
hbosbab934b2016-01-27 01:36:03 -0800983 encoder_.reset(VideoEncoder::Create(VideoEncoder::kH264));
984 payload_type = kPayloadTypeH264;
kjellander5865f482016-09-08 10:52:38 -0700985 } else if (params_.common.codec == "VP8") {
ivica5d6a06c2015-09-17 05:30:24 -0700986 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp8));
987 payload_type = kPayloadTypeVP8;
kjellander5865f482016-09-08 10:52:38 -0700988 } else if (params_.common.codec == "VP9") {
ivica5d6a06c2015-09-17 05:30:24 -0700989 encoder_.reset(VideoEncoder::Create(VideoEncoder::kVp9));
990 payload_type = kPayloadTypeVP9;
991 } else {
992 RTC_NOTREACHED() << "Codec not supported!";
993 return;
994 }
stefanff483612015-12-21 03:14:00 -0800995 video_send_config_.encoder_settings.encoder = encoder_.get();
kjellander5865f482016-09-08 10:52:38 -0700996 video_send_config_.encoder_settings.payload_name = params_.common.codec;
stefanff483612015-12-21 03:14:00 -0800997 video_send_config_.encoder_settings.payload_type = payload_type;
998 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
999 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
sprangce4aef12015-11-02 07:23:20 -08001000 for (size_t i = 0; i < num_streams; ++i)
stefanff483612015-12-21 03:14:00 -08001001 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
ivica5d6a06c2015-09-17 05:30:24 -07001002
stefanff483612015-12-21 03:14:00 -08001003 video_send_config_.rtp.extensions.clear();
kjellander5865f482016-09-08 10:52:38 -07001004 if (params_.common.send_side_bwe) {
stefanff483612015-12-21 03:14:00 -08001005 video_send_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -07001006 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
Stefan Holmer12952972015-10-29 15:13:24 +01001007 test::kTransportSequenceNumberExtensionId));
1008 } else {
stefanff483612015-12-21 03:14:00 -08001009 video_send_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -07001010 RtpExtension::kAbsSendTimeUri, test::kAbsSendTimeExtensionId));
Erik SprĂ¥ng6b8d3552015-09-24 15:06:57 +02001011 }
1012
stefanff483612015-12-21 03:14:00 -08001013 video_encoder_config_.min_transmit_bitrate_bps =
kjellander5865f482016-09-08 10:52:38 -07001014 params_.common.min_transmit_bps;
stefanff483612015-12-21 03:14:00 -08001015 video_encoder_config_.streams = params_.ss.streams;
1016 video_encoder_config_.spatial_layers = params_.ss.spatial_layers;
ivica5d6a06c2015-09-17 05:30:24 -07001017
1018 CreateMatchingReceiveConfigs(recv_transport);
1019
sprangce4aef12015-11-02 07:23:20 -08001020 for (size_t i = 0; i < num_streams; ++i) {
stefanff483612015-12-21 03:14:00 -08001021 video_receive_configs_[i].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
Stefan Holmer10880012016-02-03 13:29:59 +01001022 video_receive_configs_[i].rtp.rtx[payload_type].ssrc = kSendRtxSsrcs[i];
1023 video_receive_configs_[i].rtp.rtx[payload_type].payload_type =
sprangce4aef12015-11-02 07:23:20 -08001024 kSendRtxPayloadType;
kjellander5865f482016-09-08 10:52:38 -07001025 video_receive_configs_[i].rtp.transport_cc = params_.common.send_side_bwe;
sprangce4aef12015-11-02 07:23:20 -08001026 }
ivica5d6a06c2015-09-17 05:30:24 -07001027}
1028
sprangce4aef12015-11-02 07:23:20 -08001029void VideoQualityTest::SetupScreenshare() {
1030 RTC_CHECK(params_.screenshare.enabled);
ivica5d6a06c2015-09-17 05:30:24 -07001031
1032 // Fill out codec settings.
stefanff483612015-12-21 03:14:00 -08001033 video_encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
kjellander5865f482016-09-08 10:52:38 -07001034 if (params_.common.codec == "VP8") {
kthelgason29a44e32016-09-27 03:52:02 -07001035 VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings();
1036 vp8_settings.denoisingOn = false;
1037 vp8_settings.frameDroppingOn = false;
1038 vp8_settings.numberOfTemporalLayers =
kjellander5865f482016-09-08 10:52:38 -07001039 static_cast<unsigned char>(params_.common.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001040 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1041 VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings);
kjellander5865f482016-09-08 10:52:38 -07001042 } else if (params_.common.codec == "VP9") {
kthelgason29a44e32016-09-27 03:52:02 -07001043 VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings();
1044 vp9_settings.denoisingOn = false;
1045 vp9_settings.frameDroppingOn = false;
1046 vp9_settings.numberOfTemporalLayers =
kjellander5865f482016-09-08 10:52:38 -07001047 static_cast<unsigned char>(params_.common.num_temporal_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001048 vp9_settings.numberOfSpatialLayers =
sprangce4aef12015-11-02 07:23:20 -08001049 static_cast<unsigned char>(params_.ss.num_spatial_layers);
kthelgason29a44e32016-09-27 03:52:02 -07001050 video_encoder_config_.encoder_specific_settings = new rtc::RefCountedObject<
1051 VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings);
ivica5d6a06c2015-09-17 05:30:24 -07001052 }
1053
1054 // Setup frame generator.
1055 const size_t kWidth = 1850;
1056 const size_t kHeight = 1110;
1057 std::vector<std::string> slides;
1058 slides.push_back(test::ResourcePath("web_screenshot_1850_1110", "yuv"));
1059 slides.push_back(test::ResourcePath("presentation_1850_1110", "yuv"));
1060 slides.push_back(test::ResourcePath("photo_1850_1110", "yuv"));
1061 slides.push_back(test::ResourcePath("difficult_photo_1850_1110", "yuv"));
1062
sprangce4aef12015-11-02 07:23:20 -08001063 if (params_.screenshare.scroll_duration == 0) {
ivica5d6a06c2015-09-17 05:30:24 -07001064 // Cycle image every slide_change_interval seconds.
1065 frame_generator_.reset(test::FrameGenerator::CreateFromYuvFile(
1066 slides, kWidth, kHeight,
kjellander5865f482016-09-08 10:52:38 -07001067 params_.screenshare.slide_change_interval * params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001068 } else {
kjellander5865f482016-09-08 10:52:38 -07001069 RTC_CHECK_LE(params_.common.width, kWidth);
1070 RTC_CHECK_LE(params_.common.height, kHeight);
sprangce4aef12015-11-02 07:23:20 -08001071 RTC_CHECK_GT(params_.screenshare.slide_change_interval, 0);
1072 const int kPauseDurationMs = (params_.screenshare.slide_change_interval -
1073 params_.screenshare.scroll_duration) *
1074 1000;
1075 RTC_CHECK_LE(params_.screenshare.scroll_duration,
1076 params_.screenshare.slide_change_interval);
ivica5d6a06c2015-09-17 05:30:24 -07001077
sprangce4aef12015-11-02 07:23:20 -08001078 frame_generator_.reset(
1079 test::FrameGenerator::CreateScrollingInputFromYuvFiles(
kjellander5865f482016-09-08 10:52:38 -07001080 clock_, slides, kWidth, kHeight, params_.common.width,
1081 params_.common.height, params_.screenshare.scroll_duration * 1000,
sprangce4aef12015-11-02 07:23:20 -08001082 kPauseDurationMs));
ivica5d6a06c2015-09-17 05:30:24 -07001083 }
1084}
1085
perkja49cbd32016-09-16 07:53:41 -07001086void VideoQualityTest::CreateCapturer() {
sprangce4aef12015-11-02 07:23:20 -08001087 if (params_.screenshare.enabled) {
1088 test::FrameGeneratorCapturer* frame_generator_capturer =
perkja49cbd32016-09-16 07:53:41 -07001089 new test::FrameGeneratorCapturer(clock_, frame_generator_.release(),
1090 params_.common.fps);
ivica2d4e6c52015-09-23 01:57:06 -07001091 EXPECT_TRUE(frame_generator_capturer->Init());
1092 capturer_.reset(frame_generator_capturer);
ivica5d6a06c2015-09-17 05:30:24 -07001093 } else {
sprangce4aef12015-11-02 07:23:20 -08001094 if (params_.video.clip_name.empty()) {
perkja49cbd32016-09-16 07:53:41 -07001095 capturer_.reset(test::VcmCapturer::Create(
1096 params_.common.width, params_.common.height, params_.common.fps));
ivica5d6a06c2015-09-17 05:30:24 -07001097 } else {
ivica2d4e6c52015-09-23 01:57:06 -07001098 capturer_.reset(test::FrameGeneratorCapturer::CreateFromYuvFile(
perkja49cbd32016-09-16 07:53:41 -07001099 test::ResourcePath(params_.video.clip_name, "yuv"),
kjellander5865f482016-09-08 10:52:38 -07001100 params_.common.width, params_.common.height, params_.common.fps,
ivica2d4e6c52015-09-23 01:57:06 -07001101 clock_));
Peter Boström74f6e9e2016-04-04 17:56:10 +02001102 ASSERT_TRUE(capturer_) << "Could not create capturer for "
1103 << params_.video.clip_name
1104 << ".yuv. Is this resource file present?";
ivica5d6a06c2015-09-17 05:30:24 -07001105 }
1106 }
1107}
1108
sprang7a975f72015-10-12 06:33:21 -07001109void VideoQualityTest::RunWithAnalyzer(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001110 params_ = params;
1111
kjellander5865f482016-09-08 10:52:38 -07001112 RTC_CHECK(!params_.audio);
ivica5d6a06c2015-09-17 05:30:24 -07001113 // TODO(ivica): Merge with RunWithRenderer and use a flag / argument to
1114 // differentiate between the analyzer and the renderer case.
sprangce4aef12015-11-02 07:23:20 -08001115 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001116
1117 FILE* graph_data_output_file = nullptr;
sprangce4aef12015-11-02 07:23:20 -08001118 if (!params_.analyzer.graph_data_output_filename.empty()) {
ivica5d6a06c2015-09-17 05:30:24 -07001119 graph_data_output_file =
sprangce4aef12015-11-02 07:23:20 -08001120 fopen(params_.analyzer.graph_data_output_filename.c_str(), "w");
Peter Boström74f6e9e2016-04-04 17:56:10 +02001121 RTC_CHECK(graph_data_output_file)
sprangce4aef12015-11-02 07:23:20 -08001122 << "Can't open the file " << params_.analyzer.graph_data_output_filename
1123 << "!";
ivica87f83a92015-10-08 05:13:32 -07001124 }
sprang7a975f72015-10-12 06:33:21 -07001125
stefanf116bd02015-10-27 08:29:42 -07001126 Call::Config call_config;
kjellander5865f482016-09-08 10:52:38 -07001127 call_config.bitrate_config = params.common.call_bitrate_config;
stefanf116bd02015-10-27 08:29:42 -07001128 CreateCalls(call_config, call_config);
1129
ivica87f83a92015-10-08 05:13:32 -07001130 test::LayerFilteringTransport send_transport(
kjellander5865f482016-09-08 10:52:38 -07001131 params.pipe, sender_call_.get(), kPayloadTypeVP8, kPayloadTypeVP9,
1132 params.common.selected_tl, params_.ss.selected_sl);
1133 test::DirectTransport recv_transport(params.pipe, receiver_call_.get());
stefanf116bd02015-10-27 08:29:42 -07001134
sprangce4aef12015-11-02 07:23:20 -08001135 std::string graph_title = params_.analyzer.graph_title;
1136 if (graph_title.empty())
1137 graph_title = VideoQualityTest::GenerateGraphTitle();
1138
1139 // In the case of different resolutions, the functions calculating PSNR and
1140 // SSIM return -1.0, instead of a positive value as usual. VideoAnalyzer
1141 // aborts if the average psnr/ssim are below the given threshold, which is
1142 // 0.0 by default. Setting the thresholds to -1.1 prevents the unnecessary
1143 // abort.
1144 VideoStream& selected_stream = params_.ss.streams[params_.ss.selected_stream];
1145 int selected_sl = params_.ss.selected_sl != -1
1146 ? params_.ss.selected_sl
1147 : params_.ss.num_spatial_layers - 1;
1148 bool disable_quality_check =
kjellander5865f482016-09-08 10:52:38 -07001149 selected_stream.width != params_.common.width ||
1150 selected_stream.height != params_.common.height ||
sprangce4aef12015-11-02 07:23:20 -08001151 (!params_.ss.spatial_layers.empty() &&
1152 params_.ss.spatial_layers[selected_sl].scaling_factor_num !=
1153 params_.ss.spatial_layers[selected_sl].scaling_factor_den);
1154 if (disable_quality_check) {
1155 fprintf(stderr,
1156 "Warning: Calculating PSNR and SSIM for downsized resolution "
1157 "not implemented yet! Skipping PSNR and SSIM calculations!");
1158 }
1159
ivica5d6a06c2015-09-17 05:30:24 -07001160 VideoAnalyzer analyzer(
sprangce4aef12015-11-02 07:23:20 -08001161 &send_transport, params_.analyzer.test_label,
1162 disable_quality_check ? -1.1 : params_.analyzer.avg_psnr_threshold,
1163 disable_quality_check ? -1.1 : params_.analyzer.avg_ssim_threshold,
kjellander5865f482016-09-08 10:52:38 -07001164 params_.analyzer.test_durations_secs * params_.common.fps,
sprangce4aef12015-11-02 07:23:20 -08001165 graph_data_output_file, graph_title,
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001166 kVideoSendSsrcs[params_.ss.selected_stream]);
ivica5d6a06c2015-09-17 05:30:24 -07001167
ivica5d6a06c2015-09-17 05:30:24 -07001168 analyzer.SetReceiver(receiver_call_->Receiver());
1169 send_transport.SetReceiver(&analyzer);
1170 recv_transport.SetReceiver(sender_call_->Receiver());
1171
sprangce4aef12015-11-02 07:23:20 -08001172 SetupCommon(&analyzer, &recv_transport);
stefanff483612015-12-21 03:14:00 -08001173 video_receive_configs_[params_.ss.selected_stream].renderer = &analyzer;
pbos14fe7082016-04-20 06:35:56 -07001174 video_send_config_.pre_encode_callback = analyzer.pre_encode_proxy();
stefanff483612015-12-21 03:14:00 -08001175 for (auto& config : video_receive_configs_)
ivica5d6a06c2015-09-17 05:30:24 -07001176 config.pre_decode_callback = &analyzer;
Peter Boströme4499152016-02-05 11:13:28 +01001177 RTC_DCHECK(!video_send_config_.post_encode_callback);
1178 video_send_config_.post_encode_callback = analyzer.encode_timing_proxy();
ivica5d6a06c2015-09-17 05:30:24 -07001179
sprangce4aef12015-11-02 07:23:20 -08001180 if (params_.screenshare.enabled)
1181 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001182
Stefan Holmer9fea80f2016-01-07 17:43:18 +01001183 CreateVideoStreams();
perkja49cbd32016-09-16 07:53:41 -07001184 analyzer.SetSendStream(video_send_stream_);
1185 video_send_stream_->SetSource(analyzer.OutputInterface());
ivica5d6a06c2015-09-17 05:30:24 -07001186
perkja49cbd32016-09-16 07:53:41 -07001187 CreateCapturer();
1188 rtc::VideoSinkWants wants;
1189 capturer_->AddOrUpdateSink(analyzer.InputInterface(), wants);
ivicac1cc8542015-10-08 03:44:06 -07001190
stefanff483612015-12-21 03:14:00 -08001191 video_send_stream_->Start();
1192 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1193 receive_stream->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001194 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001195
1196 analyzer.Wait();
1197
1198 send_transport.StopSending();
1199 recv_transport.StopSending();
1200
ivica2d4e6c52015-09-23 01:57:06 -07001201 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001202 for (VideoReceiveStream* receive_stream : video_receive_streams_)
1203 receive_stream->Stop();
1204 video_send_stream_->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001205
1206 DestroyStreams();
1207
1208 if (graph_data_output_file)
1209 fclose(graph_data_output_file);
1210}
1211
minyue73208662016-08-18 06:28:55 -07001212void VideoQualityTest::RunWithRenderers(const Params& params) {
sprangce4aef12015-11-02 07:23:20 -08001213 params_ = params;
1214 CheckParams();
ivica5d6a06c2015-09-17 05:30:24 -07001215
kwiberg27f982b2016-03-01 11:52:33 -08001216 std::unique_ptr<test::VideoRenderer> local_preview(
kjellander5865f482016-09-08 10:52:38 -07001217 test::VideoRenderer::Create("Local Preview", params_.common.width,
1218 params_.common.height));
sprangce4aef12015-11-02 07:23:20 -08001219 size_t stream_id = params_.ss.selected_stream;
mflodmand1590b22015-12-09 07:07:59 -08001220 std::string title = "Loopback Video";
1221 if (params_.ss.streams.size() > 1) {
1222 std::ostringstream s;
1223 s << stream_id;
1224 title += " - Stream #" + s.str();
sprangce4aef12015-11-02 07:23:20 -08001225 }
mflodmand1590b22015-12-09 07:07:59 -08001226
kwiberg27f982b2016-03-01 11:52:33 -08001227 std::unique_ptr<test::VideoRenderer> loopback_video(
mflodmand1590b22015-12-09 07:07:59 -08001228 test::VideoRenderer::Create(title.c_str(),
1229 params_.ss.streams[stream_id].width,
sprangce4aef12015-11-02 07:23:20 -08001230 params_.ss.streams[stream_id].height));
ivica5d6a06c2015-09-17 05:30:24 -07001231
1232 // TODO(ivica): Remove bitrate_config and use the default Call::Config(), to
1233 // match the full stack tests.
1234 Call::Config call_config;
kjellander5865f482016-09-08 10:52:38 -07001235 call_config.bitrate_config = params_.common.call_bitrate_config;
minyue73208662016-08-18 06:28:55 -07001236
1237 ::VoiceEngineState voe;
kjellander5865f482016-09-08 10:52:38 -07001238 if (params_.audio) {
minyue73208662016-08-18 06:28:55 -07001239 CreateVoiceEngine(&voe, decoder_factory_);
1240 AudioState::Config audio_state_config;
1241 audio_state_config.voice_engine = voe.voice_engine;
1242 call_config.audio_state = AudioState::Create(audio_state_config);
1243 }
1244
kwiberg27f982b2016-03-01 11:52:33 -08001245 std::unique_ptr<Call> call(Call::Create(call_config));
ivica5d6a06c2015-09-17 05:30:24 -07001246
1247 test::LayerFilteringTransport transport(
stefanf116bd02015-10-27 08:29:42 -07001248 params.pipe, call.get(), kPayloadTypeVP8, kPayloadTypeVP9,
kjellander5865f482016-09-08 10:52:38 -07001249 params.common.selected_tl, params_.ss.selected_sl);
ivica5d6a06c2015-09-17 05:30:24 -07001250 // TODO(ivica): Use two calls to be able to merge with RunWithAnalyzer or at
1251 // least share as much code as possible. That way this test would also match
1252 // the full stack tests better.
1253 transport.SetReceiver(call->Receiver());
1254
sprangce4aef12015-11-02 07:23:20 -08001255 SetupCommon(&transport, &transport);
ivica87f83a92015-10-08 05:13:32 -07001256
perkj26091b12016-09-01 01:17:40 -07001257 video_send_config_.pre_encode_callback = local_preview.get();
stefanff483612015-12-21 03:14:00 -08001258 video_receive_configs_[stream_id].renderer = loopback_video.get();
kjellander5865f482016-09-08 10:52:38 -07001259 if (params_.audio && params_.audio_video_sync)
minyue73208662016-08-18 06:28:55 -07001260 video_receive_configs_[stream_id].sync_group = kSyncGroup;
sprangce4aef12015-11-02 07:23:20 -08001261
mflodman48a4beb2016-07-01 13:03:59 +02001262 video_send_config_.suspend_below_min_bitrate =
kjellander5865f482016-09-08 10:52:38 -07001263 params_.common.suspend_below_min_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +02001264
kjellander5865f482016-09-08 10:52:38 -07001265 if (params.common.fec) {
philipel274c1dc2016-05-04 06:21:01 -07001266 video_send_config_.rtp.fec.red_payload_type = kRedPayloadType;
1267 video_send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
1268 video_receive_configs_[stream_id].rtp.fec.red_payload_type =
1269 kRedPayloadType;
1270 video_receive_configs_[stream_id].rtp.fec.ulpfec_payload_type =
1271 kUlpfecPayloadType;
1272 }
1273
sprangce4aef12015-11-02 07:23:20 -08001274 if (params_.screenshare.enabled)
1275 SetupScreenshare();
ivica5d6a06c2015-09-17 05:30:24 -07001276
perkj26091b12016-09-01 01:17:40 -07001277 video_send_stream_ = call->CreateVideoSendStream(
1278 video_send_config_.Copy(), video_encoder_config_.Copy());
minyue73208662016-08-18 06:28:55 -07001279 VideoReceiveStream* video_receive_stream =
Tommi733b5472016-06-10 17:58:01 +02001280 call->CreateVideoReceiveStream(video_receive_configs_[stream_id].Copy());
perkja49cbd32016-09-16 07:53:41 -07001281 CreateCapturer();
1282 video_send_stream_->SetSource(capturer_.get());
ivica5d6a06c2015-09-17 05:30:24 -07001283
minyue73208662016-08-18 06:28:55 -07001284 AudioReceiveStream* audio_receive_stream = nullptr;
kjellander5865f482016-09-08 10:52:38 -07001285 if (params_.audio) {
minyue73208662016-08-18 06:28:55 -07001286 audio_send_config_ = AudioSendStream::Config(&transport);
1287 audio_send_config_.voe_channel_id = voe.send_channel_id;
1288 audio_send_config_.rtp.ssrc = kAudioSendSsrc;
1289
1290 // Add extension to enable audio send side BWE, and allow audio bit rate
1291 // adaptation.
1292 audio_send_config_.rtp.extensions.clear();
kjellander5865f482016-09-08 10:52:38 -07001293 if (params_.common.send_side_bwe) {
minyue73208662016-08-18 06:28:55 -07001294 audio_send_config_.rtp.extensions.push_back(webrtc::RtpExtension(
1295 webrtc::RtpExtension::kTransportSequenceNumberUri,
1296 test::kTransportSequenceNumberExtensionId));
1297 audio_send_config_.min_bitrate_kbps = kOpusMinBitrate / 1000;
1298 audio_send_config_.max_bitrate_kbps = kOpusBitrateFb / 1000;
1299 }
1300
1301 audio_send_stream_ = call->CreateAudioSendStream(audio_send_config_);
1302
1303 AudioReceiveStream::Config audio_config;
1304 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
1305 audio_config.rtcp_send_transport = &transport;
1306 audio_config.voe_channel_id = voe.receive_channel_id;
1307 audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
kjellander5865f482016-09-08 10:52:38 -07001308 audio_config.rtp.transport_cc = params_.common.send_side_bwe;
minyue73208662016-08-18 06:28:55 -07001309 audio_config.rtp.extensions = audio_send_config_.rtp.extensions;
1310 audio_config.decoder_factory = decoder_factory_;
kjellander5865f482016-09-08 10:52:38 -07001311 if (params_.audio_video_sync)
minyue73208662016-08-18 06:28:55 -07001312 audio_config.sync_group = kSyncGroup;
1313
1314 audio_receive_stream =call->CreateAudioReceiveStream(audio_config);
1315
1316 const CodecInst kOpusInst = {120, "OPUS", 48000, 960, 2, 64000};
1317 EXPECT_EQ(0, voe.codec->SetSendCodec(voe.send_channel_id, kOpusInst));
1318 }
1319
1320 // Start sending and receiving video.
1321 video_receive_stream->Start();
stefanff483612015-12-21 03:14:00 -08001322 video_send_stream_->Start();
ivica2d4e6c52015-09-23 01:57:06 -07001323 capturer_->Start();
ivica5d6a06c2015-09-17 05:30:24 -07001324
kjellander5865f482016-09-08 10:52:38 -07001325 if (params_.audio) {
minyue73208662016-08-18 06:28:55 -07001326 // Start receiving audio.
1327 audio_receive_stream->Start();
1328 EXPECT_EQ(0, voe.base->StartPlayout(voe.receive_channel_id));
1329 EXPECT_EQ(0, voe.base->StartReceive(voe.receive_channel_id));
1330
1331 // Start sending audio.
1332 audio_send_stream_->Start();
1333 EXPECT_EQ(0, voe.base->StartSend(voe.send_channel_id));
1334 }
1335
ivica5d6a06c2015-09-17 05:30:24 -07001336 test::PressEnterToContinue();
1337
kjellander5865f482016-09-08 10:52:38 -07001338 if (params_.audio) {
minyue73208662016-08-18 06:28:55 -07001339 // Stop sending audio.
1340 EXPECT_EQ(0, voe.base->StopSend(voe.send_channel_id));
1341 audio_send_stream_->Stop();
1342
1343 // Stop receiving audio.
1344 EXPECT_EQ(0, voe.base->StopReceive(voe.receive_channel_id));
1345 EXPECT_EQ(0, voe.base->StopPlayout(voe.receive_channel_id));
1346 audio_receive_stream->Stop();
1347 }
1348
1349 // Stop receiving and sending video.
ivica2d4e6c52015-09-23 01:57:06 -07001350 capturer_->Stop();
stefanff483612015-12-21 03:14:00 -08001351 video_send_stream_->Stop();
minyue73208662016-08-18 06:28:55 -07001352 video_receive_stream->Stop();
ivica5d6a06c2015-09-17 05:30:24 -07001353
minyue73208662016-08-18 06:28:55 -07001354 call->DestroyVideoReceiveStream(video_receive_stream);
stefanff483612015-12-21 03:14:00 -08001355 call->DestroyVideoSendStream(video_send_stream_);
ivica5d6a06c2015-09-17 05:30:24 -07001356
kjellander5865f482016-09-08 10:52:38 -07001357 if (params_.audio) {
1358 call->DestroyAudioSendStream(audio_send_stream_);
1359 call->DestroyAudioReceiveStream(audio_receive_stream);
minyue73208662016-08-18 06:28:55 -07001360 }
1361
ivica5d6a06c2015-09-17 05:30:24 -07001362 transport.StopSending();
kjellander5865f482016-09-08 10:52:38 -07001363 if (params_.audio)
minyue73208662016-08-18 06:28:55 -07001364 DestroyVoiceEngine(&voe);
ivica5d6a06c2015-09-17 05:30:24 -07001365}
1366
1367} // namespace webrtc