blob: 8fcd566f81a8241de0ad2fa6b1a5f64e20971afb [file] [log] [blame]
pbos@webrtc.org1d096902013-12-13 12:48:05 +00001/*
2 * Copyright (c) 2013 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 */
asaperssonf8cdd182016-03-15 01:00:47 -070010
pbos@webrtc.org1d096902013-12-13 12:48:05 +000011#include <algorithm>
asaperssonf8cdd182016-03-15 01:00:47 -070012#include <limits>
kwibergb25345e2016-03-12 06:10:44 -080013#include <memory>
pbos@webrtc.org1d096902013-12-13 12:48:05 +000014#include <string>
15
Karl Wiberg918f50c2018-07-05 11:40:33 +020016#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Artem Titov46c4e602018-08-17 14:26:54 +020018#include "api/test/simulated_network.h"
Erik Språngef75ebe2018-05-15 15:18:36 +020019#include "api/video/video_bitrate_allocation.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020020#include "api/video_codecs/video_encoder_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "call/call.h"
Artem Titov4e199e92018-08-20 13:30:39 +020022#include "call/fake_network_pipe.h"
23#include "call/simulated_network.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "logging/rtc_event_log/rtc_event_log.h"
25#include "modules/audio_coding/include/audio_coding_module.h"
Artem Titov3faa8322018-03-07 14:44:00 +010026#include "modules/audio_device/include/test_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_mixer/audio_mixer_impl.h"
28#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Alex Narestd0e196b2017-11-22 17:22:35 +010029#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/thread_annotations.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020032#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "test/call_test.h"
34#include "test/direct_transport.h"
35#include "test/drifting_clock.h"
36#include "test/encoder_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "test/fake_encoder.h"
38#include "test/field_trial.h"
39#include "test/frame_generator.h"
40#include "test/frame_generator_capturer.h"
41#include "test/gtest.h"
Niels Möllerae4237e2018-10-05 11:28:38 +020042#include "test/null_transport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/rtp_rtcp_observer.h"
44#include "test/single_threaded_task_queue.h"
45#include "test/testsupport/fileutils.h"
46#include "test/testsupport/perf_test.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020047#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "video/transport_adapter.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000049
danilchap9c6a0c72016-02-10 10:54:47 -080050using webrtc::test::DriftingClock;
danilchap9c6a0c72016-02-10 10:54:47 -080051
pbos@webrtc.org1d096902013-12-13 12:48:05 +000052namespace webrtc {
53
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000054class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000055 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020056 enum class FecMode { kOn, kOff };
57 enum class CreateOrder { kAudioFirst, kVideoFirst };
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +010058 void TestAudioVideoSync(FecMode fec,
59 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -080060 float video_ntp_speed,
61 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +010062 float audio_rtp_speed,
63 const std::string& test_label);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000064
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000065 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
66
Artem Titov46c4e602018-08-17 14:26:54 +020067 void TestCaptureNtpTime(const DefaultNetworkSimulationConfig& net_config,
wu@webrtc.orgcd701192014-04-24 22:10:24 +000068 int threshold_ms,
69 int start_time_ms,
70 int run_time_ms);
Alex Narestd0e196b2017-11-22 17:22:35 +010071 void TestMinAudioVideoBitrate(bool use_bitrate_allocation_strategy,
72 int test_bitrate_from,
73 int test_bitrate_to,
74 int test_bitrate_step,
75 int min_bwe,
76 int start_bwe,
77 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000078};
79
asaperssonf8cdd182016-03-15 01:00:47 -070080class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070081 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000082 static const int kInSyncThresholdMs = 50;
83 static const int kStartupTimeMs = 2000;
84 static const int kMinRunTimeMs = 30000;
85
86 public:
Edward Lemur947f3fe2017-12-28 15:50:33 +010087 explicit VideoRtcpAndSyncObserver(Clock* clock, const std::string& test_label)
asaperssonf8cdd182016-03-15 01:00:47 -070088 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
89 clock_(clock),
Edward Lemur947f3fe2017-12-28 15:50:33 +010090 test_label_(test_label),
pbos@webrtc.org1d096902013-12-13 12:48:05 +000091 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -070092 first_time_in_sync_(-1),
93 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000094
nisseeb83a1a2016-03-21 01:27:56 -070095 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -070096 VideoReceiveStream::Stats stats;
97 {
98 rtc::CritScope lock(&crit_);
99 if (receive_stream_)
100 stats = receive_stream_->GetStats();
101 }
102 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
103 return;
104
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000105 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000106 int64_t time_since_creation = now_ms - creation_time_ms_;
107 // During the first couple of seconds audio and video can falsely be
108 // estimated as being synchronized. We don't want to trigger on those.
109 if (time_since_creation < kStartupTimeMs)
110 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700111 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000112 if (first_time_in_sync_ == -1) {
113 first_time_in_sync_ = now_ms;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100114 webrtc::test::PrintResult("sync_convergence_time", test_label_,
115 "synchronization", time_since_creation, "ms",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000116 false);
117 }
118 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100119 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000120 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200121 if (first_time_in_sync_ != -1)
122 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000123 }
124
asaperssonf8cdd182016-03-15 01:00:47 -0700125 void set_receive_stream(VideoReceiveStream* receive_stream) {
126 rtc::CritScope lock(&crit_);
127 receive_stream_ = receive_stream;
128 }
129
danilchap46b89b92016-06-03 09:27:37 -0700130 void PrintResults() {
Edward Lemur947f3fe2017-12-28 15:50:33 +0100131 test::PrintResultList("stream_offset", test_label_, "synchronization",
Edward Lemur2f061682017-11-24 13:40:01 +0100132 sync_offset_ms_list_, "ms", false);
danilchap46b89b92016-06-03 09:27:37 -0700133 }
134
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000135 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000136 Clock* const clock_;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100137 std::string test_label_;
stefanf116bd02015-10-27 08:29:42 -0700138 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000139 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700140 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700141 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100142 std::vector<double> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000143};
144
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100145void CallPerfTest::TestAudioVideoSync(FecMode fec,
146 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800147 float video_ntp_speed,
148 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +0100149 float audio_rtp_speed,
150 const std::string& test_label) {
pbos8fc7fa72015-07-15 08:02:58 -0700151 const char* kSyncGroup = "av_sync";
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100152 const uint32_t kAudioSendSsrc = 1234;
153 const uint32_t kAudioRecvSsrc = 5678;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000154
Artem Titov46c4e602018-08-17 14:26:54 +0200155 DefaultNetworkSimulationConfig audio_net_config;
mflodman3d7db262016-04-29 00:57:13 -0700156 audio_net_config.queue_delay_ms = 500;
157 audio_net_config.loss_percent = 5;
minyue20c84cc2017-04-10 16:57:57 -0700158
Edward Lemur947f3fe2017-12-28 15:50:33 +0100159 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), test_label);
eladalon413ee9a2017-08-22 04:02:52 -0700160
minyue20c84cc2017-04-10 16:57:57 -0700161 std::map<uint8_t, MediaType> audio_pt_map;
162 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700163
eladalon413ee9a2017-08-22 04:02:52 -0700164 std::unique_ptr<test::PacketTransport> audio_send_transport;
165 std::unique_ptr<test::PacketTransport> video_send_transport;
166 std::unique_ptr<test::PacketTransport> receive_transport;
Niels Möllerae4237e2018-10-05 11:28:38 +0200167 test::NullTransport rtcp_send_transport;
mflodman3d7db262016-04-29 00:57:13 -0700168
eladalon413ee9a2017-08-22 04:02:52 -0700169 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100170 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700171 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700172
eladalon413ee9a2017-08-22 04:02:52 -0700173 task_queue_.SendTask([&]() {
174 metrics::Reset();
Artem Titov3faa8322018-03-07 14:44:00 +0100175 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device =
176 TestAudioDeviceModule::CreateTestAudioDeviceModule(
177 TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000),
178 TestAudioDeviceModule::CreateDiscardRenderer(48000),
179 audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100180 EXPECT_EQ(0, fake_audio_device->Init());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000181
eladalon413ee9a2017-08-22 04:02:52 -0700182 AudioState::Config send_audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -0700183 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +0100184 send_audio_state_config.audio_processing =
185 AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100186 send_audio_state_config.audio_device_module = fake_audio_device;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200187 Call::Config sender_config(send_event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000188
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100189 auto audio_state = AudioState::Create(send_audio_state_config);
190 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
191 sender_config.audio_state = audio_state;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200192 Call::Config receiver_config(recv_event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100193 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700194 CreateCalls(sender_config, receiver_config);
195
196 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
197 std::inserter(audio_pt_map, audio_pt_map.end()),
198 [](const std::pair<const uint8_t, MediaType>& pair) {
199 return pair.second == MediaType::AUDIO;
200 });
201 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
202 std::inserter(video_pt_map, video_pt_map.end()),
203 [](const std::pair<const uint8_t, MediaType>& pair) {
204 return pair.second == MediaType::VIDEO;
205 });
206
Karl Wiberg918f50c2018-07-05 11:40:33 +0200207 audio_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700208 &task_queue_, sender_call_.get(), &observer,
Artem Titov4e199e92018-08-20 13:30:39 +0200209 test::PacketTransport::kSender, audio_pt_map,
210 absl::make_unique<FakeNetworkPipe>(
211 Clock::GetRealTimeClock(),
212 absl::make_unique<SimulatedNetwork>(audio_net_config)));
eladalon413ee9a2017-08-22 04:02:52 -0700213 audio_send_transport->SetReceiver(receiver_call_->Receiver());
214
Karl Wiberg918f50c2018-07-05 11:40:33 +0200215 video_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700216 &task_queue_, sender_call_.get(), &observer,
217 test::PacketTransport::kSender, video_pt_map,
Artem Titov4e199e92018-08-20 13:30:39 +0200218 absl::make_unique<FakeNetworkPipe>(
219 Clock::GetRealTimeClock(), absl::make_unique<SimulatedNetwork>(
220 DefaultNetworkSimulationConfig())));
eladalon413ee9a2017-08-22 04:02:52 -0700221 video_send_transport->SetReceiver(receiver_call_->Receiver());
222
Karl Wiberg918f50c2018-07-05 11:40:33 +0200223 receive_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700224 &task_queue_, receiver_call_.get(), &observer,
225 test::PacketTransport::kReceiver, payload_type_map_,
Artem Titov4e199e92018-08-20 13:30:39 +0200226 absl::make_unique<FakeNetworkPipe>(
227 Clock::GetRealTimeClock(), absl::make_unique<SimulatedNetwork>(
228 DefaultNetworkSimulationConfig())));
eladalon413ee9a2017-08-22 04:02:52 -0700229 receive_transport->SetReceiver(sender_call_->Receiver());
230
231 CreateSendConfig(1, 0, 0, video_send_transport.get());
232 CreateMatchingReceiveConfigs(receive_transport.get());
233
234 AudioSendStream::Config audio_send_config(audio_send_transport.get());
eladalon413ee9a2017-08-22 04:02:52 -0700235 audio_send_config.rtp.ssrc = kAudioSendSsrc;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100236 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
237 kAudioSendPayloadType, {"ISAC", 16000, 1});
eladalon413ee9a2017-08-22 04:02:52 -0700238 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
239 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
240
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200241 GetVideoSendConfig()->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
eladalon413ee9a2017-08-22 04:02:52 -0700242 if (fec == FecMode::kOn) {
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200243 GetVideoSendConfig()->rtp.ulpfec.red_payload_type = kRedPayloadType;
244 GetVideoSendConfig()->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700245 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
246 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700247 }
248 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
249 video_receive_configs_[0].renderer = &observer;
250 video_receive_configs_[0].sync_group = kSyncGroup;
251
252 AudioReceiveStream::Config audio_recv_config;
253 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
254 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
Niels Möllerae4237e2018-10-05 11:28:38 +0200255 audio_recv_config.rtcp_send_transport = &rtcp_send_transport;
eladalon413ee9a2017-08-22 04:02:52 -0700256 audio_recv_config.sync_group = kSyncGroup;
Niels Möller2784a032018-03-28 14:16:04 +0200257 audio_recv_config.decoder_factory = audio_decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700258 audio_recv_config.decoder_map = {
259 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
260
261 if (create_first == CreateOrder::kAudioFirst) {
262 audio_receive_stream =
263 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
264 CreateVideoStreams();
265 } else {
266 CreateVideoStreams();
267 audio_receive_stream =
268 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
269 }
270 EXPECT_EQ(1u, video_receive_streams_.size());
271 observer.set_receive_stream(video_receive_streams_[0]);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200272 drifting_clock = absl::make_unique<DriftingClock>(clock_, video_ntp_speed);
eladalon413ee9a2017-08-22 04:02:52 -0700273 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
274 kDefaultFramerate, kDefaultWidth,
275 kDefaultHeight);
276
277 Start();
278
279 audio_send_stream->Start();
280 audio_receive_stream->Start();
281 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000282
Peter Boström5811a392015-12-10 13:02:50 +0100283 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000284 << "Timed out while waiting for audio and video to be synchronized.";
285
eladalon413ee9a2017-08-22 04:02:52 -0700286 task_queue_.SendTask([&]() {
287 audio_send_stream->Stop();
288 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000289
eladalon413ee9a2017-08-22 04:02:52 -0700290 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000291
eladalon413ee9a2017-08-22 04:02:52 -0700292 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100293
eladalon413ee9a2017-08-22 04:02:52 -0700294 video_send_transport.reset();
295 audio_send_transport.reset();
296 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100297
eladalon413ee9a2017-08-22 04:02:52 -0700298 sender_call_->DestroyAudioSendStream(audio_send_stream);
299 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000300
eladalon413ee9a2017-08-22 04:02:52 -0700301 DestroyCalls();
eladalon413ee9a2017-08-22 04:02:52 -0700302 });
asaperssonf8cdd182016-03-15 01:00:47 -0700303
danilchap46b89b92016-06-03 09:27:37 -0700304 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800305
306 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800307 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
ilnik5328b9e2017-02-21 05:20:28 -0800308 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
309 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000310}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000311
Niels Möller9a750612018-08-09 11:04:32 +0200312TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithoutClockDrift) {
313 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
314 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
315 DriftingClock::kNoDrift, "_video_no_drift");
316}
317
danilchapac287ee2016-02-29 12:17:04 -0800318TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100319 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
320 DriftingClock::PercentsFaster(10.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100321 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
322 "_video_ntp_drift");
danilchap9c6a0c72016-02-10 10:54:47 -0800323}
324
danilchap9c6a0c72016-02-10 10:54:47 -0800325TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100326 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
327 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800328 DriftingClock::PercentsSlower(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100329 DriftingClock::PercentsFaster(30.0f), "_audio_faster");
danilchap9c6a0c72016-02-10 10:54:47 -0800330}
331
332TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100333 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
334 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800335 DriftingClock::PercentsFaster(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100336 DriftingClock::PercentsSlower(30.0f), "_video_faster");
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000337}
338
Artem Titov46c4e602018-08-17 14:26:54 +0200339void CallPerfTest::TestCaptureNtpTime(
340 const DefaultNetworkSimulationConfig& net_config,
341 int threshold_ms,
342 int start_time_ms,
343 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000344 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700345 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000346 public:
Artem Titov46c4e602018-08-17 14:26:54 +0200347 CaptureNtpTimeObserver(const DefaultNetworkSimulationConfig& net_config,
stefane74eef12016-01-08 06:47:13 -0800348 int threshold_ms,
349 int start_time_ms,
350 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700351 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800352 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000353 clock_(Clock::GetRealTimeClock()),
354 threshold_ms_(threshold_ms),
355 start_time_ms_(start_time_ms),
356 run_time_ms_(run_time_ms),
357 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000358 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000359 rtp_start_timestamp_set_(false),
360 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000361
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000362 private:
eladalon413ee9a2017-08-22 04:02:52 -0700363 test::PacketTransport* CreateSendTransport(
364 test::SingleThreadedTaskQueueForTesting* task_queue,
365 Call* sender_call) override {
Artem Titov4e199e92018-08-20 13:30:39 +0200366 return new test::PacketTransport(
367 task_queue, sender_call, this, test::PacketTransport::kSender,
368 payload_type_map_,
369 absl::make_unique<FakeNetworkPipe>(
370 Clock::GetRealTimeClock(),
371 absl::make_unique<SimulatedNetwork>(net_config_)));
stefane74eef12016-01-08 06:47:13 -0800372 }
373
eladalon413ee9a2017-08-22 04:02:52 -0700374 test::PacketTransport* CreateReceiveTransport(
375 test::SingleThreadedTaskQueueForTesting* task_queue) override {
Artem Titov4e199e92018-08-20 13:30:39 +0200376 return new test::PacketTransport(
377 task_queue, nullptr, this, test::PacketTransport::kReceiver,
378 payload_type_map_,
379 absl::make_unique<FakeNetworkPipe>(
380 Clock::GetRealTimeClock(),
381 absl::make_unique<SimulatedNetwork>(net_config_)));
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100382 }
383
nisseeb83a1a2016-03-21 01:27:56 -0700384 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700385 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000386 if (video_frame.ntp_time_ms() <= 0) {
387 // Haven't got enough RTCP SR in order to calculate the capture ntp
388 // time.
389 return;
390 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000391
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000392 int64_t now_ms = clock_->TimeInMilliseconds();
393 int64_t time_since_creation = now_ms - creation_time_ms_;
394 if (time_since_creation < start_time_ms_) {
395 // Wait for |start_time_ms_| before start measuring.
396 return;
397 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000398
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000399 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100400 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000401 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000402
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000403 FrameCaptureTimeList::iterator iter =
404 capture_time_list_.find(video_frame.timestamp());
405 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000406
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000407 // The real capture time has been wrapped to uint32_t before converted
408 // to rtp timestamp in the sender side. So here we convert the estimated
409 // capture time to a uint32_t 90k timestamp also for comparing.
410 uint32_t estimated_capture_timestamp =
411 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
412 uint32_t real_capture_timestamp = iter->second;
413 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
414 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700415 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000416
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000417 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
418 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000419
nisseef8b61e2016-04-29 06:09:15 -0700420 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700421 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000422 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000423 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000424
425 if (!rtp_start_timestamp_set_) {
426 // Calculate the rtp timestamp offset in order to calculate the real
427 // capture time.
428 uint32_t first_capture_timestamp =
429 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
430 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
431 rtp_start_timestamp_set_ = true;
432 }
433
434 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
435 capture_time_list_.insert(
436 capture_time_list_.end(),
437 std::make_pair(header.timestamp, capture_timestamp));
438 return SEND_PACKET;
439 }
440
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000441 void OnFrameGeneratorCapturerCreated(
442 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000443 capturer_ = frame_generator_capturer;
444 }
445
stefanff483612015-12-21 03:14:00 -0800446 void ModifyVideoConfigs(
447 VideoSendStream::Config* send_config,
448 std::vector<VideoReceiveStream::Config>* receive_configs,
449 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000450 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000451 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000452 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000453 }
454
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000455 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100456 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
457 "estimated capture NTP time to be "
458 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700459 test::PrintResultList("capture_ntp_time", "", "real - estimated",
Edward Lemur2f061682017-11-24 13:40:01 +0100460 time_offset_ms_list_, "ms", true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000461 }
462
stefanf116bd02015-10-27 08:29:42 -0700463 rtc::CriticalSection crit_;
Artem Titov46c4e602018-08-17 14:26:54 +0200464 const DefaultNetworkSimulationConfig net_config_;
stefanf116bd02015-10-27 08:29:42 -0700465 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000466 int threshold_ms_;
467 int start_time_ms_;
468 int run_time_ms_;
469 int64_t creation_time_ms_;
470 test::FrameGeneratorCapturer* capturer_;
471 bool rtp_start_timestamp_set_;
472 uint32_t rtp_start_timestamp_;
473 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700474 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100475 std::vector<double> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800476 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000477
stefane74eef12016-01-08 06:47:13 -0800478 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000479}
480
Alex Loiko5aea38c2017-09-27 13:10:28 +0200481// Flaky tests, disabled on Mac due to webrtc:8291.
482#if !(defined(WEBRTC_MAC))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000483TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
Artem Titov46c4e602018-08-17 14:26:54 +0200484 DefaultNetworkSimulationConfig net_config;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000485 net_config.queue_delay_ms = 100;
486 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
487 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000488 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000489 const int kStartTimeMs = 10000;
490 const int kRunTimeMs = 20000;
491 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
492}
493
wu@webrtc.org0224c202014-05-05 17:42:43 +0000494TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
Artem Titov46c4e602018-08-17 14:26:54 +0200495 DefaultNetworkSimulationConfig net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000496 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000497 net_config.delay_standard_deviation_ms = 10;
498 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
499 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000500 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000501 const int kStartTimeMs = 10000;
502 const int kRunTimeMs = 20000;
503 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
504}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200505#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800506
perkj803d97f2016-11-01 11:45:46 -0700507TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700508 // Minimal normal usage at the start, then 30s overuse to allow filter to
509 // settle, and then 80s underuse to allow plenty of time for rampup again.
510 test::ScopedFieldTrials fake_overuse_settings(
511 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
512
perkj803d97f2016-11-01 11:45:46 -0700513 class LoadObserver : public test::SendTest,
514 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000515 public:
Åsa Persson8c1bf952018-09-13 10:42:19 +0200516 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kInit) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000517
perkj803d97f2016-11-01 11:45:46 -0700518 void OnFrameGeneratorCapturerCreated(
519 test::FrameGeneratorCapturer* frame_generator_capturer) override {
520 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800521 // Set a high initial resolution to be sure that we can scale down.
522 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700523 }
524
525 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
526 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700527 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700528 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
529 const rtc::VideoSinkWants& wants) override {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200530 // At kStart expect CPU overuse. Then expect CPU underuse when the encoder
perkj803d97f2016-11-01 11:45:46 -0700531 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700532 switch (test_phase_) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200533 case TestPhase::kInit:
534 // Max framerate should be set initially.
535 if (wants.max_framerate_fps != std::numeric_limits<int>::max() &&
536 wants.max_pixel_count == std::numeric_limits<int>::max()) {
537 test_phase_ = TestPhase::kStart;
538 } else {
539 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
540 << wants.max_pixel_count << ", target res = "
541 << wants.target_pixel_count.value_or(-1)
542 << ", max fps = " << wants.max_framerate_fps;
543 }
544 break;
sprangc5d62e22017-04-02 23:53:04 -0700545 case TestPhase::kStart:
546 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700547 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
548 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700549 test_phase_ = TestPhase::kAdaptedDown;
550 } else {
551 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
552 << wants.max_pixel_count << ", target res = "
553 << wants.target_pixel_count.value_or(-1)
554 << ", max fps = " << wants.max_framerate_fps;
555 }
556 break;
557 case TestPhase::kAdaptedDown:
558 // On adapting up, the adaptation counter will again be at zero, and
559 // so all constraints will be reset.
560 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
561 !wants.target_pixel_count) {
562 test_phase_ = TestPhase::kAdaptedUp;
563 observation_complete_.Set();
564 } else {
565 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
566 << wants.max_pixel_count << ", target res = "
567 << wants.target_pixel_count.value_or(-1)
568 << ", max fps = " << wants.max_framerate_fps;
569 }
570 break;
571 case TestPhase::kAdaptedUp:
572 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
573 << wants.max_pixel_count << ", target res = "
574 << wants.target_pixel_count.value_or(-1)
575 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700576 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000577 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000578
stefanff483612015-12-21 03:14:00 -0800579 void ModifyVideoConfigs(
580 VideoSendStream::Config* send_config,
581 std::vector<VideoReceiveStream::Config>* receive_configs,
Yves Gerey665174f2018-06-19 15:03:05 +0200582 VideoEncoderConfig* encoder_config) override {}
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000583
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000584 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100585 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000586 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000587
Åsa Persson8c1bf952018-09-13 10:42:19 +0200588 enum class TestPhase {
589 kInit,
590 kStart,
591 kAdaptedDown,
592 kAdaptedUp
593 } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700594 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000595
stefane74eef12016-01-08 06:47:13 -0800596 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000597}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000598
599void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
600 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000601 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000602 static const int kMinAcceptableTransmitBitrate = 130;
603 static const int kMaxAcceptableTransmitBitrate = 170;
604 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700605 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700606 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000607 public:
608 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000609 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000610 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200611 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000612 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200613 min_acceptable_bitrate_(using_min_transmit_bitrate
614 ? kMinAcceptableTransmitBitrate
615 : (kMaxEncodeBitrateKbps -
616 kAcceptableBitrateErrorMargin / 2)),
617 max_acceptable_bitrate_(using_min_transmit_bitrate
618 ? kMaxAcceptableTransmitBitrate
619 : (kMaxEncodeBitrateKbps +
620 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000621 num_bitrate_observations_in_range_(0) {}
622
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000623 private:
stefanf116bd02015-10-27 08:29:42 -0700624 // TODO(holmer): Run this with a timer instead of once per packet.
625 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000626 VideoSendStream::Stats stats = send_stream_->GetStats();
627 if (stats.substreams.size() > 0) {
kwibergaf476c72016-11-28 15:21:39 -0800628 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000629 int bitrate_kbps =
630 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200631 if (bitrate_kbps > min_acceptable_bitrate_ &&
632 bitrate_kbps < max_acceptable_bitrate_) {
633 converged_ = true;
634 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000635 if (num_bitrate_observations_in_range_ ==
636 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100637 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000638 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200639 if (converged_)
640 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000641 }
stefanf116bd02015-10-27 08:29:42 -0700642 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000643 }
644
stefanff483612015-12-21 03:14:00 -0800645 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000646 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000647 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000648 send_stream_ = send_stream;
649 }
650
stefanff483612015-12-21 03:14:00 -0800651 void ModifyVideoConfigs(
652 VideoSendStream::Config* send_config,
653 std::vector<VideoReceiveStream::Config>* receive_configs,
654 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000655 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000656 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000657 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700658 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000659 }
660 }
661
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000662 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100663 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700664 test::PrintResultList(
665 "bitrate_stats_",
666 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
667 : "without_min_transmit_bitrate"),
Edward Lemur2f061682017-11-24 13:40:01 +0100668 "bitrate_kbps", bitrate_kbps_list_, "kbps", false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000669 }
670
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000671 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200672 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000673 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200674 const int min_acceptable_bitrate_;
675 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000676 int num_bitrate_observations_in_range_;
Edward Lemur2f061682017-11-24 13:40:01 +0100677 std::vector<double> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000678 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000679
Niels Möller4db138e2018-04-19 09:04:13 +0200680 fake_encoder_max_bitrate_ = kMaxEncodeBitrateKbps;
stefane74eef12016-01-08 06:47:13 -0800681 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000682}
683
Yves Gerey665174f2018-06-19 15:03:05 +0200684TEST_F(CallPerfTest, PadsToMinTransmitBitrate) {
685 TestMinTransmitBitrate(true);
686}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000687
688TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
689 TestMinTransmitBitrate(false);
690}
691
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800692// TODO(bugs.webrtc.org/8878)
693#if defined(WEBRTC_MAC)
694#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
695 DISABLED_KeepsHighBitrateWhenReconfiguringSender
696#else
697#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
698 KeepsHighBitrateWhenReconfiguringSender
699#endif
700TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000701 static const uint32_t kInitialBitrateKbps = 400;
702 static const uint32_t kReconfigureThresholdKbps = 600;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000703
perkjfa10b552016-10-02 23:45:26 -0700704 class VideoStreamFactory
705 : public VideoEncoderConfig::VideoStreamFactoryInterface {
706 public:
707 VideoStreamFactory() {}
708
709 private:
710 std::vector<VideoStream> CreateEncoderStreams(
711 int width,
712 int height,
713 const VideoEncoderConfig& encoder_config) override {
714 std::vector<VideoStream> streams =
715 test::CreateVideoStreams(width, height, encoder_config);
716 streams[0].min_bitrate_bps = 50000;
717 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
718 return streams;
719 }
720 };
721
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000722 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
723 public:
724 BitrateObserver()
725 : EndToEndTest(kDefaultTimeoutMs),
726 FakeEncoder(Clock::GetRealTimeClock()),
Peter Boström5811a392015-12-10 13:02:50 +0100727 time_to_reconfigure_(false, false),
sprang867fb522015-08-03 04:38:41 -0700728 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100729 last_set_bitrate_kbps_(0),
730 send_stream_(nullptr),
Niels Möller4db138e2018-04-19 09:04:13 +0200731 frame_generator_(nullptr),
732 encoder_factory_(this) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000733
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000734 int32_t InitEncode(const VideoCodec* config,
735 int32_t number_of_cores,
736 size_t max_payload_size) override {
perkjfa10b552016-10-02 23:45:26 -0700737 ++encoder_inits_;
738 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700739 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100740 // |expected_bitrate| is affected by bandwidth estimation before the
741 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100742 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
743 ? last_set_bitrate_kbps_
744 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100745 EXPECT_EQ(expected_bitrate, config->startBitrate)
746 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700747 EXPECT_EQ(kDefaultWidth, config->width);
748 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100749 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700750 EXPECT_EQ(2 * kDefaultWidth, config->width);
751 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100752 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
philipel0676f222018-04-17 16:12:21 +0200753 EXPECT_GT(config->startBitrate, kReconfigureThresholdKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000754 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100755 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000756 }
757 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
758 }
759
Erik Språng566124a2018-04-23 12:32:22 +0200760 int32_t SetRateAllocation(const VideoBitrateAllocation& rate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100761 uint32_t framerate) override {
762 last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100763 if (encoder_inits_ == 1 &&
Erik Språng08127a92016-11-16 16:41:30 +0100764 rate_allocation.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100765 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000766 }
Erik Språng08127a92016-11-16 16:41:30 +0100767 return FakeEncoder::SetRateAllocation(rate_allocation, framerate);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000768 }
769
Sebastian Jansson72582242018-07-13 13:19:42 +0200770 void ModifySenderCallConfig(Call::Config* config) override {
771 config->bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000772 }
773
stefanff483612015-12-21 03:14:00 -0800774 void ModifyVideoConfigs(
775 VideoSendStream::Config* send_config,
776 std::vector<VideoReceiveStream::Config>* receive_configs,
777 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200778 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Per21d45d22016-10-30 21:37:57 +0100779 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700780 encoder_config->video_stream_factory =
781 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000782
perkj26091b12016-09-01 01:17:40 -0700783 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000784 }
785
stefanff483612015-12-21 03:14:00 -0800786 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000787 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000788 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000789 send_stream_ = send_stream;
790 }
791
perkjfa10b552016-10-02 23:45:26 -0700792 void OnFrameGeneratorCapturerCreated(
793 test::FrameGeneratorCapturer* frame_generator_capturer) override {
794 frame_generator_ = frame_generator_capturer;
795 }
796
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000797 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100798 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000799 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700800 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700801 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100802 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000803 << "Timed out while waiting for a couple of high bitrate estimates "
804 "after reconfiguring the send stream.";
805 }
806
807 private:
Peter Boström5811a392015-12-10 13:02:50 +0100808 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000809 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100810 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000811 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700812 test::FrameGeneratorCapturer* frame_generator_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200813 test::VideoEncoderProxyFactory encoder_factory_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000814 VideoEncoderConfig encoder_config_;
815 } test;
816
stefane74eef12016-01-08 06:47:13 -0800817 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000818}
819
Alex Narestd0e196b2017-11-22 17:22:35 +0100820// Discovers the minimal supported audio+video bitrate. The test bitrate is
821// considered supported if Rtt does not go above 400ms with the network
822// contrained to the test bitrate.
823//
824// |use_bitrate_allocation_strategy| use AudioPriorityBitrateAllocationStrategy
825// |test_bitrate_from test_bitrate_to| bitrate constraint range
826// |test_bitrate_step| bitrate constraint update step during the test
827// |min_bwe max_bwe| BWE range
828// |start_bwe| initial BWE
829void CallPerfTest::TestMinAudioVideoBitrate(
830 bool use_bitrate_allocation_strategy,
831 int test_bitrate_from,
832 int test_bitrate_to,
833 int test_bitrate_step,
834 int min_bwe,
835 int start_bwe,
836 int max_bwe) {
837 static const std::string kAudioTrackId = "audio_track_0";
838 static constexpr uint32_t kSufficientAudioBitrateBps = 16000;
839 static constexpr int kOpusMinBitrateBps = 6000;
840 static constexpr int kOpusBitrateFbBps = 32000;
841 static constexpr int kBitrateStabilizationMs = 10000;
842 static constexpr int kBitrateMeasurements = 10;
843 static constexpr int kBitrateMeasurementMs = 1000;
844 static constexpr int kMinGoodRttMs = 400;
845
846 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
847 public:
848 MinVideoAndAudioBitrateTester(bool use_bitrate_allocation_strategy,
849 int test_bitrate_from,
850 int test_bitrate_to,
851 int test_bitrate_step,
852 int min_bwe,
853 int start_bwe,
854 int max_bwe)
855 : EndToEndTest(),
856 allocation_strategy_(new rtc::AudioPriorityBitrateAllocationStrategy(
857 kAudioTrackId,
858 kSufficientAudioBitrateBps)),
859 use_bitrate_allocation_strategy_(use_bitrate_allocation_strategy),
860 test_bitrate_from_(test_bitrate_from),
861 test_bitrate_to_(test_bitrate_to),
862 test_bitrate_step_(test_bitrate_step),
863 min_bwe_(min_bwe),
864 start_bwe_(start_bwe),
865 max_bwe_(max_bwe) {}
866
867 protected:
Artem Titov46c4e602018-08-17 14:26:54 +0200868 DefaultNetworkSimulationConfig GetFakeNetworkPipeConfig() {
869 DefaultNetworkSimulationConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100870 pipe_config.link_capacity_kbps = test_bitrate_from_;
871 return pipe_config;
872 }
873
874 test::PacketTransport* CreateSendTransport(
875 test::SingleThreadedTaskQueueForTesting* task_queue,
876 Call* sender_call) override {
Artem Titov631cafa2018-08-21 21:01:00 +0200877 auto network =
878 absl::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig());
879 send_simulated_network_ = network.get();
880 return new test::PacketTransport(
881 task_queue, sender_call, this, test::PacketTransport::kSender,
882 test::CallTest::payload_type_map_,
883 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
884 std::move(network)));
Alex Narestd0e196b2017-11-22 17:22:35 +0100885 }
886
887 test::PacketTransport* CreateReceiveTransport(
888 test::SingleThreadedTaskQueueForTesting* task_queue) override {
Artem Titov631cafa2018-08-21 21:01:00 +0200889 auto network =
890 absl::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig());
891 receive_simulated_network_ = network.get();
892 return new test::PacketTransport(
893 task_queue, nullptr, this, test::PacketTransport::kReceiver,
894 test::CallTest::payload_type_map_,
895 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
896 std::move(network)));
Alex Narestd0e196b2017-11-22 17:22:35 +0100897 }
898
899 void PerformTest() override {
900 int last_passed_test_bitrate = -1;
901 for (int test_bitrate = test_bitrate_from_;
902 test_bitrate_from_ < test_bitrate_to_
903 ? test_bitrate <= test_bitrate_to_
904 : test_bitrate >= test_bitrate_to_;
905 test_bitrate += test_bitrate_step_) {
Artem Titov46c4e602018-08-17 14:26:54 +0200906 DefaultNetworkSimulationConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100907 pipe_config.link_capacity_kbps = test_bitrate;
Artem Titov631cafa2018-08-21 21:01:00 +0200908 send_simulated_network_->SetConfig(pipe_config);
909 receive_simulated_network_->SetConfig(pipe_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100910
911 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
912 kBitrateStabilizationMs);
913
914 int64_t avg_rtt = 0;
915 for (int i = 0; i < kBitrateMeasurements; i++) {
916 Call::Stats call_stats = sender_call_->GetStats();
917 avg_rtt += call_stats.rtt_ms;
918 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
919 kBitrateMeasurementMs);
920 }
921 avg_rtt = avg_rtt / kBitrateMeasurements;
922 if (avg_rtt > kMinGoodRttMs) {
923 break;
924 } else {
925 last_passed_test_bitrate = test_bitrate;
926 }
927 }
928 EXPECT_GT(last_passed_test_bitrate, -1)
929 << "Minimum supported bitrate out of the test scope";
Edward Lemur7f331fa2018-01-08 17:35:51 +0100930 webrtc::test::PrintResult(
931 "min_test_bitrate_",
932 use_bitrate_allocation_strategy_ ? "with_allocation_strategy"
933 : "no_allocation_strategy",
934 "min_bitrate", last_passed_test_bitrate, "kbps", false);
Alex Narestd0e196b2017-11-22 17:22:35 +0100935 }
936
937 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
938 sender_call_ = sender_call;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100939 BitrateConstraints bitrate_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100940 bitrate_config.min_bitrate_bps = min_bwe_;
941 bitrate_config.start_bitrate_bps = start_bwe_;
942 bitrate_config.max_bitrate_bps = max_bwe_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100943 sender_call->GetTransportControllerSend()->SetSdpBitrateParameters(
944 bitrate_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100945 if (use_bitrate_allocation_strategy_) {
946 sender_call->SetBitrateAllocationStrategy(
947 std::move(allocation_strategy_));
948 }
949 }
950
951 size_t GetNumVideoStreams() const override { return 1; }
952
953 size_t GetNumAudioStreams() const override { return 1; }
954
955 void ModifyAudioConfigs(
956 AudioSendStream::Config* send_config,
957 std::vector<AudioReceiveStream::Config>* receive_configs) override {
958 if (use_bitrate_allocation_strategy_) {
959 send_config->track_id = kAudioTrackId;
960 send_config->min_bitrate_bps = kOpusMinBitrateBps;
961 send_config->max_bitrate_bps = kOpusBitrateFbBps;
962 } else {
963 send_config->send_codec_spec->target_bitrate_bps =
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200964 absl::optional<int>(kOpusBitrateFbBps);
Alex Narestd0e196b2017-11-22 17:22:35 +0100965 }
966 }
967
968 private:
969 std::unique_ptr<rtc::BitrateAllocationStrategy> allocation_strategy_;
970 const bool use_bitrate_allocation_strategy_;
971 const int test_bitrate_from_;
972 const int test_bitrate_to_;
973 const int test_bitrate_step_;
974 const int min_bwe_;
975 const int start_bwe_;
976 const int max_bwe_;
Artem Titov631cafa2018-08-21 21:01:00 +0200977 SimulatedNetwork* send_simulated_network_;
978 SimulatedNetwork* receive_simulated_network_;
Alex Narestd0e196b2017-11-22 17:22:35 +0100979 Call* sender_call_;
980 } test(use_bitrate_allocation_strategy, test_bitrate_from, test_bitrate_to,
981 test_bitrate_step, min_bwe, start_bwe, max_bwe);
982
983 RunBaseTest(&test);
984}
985
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800986// TODO(bugs.webrtc.org/8878)
987#if defined(WEBRTC_MAC)
Yves Gerey665174f2018-06-19 15:03:05 +0200988#define MAYBE_MinVideoAndAudioBitrate DISABLED_MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800989#else
Yves Gerey665174f2018-06-19 15:03:05 +0200990#define MAYBE_MinVideoAndAudioBitrate MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800991#endif
992TEST_F(CallPerfTest, MAYBE_MinVideoAndAudioBitrate) {
Alex Narestd0e196b2017-11-22 17:22:35 +0100993 TestMinAudioVideoBitrate(false, 110, 40, -10, 10000, 70000, 200000);
994}
995TEST_F(CallPerfTest, MinVideoAndAudioBitrateWStrategy) {
996 TestMinAudioVideoBitrate(true, 110, 40, -10, 10000, 70000, 200000);
997}
998
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000999} // namespace webrtc