blob: 43af881aa1c65b2e6f1359d4a47d3a779c178f4f [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"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080019#include "api/video/builtin_video_bitrate_allocator_factory.h"
Erik Språngef75ebe2018-05-15 15:18:36 +020020#include "api/video/video_bitrate_allocation.h"
Elad Alon370f93a2019-06-11 14:57:57 +020021#include "api/video_codecs/video_encoder.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020022#include "api/video_codecs/video_encoder_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "call/call.h"
Artem Titov4e199e92018-08-20 13:30:39 +020024#include "call/fake_network_pipe.h"
25#include "call/simulated_network.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "logging/rtc_event_log/rtc_event_log.h"
27#include "modules/audio_coding/include/audio_coding_module.h"
Artem Titov3faa8322018-03-07 14:44:00 +010028#include "modules/audio_device/include/test_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_mixer/audio_mixer_impl.h"
30#include "modules/rtp_rtcp/include/rtp_header_parser.h"
31#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/thread_annotations.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020033#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "test/call_test.h"
35#include "test/direct_transport.h"
36#include "test/drifting_clock.h"
37#include "test/encoder_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "test/fake_encoder.h"
39#include "test/field_trial.h"
40#include "test/frame_generator.h"
41#include "test/frame_generator_capturer.h"
42#include "test/gtest.h"
Niels Möllerae4237e2018-10-05 11:28:38 +020043#include "test/null_transport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/rtp_rtcp_observer.h"
45#include "test/single_threaded_task_queue.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "test/testsupport/file_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "test/testsupport/perf_test.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020048#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "video/transport_adapter.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000050
danilchap9c6a0c72016-02-10 10:54:47 -080051using webrtc::test::DriftingClock;
danilchap9c6a0c72016-02-10 10:54:47 -080052
pbos@webrtc.org1d096902013-12-13 12:48:05 +000053namespace webrtc {
Elad Alond8d32482019-02-18 23:45:57 +010054namespace {
55enum : int { // The first valid value is 1.
56 kTransportSequenceNumberExtensionId = 1,
57};
58} // namespace
pbos@webrtc.org1d096902013-12-13 12:48:05 +000059
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000060class CallPerfTest : public test::CallTest {
Elad Alond8d32482019-02-18 23:45:57 +010061 public:
62 CallPerfTest() {
63 RegisterRtpExtension(RtpExtension(RtpExtension::kTransportSequenceNumberUri,
64 kTransportSequenceNumberExtensionId));
65 }
66
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000067 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020068 enum class FecMode { kOn, kOff };
69 enum class CreateOrder { kAudioFirst, kVideoFirst };
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +010070 void TestAudioVideoSync(FecMode fec,
71 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -080072 float video_ntp_speed,
73 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +010074 float audio_rtp_speed,
75 const std::string& test_label);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000076
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000077 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
78
Artem Titov75e36472018-10-08 12:28:56 +020079 void TestCaptureNtpTime(const BuiltInNetworkBehaviorConfig& net_config,
wu@webrtc.orgcd701192014-04-24 22:10:24 +000080 int threshold_ms,
81 int start_time_ms,
82 int run_time_ms);
Jonas Olsson0182a032019-07-09 12:31:20 +020083 void TestMinAudioVideoBitrate(int test_bitrate_from,
Alex Narestd0e196b2017-11-22 17:22:35 +010084 int test_bitrate_to,
85 int test_bitrate_step,
86 int min_bwe,
87 int start_bwe,
88 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000089};
90
asaperssonf8cdd182016-03-15 01:00:47 -070091class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070092 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000093 static const int kInSyncThresholdMs = 50;
94 static const int kStartupTimeMs = 2000;
95 static const int kMinRunTimeMs = 30000;
96
97 public:
Edward Lemur947f3fe2017-12-28 15:50:33 +010098 explicit VideoRtcpAndSyncObserver(Clock* clock, const std::string& test_label)
asaperssonf8cdd182016-03-15 01:00:47 -070099 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
100 clock_(clock),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100101 test_label_(test_label),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000102 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -0700103 first_time_in_sync_(-1),
104 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000105
nisseeb83a1a2016-03-21 01:27:56 -0700106 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -0700107 VideoReceiveStream::Stats stats;
108 {
109 rtc::CritScope lock(&crit_);
110 if (receive_stream_)
111 stats = receive_stream_->GetStats();
112 }
113 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
114 return;
115
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000116 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000117 int64_t time_since_creation = now_ms - creation_time_ms_;
118 // During the first couple of seconds audio and video can falsely be
119 // estimated as being synchronized. We don't want to trigger on those.
120 if (time_since_creation < kStartupTimeMs)
121 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700122 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000123 if (first_time_in_sync_ == -1) {
124 first_time_in_sync_ = now_ms;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100125 webrtc::test::PrintResult("sync_convergence_time", test_label_,
126 "synchronization", time_since_creation, "ms",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000127 false);
128 }
129 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100130 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000131 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200132 if (first_time_in_sync_ != -1)
133 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000134 }
135
asaperssonf8cdd182016-03-15 01:00:47 -0700136 void set_receive_stream(VideoReceiveStream* receive_stream) {
137 rtc::CritScope lock(&crit_);
138 receive_stream_ = receive_stream;
139 }
140
danilchap46b89b92016-06-03 09:27:37 -0700141 void PrintResults() {
Edward Lemur947f3fe2017-12-28 15:50:33 +0100142 test::PrintResultList("stream_offset", test_label_, "synchronization",
Edward Lemur2f061682017-11-24 13:40:01 +0100143 sync_offset_ms_list_, "ms", false);
danilchap46b89b92016-06-03 09:27:37 -0700144 }
145
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000146 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000147 Clock* const clock_;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100148 std::string test_label_;
stefanf116bd02015-10-27 08:29:42 -0700149 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000150 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700151 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700152 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100153 std::vector<double> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000154};
155
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100156void CallPerfTest::TestAudioVideoSync(FecMode fec,
157 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800158 float video_ntp_speed,
159 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +0100160 float audio_rtp_speed,
161 const std::string& test_label) {
pbos8fc7fa72015-07-15 08:02:58 -0700162 const char* kSyncGroup = "av_sync";
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100163 const uint32_t kAudioSendSsrc = 1234;
164 const uint32_t kAudioRecvSsrc = 5678;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000165
Artem Titov75e36472018-10-08 12:28:56 +0200166 BuiltInNetworkBehaviorConfig audio_net_config;
mflodman3d7db262016-04-29 00:57:13 -0700167 audio_net_config.queue_delay_ms = 500;
168 audio_net_config.loss_percent = 5;
minyue20c84cc2017-04-10 16:57:57 -0700169
Edward Lemur947f3fe2017-12-28 15:50:33 +0100170 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), test_label);
eladalon413ee9a2017-08-22 04:02:52 -0700171
minyue20c84cc2017-04-10 16:57:57 -0700172 std::map<uint8_t, MediaType> audio_pt_map;
173 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700174
eladalon413ee9a2017-08-22 04:02:52 -0700175 std::unique_ptr<test::PacketTransport> audio_send_transport;
176 std::unique_ptr<test::PacketTransport> video_send_transport;
177 std::unique_ptr<test::PacketTransport> receive_transport;
Niels Möllerae4237e2018-10-05 11:28:38 +0200178 test::NullTransport rtcp_send_transport;
mflodman3d7db262016-04-29 00:57:13 -0700179
eladalon413ee9a2017-08-22 04:02:52 -0700180 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100181 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700182 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700183
eladalon413ee9a2017-08-22 04:02:52 -0700184 task_queue_.SendTask([&]() {
185 metrics::Reset();
Artem Titov3faa8322018-03-07 14:44:00 +0100186 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device =
Danil Chapovalov08fa9532019-06-12 11:49:17 +0000187 TestAudioDeviceModule::Create(
188 task_queue_factory_.get(),
Artem Titov3faa8322018-03-07 14:44:00 +0100189 TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000),
190 TestAudioDeviceModule::CreateDiscardRenderer(48000),
191 audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100192 EXPECT_EQ(0, fake_audio_device->Init());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000193
eladalon413ee9a2017-08-22 04:02:52 -0700194 AudioState::Config send_audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -0700195 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +0100196 send_audio_state_config.audio_processing =
197 AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100198 send_audio_state_config.audio_device_module = fake_audio_device;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200199 Call::Config sender_config(send_event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000200
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100201 auto audio_state = AudioState::Create(send_audio_state_config);
202 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
203 sender_config.audio_state = audio_state;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200204 Call::Config receiver_config(recv_event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100205 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700206 CreateCalls(sender_config, receiver_config);
207
208 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
209 std::inserter(audio_pt_map, audio_pt_map.end()),
210 [](const std::pair<const uint8_t, MediaType>& pair) {
211 return pair.second == MediaType::AUDIO;
212 });
213 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
214 std::inserter(video_pt_map, video_pt_map.end()),
215 [](const std::pair<const uint8_t, MediaType>& pair) {
216 return pair.second == MediaType::VIDEO;
217 });
218
Karl Wiberg918f50c2018-07-05 11:40:33 +0200219 audio_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700220 &task_queue_, sender_call_.get(), &observer,
Artem Titov4e199e92018-08-20 13:30:39 +0200221 test::PacketTransport::kSender, audio_pt_map,
222 absl::make_unique<FakeNetworkPipe>(
223 Clock::GetRealTimeClock(),
224 absl::make_unique<SimulatedNetwork>(audio_net_config)));
eladalon413ee9a2017-08-22 04:02:52 -0700225 audio_send_transport->SetReceiver(receiver_call_->Receiver());
226
Karl Wiberg918f50c2018-07-05 11:40:33 +0200227 video_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700228 &task_queue_, sender_call_.get(), &observer,
229 test::PacketTransport::kSender, video_pt_map,
Artem Titov4e199e92018-08-20 13:30:39 +0200230 absl::make_unique<FakeNetworkPipe>(
231 Clock::GetRealTimeClock(), absl::make_unique<SimulatedNetwork>(
Artem Titov75e36472018-10-08 12:28:56 +0200232 BuiltInNetworkBehaviorConfig())));
eladalon413ee9a2017-08-22 04:02:52 -0700233 video_send_transport->SetReceiver(receiver_call_->Receiver());
234
Karl Wiberg918f50c2018-07-05 11:40:33 +0200235 receive_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700236 &task_queue_, receiver_call_.get(), &observer,
237 test::PacketTransport::kReceiver, payload_type_map_,
Artem Titov4e199e92018-08-20 13:30:39 +0200238 absl::make_unique<FakeNetworkPipe>(
239 Clock::GetRealTimeClock(), absl::make_unique<SimulatedNetwork>(
Artem Titov75e36472018-10-08 12:28:56 +0200240 BuiltInNetworkBehaviorConfig())));
eladalon413ee9a2017-08-22 04:02:52 -0700241 receive_transport->SetReceiver(sender_call_->Receiver());
242
243 CreateSendConfig(1, 0, 0, video_send_transport.get());
244 CreateMatchingReceiveConfigs(receive_transport.get());
245
Niels Möller7d76a312018-10-26 12:57:07 +0200246 AudioSendStream::Config audio_send_config(audio_send_transport.get(),
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700247 MediaTransportConfig());
eladalon413ee9a2017-08-22 04:02:52 -0700248 audio_send_config.rtp.ssrc = kAudioSendSsrc;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100249 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
250 kAudioSendPayloadType, {"ISAC", 16000, 1});
eladalon413ee9a2017-08-22 04:02:52 -0700251 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
252 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
253
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200254 GetVideoSendConfig()->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
eladalon413ee9a2017-08-22 04:02:52 -0700255 if (fec == FecMode::kOn) {
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200256 GetVideoSendConfig()->rtp.ulpfec.red_payload_type = kRedPayloadType;
257 GetVideoSendConfig()->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700258 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
259 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700260 }
261 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
262 video_receive_configs_[0].renderer = &observer;
263 video_receive_configs_[0].sync_group = kSyncGroup;
264
265 AudioReceiveStream::Config audio_recv_config;
266 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
267 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
Niels Möllerae4237e2018-10-05 11:28:38 +0200268 audio_recv_config.rtcp_send_transport = &rtcp_send_transport;
eladalon413ee9a2017-08-22 04:02:52 -0700269 audio_recv_config.sync_group = kSyncGroup;
Niels Möller2784a032018-03-28 14:16:04 +0200270 audio_recv_config.decoder_factory = audio_decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700271 audio_recv_config.decoder_map = {
272 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
273
274 if (create_first == CreateOrder::kAudioFirst) {
275 audio_receive_stream =
276 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
277 CreateVideoStreams();
278 } else {
279 CreateVideoStreams();
280 audio_receive_stream =
281 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
282 }
283 EXPECT_EQ(1u, video_receive_streams_.size());
284 observer.set_receive_stream(video_receive_streams_[0]);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200285 drifting_clock = absl::make_unique<DriftingClock>(clock_, video_ntp_speed);
eladalon413ee9a2017-08-22 04:02:52 -0700286 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
287 kDefaultFramerate, kDefaultWidth,
288 kDefaultHeight);
289
290 Start();
291
292 audio_send_stream->Start();
293 audio_receive_stream->Start();
294 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000295
Peter Boström5811a392015-12-10 13:02:50 +0100296 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000297 << "Timed out while waiting for audio and video to be synchronized.";
298
eladalon413ee9a2017-08-22 04:02:52 -0700299 task_queue_.SendTask([&]() {
300 audio_send_stream->Stop();
301 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000302
eladalon413ee9a2017-08-22 04:02:52 -0700303 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000304
eladalon413ee9a2017-08-22 04:02:52 -0700305 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100306
eladalon413ee9a2017-08-22 04:02:52 -0700307 video_send_transport.reset();
308 audio_send_transport.reset();
309 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100310
eladalon413ee9a2017-08-22 04:02:52 -0700311 sender_call_->DestroyAudioSendStream(audio_send_stream);
312 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000313
eladalon413ee9a2017-08-22 04:02:52 -0700314 DestroyCalls();
eladalon413ee9a2017-08-22 04:02:52 -0700315 });
asaperssonf8cdd182016-03-15 01:00:47 -0700316
danilchap46b89b92016-06-03 09:27:37 -0700317 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800318
319 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800320 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
Artem Titarenkoded1e4f2019-03-15 11:36:39 +0100321// TODO(bugs.webrtc.org/10417): Reenable this for iOS
322#if !defined(WEBRTC_IOS)
ilnik5328b9e2017-02-21 05:20:28 -0800323 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
Artem Titarenkoded1e4f2019-03-15 11:36:39 +0100324#endif
ilnik5328b9e2017-02-21 05:20:28 -0800325 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000326}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000327
Niels Möller9a750612018-08-09 11:04:32 +0200328TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithoutClockDrift) {
329 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
330 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
331 DriftingClock::kNoDrift, "_video_no_drift");
332}
333
danilchapac287ee2016-02-29 12:17:04 -0800334TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100335 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
336 DriftingClock::PercentsFaster(10.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100337 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
338 "_video_ntp_drift");
danilchap9c6a0c72016-02-10 10:54:47 -0800339}
340
danilchap9c6a0c72016-02-10 10:54:47 -0800341TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100342 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
343 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800344 DriftingClock::PercentsSlower(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100345 DriftingClock::PercentsFaster(30.0f), "_audio_faster");
danilchap9c6a0c72016-02-10 10:54:47 -0800346}
347
348TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100349 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
350 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800351 DriftingClock::PercentsFaster(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100352 DriftingClock::PercentsSlower(30.0f), "_video_faster");
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000353}
354
Artem Titov46c4e602018-08-17 14:26:54 +0200355void CallPerfTest::TestCaptureNtpTime(
Artem Titov75e36472018-10-08 12:28:56 +0200356 const BuiltInNetworkBehaviorConfig& net_config,
Artem Titov46c4e602018-08-17 14:26:54 +0200357 int threshold_ms,
358 int start_time_ms,
359 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000360 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700361 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000362 public:
Artem Titov75e36472018-10-08 12:28:56 +0200363 CaptureNtpTimeObserver(const BuiltInNetworkBehaviorConfig& net_config,
stefane74eef12016-01-08 06:47:13 -0800364 int threshold_ms,
365 int start_time_ms,
366 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700367 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800368 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000369 clock_(Clock::GetRealTimeClock()),
370 threshold_ms_(threshold_ms),
371 start_time_ms_(start_time_ms),
372 run_time_ms_(run_time_ms),
373 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000374 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000375 rtp_start_timestamp_set_(false),
376 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000377
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000378 private:
eladalon413ee9a2017-08-22 04:02:52 -0700379 test::PacketTransport* CreateSendTransport(
380 test::SingleThreadedTaskQueueForTesting* task_queue,
381 Call* sender_call) override {
Artem Titov4e199e92018-08-20 13:30:39 +0200382 return new test::PacketTransport(
383 task_queue, sender_call, this, test::PacketTransport::kSender,
384 payload_type_map_,
385 absl::make_unique<FakeNetworkPipe>(
386 Clock::GetRealTimeClock(),
387 absl::make_unique<SimulatedNetwork>(net_config_)));
stefane74eef12016-01-08 06:47:13 -0800388 }
389
eladalon413ee9a2017-08-22 04:02:52 -0700390 test::PacketTransport* CreateReceiveTransport(
391 test::SingleThreadedTaskQueueForTesting* task_queue) override {
Artem Titov4e199e92018-08-20 13:30:39 +0200392 return new test::PacketTransport(
393 task_queue, nullptr, this, test::PacketTransport::kReceiver,
394 payload_type_map_,
395 absl::make_unique<FakeNetworkPipe>(
396 Clock::GetRealTimeClock(),
397 absl::make_unique<SimulatedNetwork>(net_config_)));
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100398 }
399
nisseeb83a1a2016-03-21 01:27:56 -0700400 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700401 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000402 if (video_frame.ntp_time_ms() <= 0) {
403 // Haven't got enough RTCP SR in order to calculate the capture ntp
404 // time.
405 return;
406 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000407
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000408 int64_t now_ms = clock_->TimeInMilliseconds();
409 int64_t time_since_creation = now_ms - creation_time_ms_;
410 if (time_since_creation < start_time_ms_) {
411 // Wait for |start_time_ms_| before start measuring.
412 return;
413 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000414
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000415 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100416 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000417 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000418
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000419 FrameCaptureTimeList::iterator iter =
420 capture_time_list_.find(video_frame.timestamp());
421 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000422
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000423 // The real capture time has been wrapped to uint32_t before converted
424 // to rtp timestamp in the sender side. So here we convert the estimated
425 // capture time to a uint32_t 90k timestamp also for comparing.
426 uint32_t estimated_capture_timestamp =
427 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
428 uint32_t real_capture_timestamp = iter->second;
429 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
430 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700431 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000432
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000433 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
434 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000435
nisseef8b61e2016-04-29 06:09:15 -0700436 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700437 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000438 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000439 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000440
441 if (!rtp_start_timestamp_set_) {
442 // Calculate the rtp timestamp offset in order to calculate the real
443 // capture time.
444 uint32_t first_capture_timestamp =
445 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
446 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
447 rtp_start_timestamp_set_ = true;
448 }
449
450 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
451 capture_time_list_.insert(
452 capture_time_list_.end(),
453 std::make_pair(header.timestamp, capture_timestamp));
454 return SEND_PACKET;
455 }
456
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000457 void OnFrameGeneratorCapturerCreated(
458 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000459 capturer_ = frame_generator_capturer;
460 }
461
stefanff483612015-12-21 03:14:00 -0800462 void ModifyVideoConfigs(
463 VideoSendStream::Config* send_config,
464 std::vector<VideoReceiveStream::Config>* receive_configs,
465 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000466 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000467 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000468 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000469 }
470
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000471 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100472 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
473 "estimated capture NTP time to be "
474 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700475 test::PrintResultList("capture_ntp_time", "", "real - estimated",
Edward Lemur2f061682017-11-24 13:40:01 +0100476 time_offset_ms_list_, "ms", true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000477 }
478
stefanf116bd02015-10-27 08:29:42 -0700479 rtc::CriticalSection crit_;
Artem Titov75e36472018-10-08 12:28:56 +0200480 const BuiltInNetworkBehaviorConfig net_config_;
stefanf116bd02015-10-27 08:29:42 -0700481 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000482 int threshold_ms_;
483 int start_time_ms_;
484 int run_time_ms_;
485 int64_t creation_time_ms_;
486 test::FrameGeneratorCapturer* capturer_;
487 bool rtp_start_timestamp_set_;
488 uint32_t rtp_start_timestamp_;
489 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700490 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100491 std::vector<double> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800492 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000493
stefane74eef12016-01-08 06:47:13 -0800494 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000495}
496
Alex Loikoaf228ee2018-11-22 11:53:18 +0100497// Flaky tests, disabled on Mac and Windows due to webrtc:8291.
498#if !(defined(WEBRTC_MAC) || defined(WEBRTC_WIN))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000499TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
Artem Titov75e36472018-10-08 12:28:56 +0200500 BuiltInNetworkBehaviorConfig net_config;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000501 net_config.queue_delay_ms = 100;
502 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
503 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000504 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000505 const int kStartTimeMs = 10000;
506 const int kRunTimeMs = 20000;
507 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
508}
509
wu@webrtc.org0224c202014-05-05 17:42:43 +0000510TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
Artem Titov75e36472018-10-08 12:28:56 +0200511 BuiltInNetworkBehaviorConfig net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000512 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000513 net_config.delay_standard_deviation_ms = 10;
514 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
515 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000516 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000517 const int kStartTimeMs = 10000;
518 const int kRunTimeMs = 20000;
519 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
520}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200521#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800522
perkj803d97f2016-11-01 11:45:46 -0700523TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700524 // Minimal normal usage at the start, then 30s overuse to allow filter to
525 // settle, and then 80s underuse to allow plenty of time for rampup again.
526 test::ScopedFieldTrials fake_overuse_settings(
527 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
528
perkj803d97f2016-11-01 11:45:46 -0700529 class LoadObserver : public test::SendTest,
530 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000531 public:
Åsa Persson8c1bf952018-09-13 10:42:19 +0200532 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kInit) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000533
perkj803d97f2016-11-01 11:45:46 -0700534 void OnFrameGeneratorCapturerCreated(
535 test::FrameGeneratorCapturer* frame_generator_capturer) override {
536 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800537 // Set a high initial resolution to be sure that we can scale down.
538 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700539 }
540
541 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
542 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700543 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700544 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
545 const rtc::VideoSinkWants& wants) override {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200546 // At kStart expect CPU overuse. Then expect CPU underuse when the encoder
perkj803d97f2016-11-01 11:45:46 -0700547 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700548 switch (test_phase_) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200549 case TestPhase::kInit:
550 // Max framerate should be set initially.
551 if (wants.max_framerate_fps != std::numeric_limits<int>::max() &&
552 wants.max_pixel_count == std::numeric_limits<int>::max()) {
553 test_phase_ = TestPhase::kStart;
554 } else {
555 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
556 << wants.max_pixel_count << ", target res = "
557 << wants.target_pixel_count.value_or(-1)
558 << ", max fps = " << wants.max_framerate_fps;
559 }
560 break;
sprangc5d62e22017-04-02 23:53:04 -0700561 case TestPhase::kStart:
562 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700563 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
564 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700565 test_phase_ = TestPhase::kAdaptedDown;
566 } else {
567 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
568 << wants.max_pixel_count << ", target res = "
569 << wants.target_pixel_count.value_or(-1)
570 << ", max fps = " << wants.max_framerate_fps;
571 }
572 break;
573 case TestPhase::kAdaptedDown:
574 // On adapting up, the adaptation counter will again be at zero, and
575 // so all constraints will be reset.
576 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
577 !wants.target_pixel_count) {
578 test_phase_ = TestPhase::kAdaptedUp;
579 observation_complete_.Set();
580 } else {
581 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
582 << wants.max_pixel_count << ", target res = "
583 << wants.target_pixel_count.value_or(-1)
584 << ", max fps = " << wants.max_framerate_fps;
585 }
586 break;
587 case TestPhase::kAdaptedUp:
588 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
589 << wants.max_pixel_count << ", target res = "
590 << wants.target_pixel_count.value_or(-1)
591 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700592 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000593 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000594
stefanff483612015-12-21 03:14:00 -0800595 void ModifyVideoConfigs(
596 VideoSendStream::Config* send_config,
597 std::vector<VideoReceiveStream::Config>* receive_configs,
Yves Gerey665174f2018-06-19 15:03:05 +0200598 VideoEncoderConfig* encoder_config) override {}
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000599
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000600 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100601 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000602 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000603
Åsa Persson8c1bf952018-09-13 10:42:19 +0200604 enum class TestPhase {
605 kInit,
606 kStart,
607 kAdaptedDown,
608 kAdaptedUp
609 } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700610 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000611
stefane74eef12016-01-08 06:47:13 -0800612 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000613}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000614
615void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
616 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000617 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000618 static const int kMinAcceptableTransmitBitrate = 130;
619 static const int kMaxAcceptableTransmitBitrate = 170;
620 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700621 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700622 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000623 public:
624 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000625 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000626 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200627 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000628 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200629 min_acceptable_bitrate_(using_min_transmit_bitrate
630 ? kMinAcceptableTransmitBitrate
631 : (kMaxEncodeBitrateKbps -
632 kAcceptableBitrateErrorMargin / 2)),
633 max_acceptable_bitrate_(using_min_transmit_bitrate
634 ? kMaxAcceptableTransmitBitrate
635 : (kMaxEncodeBitrateKbps +
636 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000637 num_bitrate_observations_in_range_(0) {}
638
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000639 private:
stefanf116bd02015-10-27 08:29:42 -0700640 // TODO(holmer): Run this with a timer instead of once per packet.
641 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000642 VideoSendStream::Stats stats = send_stream_->GetStats();
Benjamin Wright41f9f2c2019-03-13 18:03:29 -0700643 if (!stats.substreams.empty()) {
kwibergaf476c72016-11-28 15:21:39 -0800644 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000645 int bitrate_kbps =
646 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200647 if (bitrate_kbps > min_acceptable_bitrate_ &&
648 bitrate_kbps < max_acceptable_bitrate_) {
649 converged_ = true;
650 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000651 if (num_bitrate_observations_in_range_ ==
652 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100653 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000654 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200655 if (converged_)
656 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000657 }
stefanf116bd02015-10-27 08:29:42 -0700658 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000659 }
660
stefanff483612015-12-21 03:14:00 -0800661 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000662 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000663 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000664 send_stream_ = send_stream;
665 }
666
stefanff483612015-12-21 03:14:00 -0800667 void ModifyVideoConfigs(
668 VideoSendStream::Config* send_config,
669 std::vector<VideoReceiveStream::Config>* receive_configs,
670 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000671 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000672 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000673 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700674 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000675 }
676 }
677
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000678 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100679 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700680 test::PrintResultList(
681 "bitrate_stats_",
682 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
683 : "without_min_transmit_bitrate"),
Edward Lemur2f061682017-11-24 13:40:01 +0100684 "bitrate_kbps", bitrate_kbps_list_, "kbps", false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000685 }
686
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000687 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200688 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000689 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200690 const int min_acceptable_bitrate_;
691 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000692 int num_bitrate_observations_in_range_;
Edward Lemur2f061682017-11-24 13:40:01 +0100693 std::vector<double> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000694 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000695
Niels Möller4db138e2018-04-19 09:04:13 +0200696 fake_encoder_max_bitrate_ = kMaxEncodeBitrateKbps;
stefane74eef12016-01-08 06:47:13 -0800697 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000698}
699
Yves Gerey665174f2018-06-19 15:03:05 +0200700TEST_F(CallPerfTest, PadsToMinTransmitBitrate) {
701 TestMinTransmitBitrate(true);
702}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000703
704TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
705 TestMinTransmitBitrate(false);
706}
707
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800708// TODO(bugs.webrtc.org/8878)
709#if defined(WEBRTC_MAC)
710#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
711 DISABLED_KeepsHighBitrateWhenReconfiguringSender
712#else
713#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
714 KeepsHighBitrateWhenReconfiguringSender
715#endif
716TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000717 static const uint32_t kInitialBitrateKbps = 400;
718 static const uint32_t kReconfigureThresholdKbps = 600;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000719
perkjfa10b552016-10-02 23:45:26 -0700720 class VideoStreamFactory
721 : public VideoEncoderConfig::VideoStreamFactoryInterface {
722 public:
723 VideoStreamFactory() {}
724
725 private:
726 std::vector<VideoStream> CreateEncoderStreams(
727 int width,
728 int height,
729 const VideoEncoderConfig& encoder_config) override {
730 std::vector<VideoStream> streams =
731 test::CreateVideoStreams(width, height, encoder_config);
732 streams[0].min_bitrate_bps = 50000;
733 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
734 return streams;
735 }
736 };
737
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000738 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
739 public:
740 BitrateObserver()
741 : EndToEndTest(kDefaultTimeoutMs),
742 FakeEncoder(Clock::GetRealTimeClock()),
sprang867fb522015-08-03 04:38:41 -0700743 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100744 last_set_bitrate_kbps_(0),
745 send_stream_(nullptr),
Niels Möller4db138e2018-04-19 09:04:13 +0200746 frame_generator_(nullptr),
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800747 encoder_factory_(this),
748 bitrate_allocator_factory_(
749 CreateBuiltinVideoBitrateAllocatorFactory()) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000750
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000751 int32_t InitEncode(const VideoCodec* config,
Elad Alon370f93a2019-06-11 14:57:57 +0200752 const VideoEncoder::Settings& settings) override {
perkjfa10b552016-10-02 23:45:26 -0700753 ++encoder_inits_;
754 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700755 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100756 // |expected_bitrate| is affected by bandwidth estimation before the
757 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100758 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
759 ? last_set_bitrate_kbps_
760 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100761 EXPECT_EQ(expected_bitrate, config->startBitrate)
762 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700763 EXPECT_EQ(kDefaultWidth, config->width);
764 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100765 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700766 EXPECT_EQ(2 * kDefaultWidth, config->width);
767 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100768 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
philipel0676f222018-04-17 16:12:21 +0200769 EXPECT_GT(config->startBitrate, kReconfigureThresholdKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000770 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100771 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000772 }
Elad Alon370f93a2019-06-11 14:57:57 +0200773 return FakeEncoder::InitEncode(config, settings);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000774 }
775
Erik Språng16cb8f52019-04-12 13:59:09 +0200776 void SetRates(const RateControlParameters& parameters) override {
777 last_set_bitrate_kbps_ = parameters.bitrate.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100778 if (encoder_inits_ == 1 &&
Erik Språng16cb8f52019-04-12 13:59:09 +0200779 parameters.bitrate.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100780 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000781 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200782 FakeEncoder::SetRates(parameters);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000783 }
784
Niels Möllerde8e6e62018-11-13 15:10:33 +0100785 void ModifySenderBitrateConfig(
786 BitrateConstraints* bitrate_config) override {
787 bitrate_config->start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000788 }
789
stefanff483612015-12-21 03:14:00 -0800790 void ModifyVideoConfigs(
791 VideoSendStream::Config* send_config,
792 std::vector<VideoReceiveStream::Config>* receive_configs,
793 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200794 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800795 send_config->encoder_settings.bitrate_allocator_factory =
796 bitrate_allocator_factory_.get();
Per21d45d22016-10-30 21:37:57 +0100797 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700798 encoder_config->video_stream_factory =
799 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000800
perkj26091b12016-09-01 01:17:40 -0700801 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000802 }
803
stefanff483612015-12-21 03:14:00 -0800804 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000805 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000806 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000807 send_stream_ = send_stream;
808 }
809
perkjfa10b552016-10-02 23:45:26 -0700810 void OnFrameGeneratorCapturerCreated(
811 test::FrameGeneratorCapturer* frame_generator_capturer) override {
812 frame_generator_ = frame_generator_capturer;
813 }
814
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000815 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100816 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000817 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700818 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700819 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100820 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000821 << "Timed out while waiting for a couple of high bitrate estimates "
822 "after reconfiguring the send stream.";
823 }
824
825 private:
Peter Boström5811a392015-12-10 13:02:50 +0100826 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000827 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100828 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000829 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700830 test::FrameGeneratorCapturer* frame_generator_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200831 test::VideoEncoderProxyFactory encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800832 std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000833 VideoEncoderConfig encoder_config_;
834 } test;
835
stefane74eef12016-01-08 06:47:13 -0800836 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000837}
838
Alex Narestd0e196b2017-11-22 17:22:35 +0100839// Discovers the minimal supported audio+video bitrate. The test bitrate is
840// considered supported if Rtt does not go above 400ms with the network
841// contrained to the test bitrate.
842//
Alex Narestd0e196b2017-11-22 17:22:35 +0100843// |test_bitrate_from test_bitrate_to| bitrate constraint range
844// |test_bitrate_step| bitrate constraint update step during the test
845// |min_bwe max_bwe| BWE range
846// |start_bwe| initial BWE
Jonas Olsson0182a032019-07-09 12:31:20 +0200847void CallPerfTest::TestMinAudioVideoBitrate(int test_bitrate_from,
848 int test_bitrate_to,
849 int test_bitrate_step,
850 int min_bwe,
851 int start_bwe,
852 int max_bwe) {
Alex Narestd0e196b2017-11-22 17:22:35 +0100853 static const std::string kAudioTrackId = "audio_track_0";
Alex Narestd0e196b2017-11-22 17:22:35 +0100854 static constexpr int kOpusBitrateFbBps = 32000;
855 static constexpr int kBitrateStabilizationMs = 10000;
856 static constexpr int kBitrateMeasurements = 10;
857 static constexpr int kBitrateMeasurementMs = 1000;
Ilya Nikolaevskiy0500b522019-01-22 11:12:51 +0100858 static constexpr int kShortDelayMs = 10;
Alex Narestd0e196b2017-11-22 17:22:35 +0100859 static constexpr int kMinGoodRttMs = 400;
860
861 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
862 public:
Jonas Olsson0182a032019-07-09 12:31:20 +0200863 MinVideoAndAudioBitrateTester(int test_bitrate_from,
Alex Narestd0e196b2017-11-22 17:22:35 +0100864 int test_bitrate_to,
865 int test_bitrate_step,
866 int min_bwe,
867 int start_bwe,
868 int max_bwe)
869 : EndToEndTest(),
Alex Narestd0e196b2017-11-22 17:22:35 +0100870 test_bitrate_from_(test_bitrate_from),
871 test_bitrate_to_(test_bitrate_to),
872 test_bitrate_step_(test_bitrate_step),
873 min_bwe_(min_bwe),
874 start_bwe_(start_bwe),
875 max_bwe_(max_bwe) {}
876
877 protected:
Artem Titov75e36472018-10-08 12:28:56 +0200878 BuiltInNetworkBehaviorConfig GetFakeNetworkPipeConfig() {
879 BuiltInNetworkBehaviorConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100880 pipe_config.link_capacity_kbps = test_bitrate_from_;
881 return pipe_config;
882 }
883
884 test::PacketTransport* CreateSendTransport(
885 test::SingleThreadedTaskQueueForTesting* task_queue,
886 Call* sender_call) override {
Artem Titov631cafa2018-08-21 21:01:00 +0200887 auto network =
888 absl::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig());
889 send_simulated_network_ = network.get();
890 return new test::PacketTransport(
891 task_queue, sender_call, this, test::PacketTransport::kSender,
892 test::CallTest::payload_type_map_,
893 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
894 std::move(network)));
Alex Narestd0e196b2017-11-22 17:22:35 +0100895 }
896
897 test::PacketTransport* CreateReceiveTransport(
898 test::SingleThreadedTaskQueueForTesting* task_queue) override {
Artem Titov631cafa2018-08-21 21:01:00 +0200899 auto network =
900 absl::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig());
901 receive_simulated_network_ = network.get();
902 return new test::PacketTransport(
903 task_queue, nullptr, this, test::PacketTransport::kReceiver,
904 test::CallTest::payload_type_map_,
905 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
906 std::move(network)));
Alex Narestd0e196b2017-11-22 17:22:35 +0100907 }
908
909 void PerformTest() override {
Ilya Nikolaevskiy0500b522019-01-22 11:12:51 +0100910 // Quick test mode, just to exercise all the code paths without actually
911 // caring about performance measurements.
912 const bool quick_perf_test =
913 field_trial::IsEnabled("WebRTC-QuickPerfTest");
Alex Narestd0e196b2017-11-22 17:22:35 +0100914 int last_passed_test_bitrate = -1;
915 for (int test_bitrate = test_bitrate_from_;
916 test_bitrate_from_ < test_bitrate_to_
917 ? test_bitrate <= test_bitrate_to_
918 : test_bitrate >= test_bitrate_to_;
919 test_bitrate += test_bitrate_step_) {
Artem Titov75e36472018-10-08 12:28:56 +0200920 BuiltInNetworkBehaviorConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100921 pipe_config.link_capacity_kbps = test_bitrate;
Artem Titov631cafa2018-08-21 21:01:00 +0200922 send_simulated_network_->SetConfig(pipe_config);
923 receive_simulated_network_->SetConfig(pipe_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100924
925 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
Ilya Nikolaevskiy0500b522019-01-22 11:12:51 +0100926 quick_perf_test ? kShortDelayMs : kBitrateStabilizationMs);
Alex Narestd0e196b2017-11-22 17:22:35 +0100927
928 int64_t avg_rtt = 0;
929 for (int i = 0; i < kBitrateMeasurements; i++) {
930 Call::Stats call_stats = sender_call_->GetStats();
931 avg_rtt += call_stats.rtt_ms;
932 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
Ilya Nikolaevskiy0500b522019-01-22 11:12:51 +0100933 quick_perf_test ? kShortDelayMs : kBitrateMeasurementMs);
Alex Narestd0e196b2017-11-22 17:22:35 +0100934 }
935 avg_rtt = avg_rtt / kBitrateMeasurements;
936 if (avg_rtt > kMinGoodRttMs) {
937 break;
938 } else {
939 last_passed_test_bitrate = test_bitrate;
940 }
941 }
942 EXPECT_GT(last_passed_test_bitrate, -1)
943 << "Minimum supported bitrate out of the test scope";
Jonas Olsson0182a032019-07-09 12:31:20 +0200944 webrtc::test::PrintResult("min_test_bitrate_", "", "min_bitrate",
945 last_passed_test_bitrate, "kbps", false);
Alex Narestd0e196b2017-11-22 17:22:35 +0100946 }
947
948 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
949 sender_call_ = sender_call;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100950 BitrateConstraints bitrate_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100951 bitrate_config.min_bitrate_bps = min_bwe_;
952 bitrate_config.start_bitrate_bps = start_bwe_;
953 bitrate_config.max_bitrate_bps = max_bwe_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100954 sender_call->GetTransportControllerSend()->SetSdpBitrateParameters(
955 bitrate_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100956 }
957
958 size_t GetNumVideoStreams() const override { return 1; }
959
960 size_t GetNumAudioStreams() const override { return 1; }
961
962 void ModifyAudioConfigs(
963 AudioSendStream::Config* send_config,
964 std::vector<AudioReceiveStream::Config>* receive_configs) override {
Jonas Olsson0182a032019-07-09 12:31:20 +0200965 send_config->send_codec_spec->target_bitrate_bps =
966 absl::optional<int>(kOpusBitrateFbBps);
Alex Narestd0e196b2017-11-22 17:22:35 +0100967 }
968
969 private:
Alex Narestd0e196b2017-11-22 17:22:35 +0100970 const int test_bitrate_from_;
971 const int test_bitrate_to_;
972 const int test_bitrate_step_;
973 const int min_bwe_;
974 const int start_bwe_;
975 const int max_bwe_;
Artem Titov631cafa2018-08-21 21:01:00 +0200976 SimulatedNetwork* send_simulated_network_;
977 SimulatedNetwork* receive_simulated_network_;
Alex Narestd0e196b2017-11-22 17:22:35 +0100978 Call* sender_call_;
Jonas Olsson0182a032019-07-09 12:31:20 +0200979 } test(test_bitrate_from, test_bitrate_to, test_bitrate_step, min_bwe,
980 start_bwe, max_bwe);
Alex Narestd0e196b2017-11-22 17:22:35 +0100981
982 RunBaseTest(&test);
983}
984
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800985// TODO(bugs.webrtc.org/8878)
986#if defined(WEBRTC_MAC)
Yves Gerey665174f2018-06-19 15:03:05 +0200987#define MAYBE_MinVideoAndAudioBitrate DISABLED_MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800988#else
Yves Gerey665174f2018-06-19 15:03:05 +0200989#define MAYBE_MinVideoAndAudioBitrate MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800990#endif
991TEST_F(CallPerfTest, MAYBE_MinVideoAndAudioBitrate) {
Jonas Olsson0182a032019-07-09 12:31:20 +0200992 TestMinAudioVideoBitrate(110, 40, -10, 10000, 70000, 200000);
Alex Narestd0e196b2017-11-22 17:22:35 +0100993}
994
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000995} // namespace webrtc