blob: 05c39feb8f3a5e472eee7d2de2f11de2a79c2fb2 [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"
32#include "system_wrappers/include/metrics_default.h"
33#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"
42#include "test/rtp_rtcp_observer.h"
43#include "test/single_threaded_task_queue.h"
44#include "test/testsupport/fileutils.h"
45#include "test/testsupport/perf_test.h"
Oleh Prypinefb94d52018-09-27 13:55:36 +000046#include "test/video_encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "video/transport_adapter.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000048
danilchap9c6a0c72016-02-10 10:54:47 -080049using webrtc::test::DriftingClock;
danilchap9c6a0c72016-02-10 10:54:47 -080050
pbos@webrtc.org1d096902013-12-13 12:48:05 +000051namespace webrtc {
52
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000053class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000054 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020055 enum class FecMode { kOn, kOff };
56 enum class CreateOrder { kAudioFirst, kVideoFirst };
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +010057 void TestAudioVideoSync(FecMode fec,
58 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -080059 float video_ntp_speed,
60 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +010061 float audio_rtp_speed,
62 const std::string& test_label);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000063
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000064 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
65
Artem Titov46c4e602018-08-17 14:26:54 +020066 void TestCaptureNtpTime(const DefaultNetworkSimulationConfig& net_config,
wu@webrtc.orgcd701192014-04-24 22:10:24 +000067 int threshold_ms,
68 int start_time_ms,
69 int run_time_ms);
Alex Narestd0e196b2017-11-22 17:22:35 +010070 void TestMinAudioVideoBitrate(bool use_bitrate_allocation_strategy,
71 int test_bitrate_from,
72 int test_bitrate_to,
73 int test_bitrate_step,
74 int min_bwe,
75 int start_bwe,
76 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000077};
78
asaperssonf8cdd182016-03-15 01:00:47 -070079class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070080 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000081 static const int kInSyncThresholdMs = 50;
82 static const int kStartupTimeMs = 2000;
83 static const int kMinRunTimeMs = 30000;
84
85 public:
Edward Lemur947f3fe2017-12-28 15:50:33 +010086 explicit VideoRtcpAndSyncObserver(Clock* clock, const std::string& test_label)
asaperssonf8cdd182016-03-15 01:00:47 -070087 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
88 clock_(clock),
Edward Lemur947f3fe2017-12-28 15:50:33 +010089 test_label_(test_label),
pbos@webrtc.org1d096902013-12-13 12:48:05 +000090 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -070091 first_time_in_sync_(-1),
92 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000093
nisseeb83a1a2016-03-21 01:27:56 -070094 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -070095 VideoReceiveStream::Stats stats;
96 {
97 rtc::CritScope lock(&crit_);
98 if (receive_stream_)
99 stats = receive_stream_->GetStats();
100 }
101 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
102 return;
103
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000104 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000105 int64_t time_since_creation = now_ms - creation_time_ms_;
106 // During the first couple of seconds audio and video can falsely be
107 // estimated as being synchronized. We don't want to trigger on those.
108 if (time_since_creation < kStartupTimeMs)
109 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700110 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000111 if (first_time_in_sync_ == -1) {
112 first_time_in_sync_ = now_ms;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100113 webrtc::test::PrintResult("sync_convergence_time", test_label_,
114 "synchronization", time_since_creation, "ms",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000115 false);
116 }
117 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100118 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000119 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200120 if (first_time_in_sync_ != -1)
121 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000122 }
123
asaperssonf8cdd182016-03-15 01:00:47 -0700124 void set_receive_stream(VideoReceiveStream* receive_stream) {
125 rtc::CritScope lock(&crit_);
126 receive_stream_ = receive_stream;
127 }
128
danilchap46b89b92016-06-03 09:27:37 -0700129 void PrintResults() {
Edward Lemur947f3fe2017-12-28 15:50:33 +0100130 test::PrintResultList("stream_offset", test_label_, "synchronization",
Edward Lemur2f061682017-11-24 13:40:01 +0100131 sync_offset_ms_list_, "ms", false);
danilchap46b89b92016-06-03 09:27:37 -0700132 }
133
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000134 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000135 Clock* const clock_;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100136 std::string test_label_;
stefanf116bd02015-10-27 08:29:42 -0700137 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000138 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700139 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700140 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100141 std::vector<double> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000142};
143
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100144void CallPerfTest::TestAudioVideoSync(FecMode fec,
145 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800146 float video_ntp_speed,
147 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +0100148 float audio_rtp_speed,
149 const std::string& test_label) {
pbos8fc7fa72015-07-15 08:02:58 -0700150 const char* kSyncGroup = "av_sync";
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100151 const uint32_t kAudioSendSsrc = 1234;
152 const uint32_t kAudioRecvSsrc = 5678;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000153
Artem Titov46c4e602018-08-17 14:26:54 +0200154 DefaultNetworkSimulationConfig audio_net_config;
mflodman3d7db262016-04-29 00:57:13 -0700155 audio_net_config.queue_delay_ms = 500;
156 audio_net_config.loss_percent = 5;
minyue20c84cc2017-04-10 16:57:57 -0700157
Edward Lemur947f3fe2017-12-28 15:50:33 +0100158 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), test_label);
eladalon413ee9a2017-08-22 04:02:52 -0700159
minyue20c84cc2017-04-10 16:57:57 -0700160 std::map<uint8_t, MediaType> audio_pt_map;
161 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700162
eladalon413ee9a2017-08-22 04:02:52 -0700163 std::unique_ptr<test::PacketTransport> audio_send_transport;
164 std::unique_ptr<test::PacketTransport> video_send_transport;
165 std::unique_ptr<test::PacketTransport> receive_transport;
mflodman3d7db262016-04-29 00:57:13 -0700166
eladalon413ee9a2017-08-22 04:02:52 -0700167 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100168 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700169 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700170
eladalon413ee9a2017-08-22 04:02:52 -0700171 task_queue_.SendTask([&]() {
172 metrics::Reset();
Artem Titov3faa8322018-03-07 14:44:00 +0100173 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device =
174 TestAudioDeviceModule::CreateTestAudioDeviceModule(
175 TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000),
176 TestAudioDeviceModule::CreateDiscardRenderer(48000),
177 audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100178 EXPECT_EQ(0, fake_audio_device->Init());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000179
eladalon413ee9a2017-08-22 04:02:52 -0700180 AudioState::Config send_audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -0700181 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +0100182 send_audio_state_config.audio_processing =
183 AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100184 send_audio_state_config.audio_device_module = fake_audio_device;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200185 Call::Config sender_config(send_event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000186
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100187 auto audio_state = AudioState::Create(send_audio_state_config);
188 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
189 sender_config.audio_state = audio_state;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200190 Call::Config receiver_config(recv_event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100191 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700192 CreateCalls(sender_config, receiver_config);
193
194 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
195 std::inserter(audio_pt_map, audio_pt_map.end()),
196 [](const std::pair<const uint8_t, MediaType>& pair) {
197 return pair.second == MediaType::AUDIO;
198 });
199 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
200 std::inserter(video_pt_map, video_pt_map.end()),
201 [](const std::pair<const uint8_t, MediaType>& pair) {
202 return pair.second == MediaType::VIDEO;
203 });
204
Karl Wiberg918f50c2018-07-05 11:40:33 +0200205 audio_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700206 &task_queue_, sender_call_.get(), &observer,
Artem Titov4e199e92018-08-20 13:30:39 +0200207 test::PacketTransport::kSender, audio_pt_map,
208 absl::make_unique<FakeNetworkPipe>(
209 Clock::GetRealTimeClock(),
210 absl::make_unique<SimulatedNetwork>(audio_net_config)));
eladalon413ee9a2017-08-22 04:02:52 -0700211 audio_send_transport->SetReceiver(receiver_call_->Receiver());
212
Karl Wiberg918f50c2018-07-05 11:40:33 +0200213 video_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700214 &task_queue_, sender_call_.get(), &observer,
215 test::PacketTransport::kSender, video_pt_map,
Artem Titov4e199e92018-08-20 13:30:39 +0200216 absl::make_unique<FakeNetworkPipe>(
217 Clock::GetRealTimeClock(), absl::make_unique<SimulatedNetwork>(
218 DefaultNetworkSimulationConfig())));
eladalon413ee9a2017-08-22 04:02:52 -0700219 video_send_transport->SetReceiver(receiver_call_->Receiver());
220
Karl Wiberg918f50c2018-07-05 11:40:33 +0200221 receive_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700222 &task_queue_, receiver_call_.get(), &observer,
223 test::PacketTransport::kReceiver, payload_type_map_,
Artem Titov4e199e92018-08-20 13:30:39 +0200224 absl::make_unique<FakeNetworkPipe>(
225 Clock::GetRealTimeClock(), absl::make_unique<SimulatedNetwork>(
226 DefaultNetworkSimulationConfig())));
eladalon413ee9a2017-08-22 04:02:52 -0700227 receive_transport->SetReceiver(sender_call_->Receiver());
228
229 CreateSendConfig(1, 0, 0, video_send_transport.get());
230 CreateMatchingReceiveConfigs(receive_transport.get());
231
232 AudioSendStream::Config audio_send_config(audio_send_transport.get());
eladalon413ee9a2017-08-22 04:02:52 -0700233 audio_send_config.rtp.ssrc = kAudioSendSsrc;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100234 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
235 kAudioSendPayloadType, {"ISAC", 16000, 1});
eladalon413ee9a2017-08-22 04:02:52 -0700236 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
237 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
238
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200239 GetVideoSendConfig()->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
eladalon413ee9a2017-08-22 04:02:52 -0700240 if (fec == FecMode::kOn) {
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200241 GetVideoSendConfig()->rtp.ulpfec.red_payload_type = kRedPayloadType;
242 GetVideoSendConfig()->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700243 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
244 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700245 }
246 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
247 video_receive_configs_[0].renderer = &observer;
248 video_receive_configs_[0].sync_group = kSyncGroup;
249
250 AudioReceiveStream::Config audio_recv_config;
251 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
252 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
eladalon413ee9a2017-08-22 04:02:52 -0700253 audio_recv_config.sync_group = kSyncGroup;
Niels Möller2784a032018-03-28 14:16:04 +0200254 audio_recv_config.decoder_factory = audio_decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700255 audio_recv_config.decoder_map = {
256 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
257
258 if (create_first == CreateOrder::kAudioFirst) {
259 audio_receive_stream =
260 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
261 CreateVideoStreams();
262 } else {
263 CreateVideoStreams();
264 audio_receive_stream =
265 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
266 }
267 EXPECT_EQ(1u, video_receive_streams_.size());
268 observer.set_receive_stream(video_receive_streams_[0]);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200269 drifting_clock = absl::make_unique<DriftingClock>(clock_, video_ntp_speed);
eladalon413ee9a2017-08-22 04:02:52 -0700270 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
271 kDefaultFramerate, kDefaultWidth,
272 kDefaultHeight);
273
274 Start();
275
276 audio_send_stream->Start();
277 audio_receive_stream->Start();
278 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000279
Peter Boström5811a392015-12-10 13:02:50 +0100280 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000281 << "Timed out while waiting for audio and video to be synchronized.";
282
eladalon413ee9a2017-08-22 04:02:52 -0700283 task_queue_.SendTask([&]() {
284 audio_send_stream->Stop();
285 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000286
eladalon413ee9a2017-08-22 04:02:52 -0700287 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000288
eladalon413ee9a2017-08-22 04:02:52 -0700289 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100290
eladalon413ee9a2017-08-22 04:02:52 -0700291 video_send_transport.reset();
292 audio_send_transport.reset();
293 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100294
eladalon413ee9a2017-08-22 04:02:52 -0700295 sender_call_->DestroyAudioSendStream(audio_send_stream);
296 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000297
eladalon413ee9a2017-08-22 04:02:52 -0700298 DestroyCalls();
eladalon413ee9a2017-08-22 04:02:52 -0700299 });
asaperssonf8cdd182016-03-15 01:00:47 -0700300
danilchap46b89b92016-06-03 09:27:37 -0700301 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800302
303 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800304 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
ilnik5328b9e2017-02-21 05:20:28 -0800305 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
306 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000307}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000308
Niels Möller9a750612018-08-09 11:04:32 +0200309TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithoutClockDrift) {
310 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
311 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
312 DriftingClock::kNoDrift, "_video_no_drift");
313}
314
danilchapac287ee2016-02-29 12:17:04 -0800315TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100316 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
317 DriftingClock::PercentsFaster(10.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100318 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
319 "_video_ntp_drift");
danilchap9c6a0c72016-02-10 10:54:47 -0800320}
321
danilchap9c6a0c72016-02-10 10:54:47 -0800322TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100323 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
324 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800325 DriftingClock::PercentsSlower(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100326 DriftingClock::PercentsFaster(30.0f), "_audio_faster");
danilchap9c6a0c72016-02-10 10:54:47 -0800327}
328
329TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100330 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
331 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800332 DriftingClock::PercentsFaster(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100333 DriftingClock::PercentsSlower(30.0f), "_video_faster");
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000334}
335
Artem Titov46c4e602018-08-17 14:26:54 +0200336void CallPerfTest::TestCaptureNtpTime(
337 const DefaultNetworkSimulationConfig& net_config,
338 int threshold_ms,
339 int start_time_ms,
340 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000341 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700342 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000343 public:
Artem Titov46c4e602018-08-17 14:26:54 +0200344 CaptureNtpTimeObserver(const DefaultNetworkSimulationConfig& net_config,
stefane74eef12016-01-08 06:47:13 -0800345 int threshold_ms,
346 int start_time_ms,
347 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700348 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800349 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000350 clock_(Clock::GetRealTimeClock()),
351 threshold_ms_(threshold_ms),
352 start_time_ms_(start_time_ms),
353 run_time_ms_(run_time_ms),
354 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000355 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000356 rtp_start_timestamp_set_(false),
357 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000358
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000359 private:
eladalon413ee9a2017-08-22 04:02:52 -0700360 test::PacketTransport* CreateSendTransport(
361 test::SingleThreadedTaskQueueForTesting* task_queue,
362 Call* sender_call) override {
Artem Titov4e199e92018-08-20 13:30:39 +0200363 return new test::PacketTransport(
364 task_queue, sender_call, this, test::PacketTransport::kSender,
365 payload_type_map_,
366 absl::make_unique<FakeNetworkPipe>(
367 Clock::GetRealTimeClock(),
368 absl::make_unique<SimulatedNetwork>(net_config_)));
stefane74eef12016-01-08 06:47:13 -0800369 }
370
eladalon413ee9a2017-08-22 04:02:52 -0700371 test::PacketTransport* CreateReceiveTransport(
372 test::SingleThreadedTaskQueueForTesting* task_queue) override {
Artem Titov4e199e92018-08-20 13:30:39 +0200373 return new test::PacketTransport(
374 task_queue, nullptr, this, test::PacketTransport::kReceiver,
375 payload_type_map_,
376 absl::make_unique<FakeNetworkPipe>(
377 Clock::GetRealTimeClock(),
378 absl::make_unique<SimulatedNetwork>(net_config_)));
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100379 }
380
nisseeb83a1a2016-03-21 01:27:56 -0700381 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700382 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000383 if (video_frame.ntp_time_ms() <= 0) {
384 // Haven't got enough RTCP SR in order to calculate the capture ntp
385 // time.
386 return;
387 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000388
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000389 int64_t now_ms = clock_->TimeInMilliseconds();
390 int64_t time_since_creation = now_ms - creation_time_ms_;
391 if (time_since_creation < start_time_ms_) {
392 // Wait for |start_time_ms_| before start measuring.
393 return;
394 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000395
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000396 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100397 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000398 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000399
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000400 FrameCaptureTimeList::iterator iter =
401 capture_time_list_.find(video_frame.timestamp());
402 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000403
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000404 // The real capture time has been wrapped to uint32_t before converted
405 // to rtp timestamp in the sender side. So here we convert the estimated
406 // capture time to a uint32_t 90k timestamp also for comparing.
407 uint32_t estimated_capture_timestamp =
408 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
409 uint32_t real_capture_timestamp = iter->second;
410 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
411 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700412 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000413
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000414 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
415 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000416
nisseef8b61e2016-04-29 06:09:15 -0700417 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700418 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000419 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000420 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000421
422 if (!rtp_start_timestamp_set_) {
423 // Calculate the rtp timestamp offset in order to calculate the real
424 // capture time.
425 uint32_t first_capture_timestamp =
426 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
427 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
428 rtp_start_timestamp_set_ = true;
429 }
430
431 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
432 capture_time_list_.insert(
433 capture_time_list_.end(),
434 std::make_pair(header.timestamp, capture_timestamp));
435 return SEND_PACKET;
436 }
437
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000438 void OnFrameGeneratorCapturerCreated(
439 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000440 capturer_ = frame_generator_capturer;
441 }
442
stefanff483612015-12-21 03:14:00 -0800443 void ModifyVideoConfigs(
444 VideoSendStream::Config* send_config,
445 std::vector<VideoReceiveStream::Config>* receive_configs,
446 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000447 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000448 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000449 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000450 }
451
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000452 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100453 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
454 "estimated capture NTP time to be "
455 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700456 test::PrintResultList("capture_ntp_time", "", "real - estimated",
Edward Lemur2f061682017-11-24 13:40:01 +0100457 time_offset_ms_list_, "ms", true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000458 }
459
stefanf116bd02015-10-27 08:29:42 -0700460 rtc::CriticalSection crit_;
Artem Titov46c4e602018-08-17 14:26:54 +0200461 const DefaultNetworkSimulationConfig net_config_;
stefanf116bd02015-10-27 08:29:42 -0700462 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000463 int threshold_ms_;
464 int start_time_ms_;
465 int run_time_ms_;
466 int64_t creation_time_ms_;
467 test::FrameGeneratorCapturer* capturer_;
468 bool rtp_start_timestamp_set_;
469 uint32_t rtp_start_timestamp_;
470 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700471 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100472 std::vector<double> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800473 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000474
stefane74eef12016-01-08 06:47:13 -0800475 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000476}
477
Alex Loiko5aea38c2017-09-27 13:10:28 +0200478// Flaky tests, disabled on Mac due to webrtc:8291.
479#if !(defined(WEBRTC_MAC))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000480TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
Artem Titov46c4e602018-08-17 14:26:54 +0200481 DefaultNetworkSimulationConfig net_config;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000482 net_config.queue_delay_ms = 100;
483 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
484 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000485 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000486 const int kStartTimeMs = 10000;
487 const int kRunTimeMs = 20000;
488 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
489}
490
wu@webrtc.org0224c202014-05-05 17:42:43 +0000491TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
Artem Titov46c4e602018-08-17 14:26:54 +0200492 DefaultNetworkSimulationConfig net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000493 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000494 net_config.delay_standard_deviation_ms = 10;
495 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
496 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000497 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000498 const int kStartTimeMs = 10000;
499 const int kRunTimeMs = 20000;
500 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
501}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200502#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800503
perkj803d97f2016-11-01 11:45:46 -0700504TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700505 // Minimal normal usage at the start, then 30s overuse to allow filter to
506 // settle, and then 80s underuse to allow plenty of time for rampup again.
507 test::ScopedFieldTrials fake_overuse_settings(
508 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
509
perkj803d97f2016-11-01 11:45:46 -0700510 class LoadObserver : public test::SendTest,
511 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000512 public:
Åsa Persson8c1bf952018-09-13 10:42:19 +0200513 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kInit) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000514
perkj803d97f2016-11-01 11:45:46 -0700515 void OnFrameGeneratorCapturerCreated(
516 test::FrameGeneratorCapturer* frame_generator_capturer) override {
517 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800518 // Set a high initial resolution to be sure that we can scale down.
519 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700520 }
521
522 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
523 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700524 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700525 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
526 const rtc::VideoSinkWants& wants) override {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200527 // At kStart expect CPU overuse. Then expect CPU underuse when the encoder
perkj803d97f2016-11-01 11:45:46 -0700528 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700529 switch (test_phase_) {
Åsa Persson8c1bf952018-09-13 10:42:19 +0200530 case TestPhase::kInit:
531 // Max framerate should be set initially.
532 if (wants.max_framerate_fps != std::numeric_limits<int>::max() &&
533 wants.max_pixel_count == std::numeric_limits<int>::max()) {
534 test_phase_ = TestPhase::kStart;
535 } else {
536 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
537 << wants.max_pixel_count << ", target res = "
538 << wants.target_pixel_count.value_or(-1)
539 << ", max fps = " << wants.max_framerate_fps;
540 }
541 break;
sprangc5d62e22017-04-02 23:53:04 -0700542 case TestPhase::kStart:
543 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700544 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
545 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700546 test_phase_ = TestPhase::kAdaptedDown;
547 } else {
548 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
549 << wants.max_pixel_count << ", target res = "
550 << wants.target_pixel_count.value_or(-1)
551 << ", max fps = " << wants.max_framerate_fps;
552 }
553 break;
554 case TestPhase::kAdaptedDown:
555 // On adapting up, the adaptation counter will again be at zero, and
556 // so all constraints will be reset.
557 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
558 !wants.target_pixel_count) {
559 test_phase_ = TestPhase::kAdaptedUp;
560 observation_complete_.Set();
561 } else {
562 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
563 << wants.max_pixel_count << ", target res = "
564 << wants.target_pixel_count.value_or(-1)
565 << ", max fps = " << wants.max_framerate_fps;
566 }
567 break;
568 case TestPhase::kAdaptedUp:
569 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
570 << wants.max_pixel_count << ", target res = "
571 << wants.target_pixel_count.value_or(-1)
572 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700573 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000574 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000575
stefanff483612015-12-21 03:14:00 -0800576 void ModifyVideoConfigs(
577 VideoSendStream::Config* send_config,
578 std::vector<VideoReceiveStream::Config>* receive_configs,
Yves Gerey665174f2018-06-19 15:03:05 +0200579 VideoEncoderConfig* encoder_config) override {}
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000580
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000581 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100582 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000583 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000584
Åsa Persson8c1bf952018-09-13 10:42:19 +0200585 enum class TestPhase {
586 kInit,
587 kStart,
588 kAdaptedDown,
589 kAdaptedUp
590 } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700591 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000592
stefane74eef12016-01-08 06:47:13 -0800593 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000594}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000595
596void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
597 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000598 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000599 static const int kMinAcceptableTransmitBitrate = 130;
600 static const int kMaxAcceptableTransmitBitrate = 170;
601 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700602 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700603 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000604 public:
605 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000606 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000607 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200608 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000609 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200610 min_acceptable_bitrate_(using_min_transmit_bitrate
611 ? kMinAcceptableTransmitBitrate
612 : (kMaxEncodeBitrateKbps -
613 kAcceptableBitrateErrorMargin / 2)),
614 max_acceptable_bitrate_(using_min_transmit_bitrate
615 ? kMaxAcceptableTransmitBitrate
616 : (kMaxEncodeBitrateKbps +
617 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000618 num_bitrate_observations_in_range_(0) {}
619
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000620 private:
stefanf116bd02015-10-27 08:29:42 -0700621 // TODO(holmer): Run this with a timer instead of once per packet.
622 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000623 VideoSendStream::Stats stats = send_stream_->GetStats();
624 if (stats.substreams.size() > 0) {
kwibergaf476c72016-11-28 15:21:39 -0800625 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000626 int bitrate_kbps =
627 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200628 if (bitrate_kbps > min_acceptable_bitrate_ &&
629 bitrate_kbps < max_acceptable_bitrate_) {
630 converged_ = true;
631 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000632 if (num_bitrate_observations_in_range_ ==
633 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100634 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000635 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200636 if (converged_)
637 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000638 }
stefanf116bd02015-10-27 08:29:42 -0700639 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000640 }
641
stefanff483612015-12-21 03:14:00 -0800642 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000643 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000644 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000645 send_stream_ = send_stream;
646 }
647
stefanff483612015-12-21 03:14:00 -0800648 void ModifyVideoConfigs(
649 VideoSendStream::Config* send_config,
650 std::vector<VideoReceiveStream::Config>* receive_configs,
651 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000652 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000653 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000654 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700655 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000656 }
657 }
658
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000659 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100660 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700661 test::PrintResultList(
662 "bitrate_stats_",
663 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
664 : "without_min_transmit_bitrate"),
Edward Lemur2f061682017-11-24 13:40:01 +0100665 "bitrate_kbps", bitrate_kbps_list_, "kbps", false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000666 }
667
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000668 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200669 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000670 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200671 const int min_acceptable_bitrate_;
672 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000673 int num_bitrate_observations_in_range_;
Edward Lemur2f061682017-11-24 13:40:01 +0100674 std::vector<double> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000675 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000676
Niels Möller4db138e2018-04-19 09:04:13 +0200677 fake_encoder_max_bitrate_ = kMaxEncodeBitrateKbps;
stefane74eef12016-01-08 06:47:13 -0800678 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000679}
680
Yves Gerey665174f2018-06-19 15:03:05 +0200681TEST_F(CallPerfTest, PadsToMinTransmitBitrate) {
682 TestMinTransmitBitrate(true);
683}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000684
685TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
686 TestMinTransmitBitrate(false);
687}
688
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800689// TODO(bugs.webrtc.org/8878)
690#if defined(WEBRTC_MAC)
691#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
692 DISABLED_KeepsHighBitrateWhenReconfiguringSender
693#else
694#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
695 KeepsHighBitrateWhenReconfiguringSender
696#endif
697TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000698 static const uint32_t kInitialBitrateKbps = 400;
699 static const uint32_t kReconfigureThresholdKbps = 600;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000700
perkjfa10b552016-10-02 23:45:26 -0700701 class VideoStreamFactory
702 : public VideoEncoderConfig::VideoStreamFactoryInterface {
703 public:
704 VideoStreamFactory() {}
705
706 private:
707 std::vector<VideoStream> CreateEncoderStreams(
708 int width,
709 int height,
710 const VideoEncoderConfig& encoder_config) override {
711 std::vector<VideoStream> streams =
712 test::CreateVideoStreams(width, height, encoder_config);
713 streams[0].min_bitrate_bps = 50000;
714 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
715 return streams;
716 }
717 };
718
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000719 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
720 public:
721 BitrateObserver()
722 : EndToEndTest(kDefaultTimeoutMs),
723 FakeEncoder(Clock::GetRealTimeClock()),
Peter Boström5811a392015-12-10 13:02:50 +0100724 time_to_reconfigure_(false, false),
sprang867fb522015-08-03 04:38:41 -0700725 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100726 last_set_bitrate_kbps_(0),
727 send_stream_(nullptr),
Niels Möller4db138e2018-04-19 09:04:13 +0200728 frame_generator_(nullptr),
729 encoder_factory_(this) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000730
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000731 int32_t InitEncode(const VideoCodec* config,
732 int32_t number_of_cores,
733 size_t max_payload_size) override {
perkjfa10b552016-10-02 23:45:26 -0700734 ++encoder_inits_;
735 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700736 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100737 // |expected_bitrate| is affected by bandwidth estimation before the
738 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100739 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
740 ? last_set_bitrate_kbps_
741 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100742 EXPECT_EQ(expected_bitrate, config->startBitrate)
743 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700744 EXPECT_EQ(kDefaultWidth, config->width);
745 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100746 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700747 EXPECT_EQ(2 * kDefaultWidth, config->width);
748 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100749 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
philipel0676f222018-04-17 16:12:21 +0200750 EXPECT_GT(config->startBitrate, kReconfigureThresholdKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000751 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100752 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000753 }
754 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
755 }
756
Erik Språng566124a2018-04-23 12:32:22 +0200757 int32_t SetRateAllocation(const VideoBitrateAllocation& rate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100758 uint32_t framerate) override {
759 last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100760 if (encoder_inits_ == 1 &&
Erik Språng08127a92016-11-16 16:41:30 +0100761 rate_allocation.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100762 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000763 }
Erik Språng08127a92016-11-16 16:41:30 +0100764 return FakeEncoder::SetRateAllocation(rate_allocation, framerate);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000765 }
766
Sebastian Jansson72582242018-07-13 13:19:42 +0200767 void ModifySenderCallConfig(Call::Config* config) override {
768 config->bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000769 }
770
stefanff483612015-12-21 03:14:00 -0800771 void ModifyVideoConfigs(
772 VideoSendStream::Config* send_config,
773 std::vector<VideoReceiveStream::Config>* receive_configs,
774 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200775 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Per21d45d22016-10-30 21:37:57 +0100776 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700777 encoder_config->video_stream_factory =
778 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000779
perkj26091b12016-09-01 01:17:40 -0700780 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000781 }
782
stefanff483612015-12-21 03:14:00 -0800783 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000784 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000785 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000786 send_stream_ = send_stream;
787 }
788
perkjfa10b552016-10-02 23:45:26 -0700789 void OnFrameGeneratorCapturerCreated(
790 test::FrameGeneratorCapturer* frame_generator_capturer) override {
791 frame_generator_ = frame_generator_capturer;
792 }
793
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000794 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100795 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000796 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700797 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700798 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100799 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000800 << "Timed out while waiting for a couple of high bitrate estimates "
801 "after reconfiguring the send stream.";
802 }
803
804 private:
Peter Boström5811a392015-12-10 13:02:50 +0100805 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000806 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100807 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000808 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700809 test::FrameGeneratorCapturer* frame_generator_;
Oleh Prypinefb94d52018-09-27 13:55:36 +0000810 test::VideoEncoderProxyFactory encoder_factory_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000811 VideoEncoderConfig encoder_config_;
812 } test;
813
stefane74eef12016-01-08 06:47:13 -0800814 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000815}
816
Alex Narestd0e196b2017-11-22 17:22:35 +0100817// Discovers the minimal supported audio+video bitrate. The test bitrate is
818// considered supported if Rtt does not go above 400ms with the network
819// contrained to the test bitrate.
820//
821// |use_bitrate_allocation_strategy| use AudioPriorityBitrateAllocationStrategy
822// |test_bitrate_from test_bitrate_to| bitrate constraint range
823// |test_bitrate_step| bitrate constraint update step during the test
824// |min_bwe max_bwe| BWE range
825// |start_bwe| initial BWE
826void CallPerfTest::TestMinAudioVideoBitrate(
827 bool use_bitrate_allocation_strategy,
828 int test_bitrate_from,
829 int test_bitrate_to,
830 int test_bitrate_step,
831 int min_bwe,
832 int start_bwe,
833 int max_bwe) {
834 static const std::string kAudioTrackId = "audio_track_0";
835 static constexpr uint32_t kSufficientAudioBitrateBps = 16000;
836 static constexpr int kOpusMinBitrateBps = 6000;
837 static constexpr int kOpusBitrateFbBps = 32000;
838 static constexpr int kBitrateStabilizationMs = 10000;
839 static constexpr int kBitrateMeasurements = 10;
840 static constexpr int kBitrateMeasurementMs = 1000;
841 static constexpr int kMinGoodRttMs = 400;
842
843 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
844 public:
845 MinVideoAndAudioBitrateTester(bool use_bitrate_allocation_strategy,
846 int test_bitrate_from,
847 int test_bitrate_to,
848 int test_bitrate_step,
849 int min_bwe,
850 int start_bwe,
851 int max_bwe)
852 : EndToEndTest(),
853 allocation_strategy_(new rtc::AudioPriorityBitrateAllocationStrategy(
854 kAudioTrackId,
855 kSufficientAudioBitrateBps)),
856 use_bitrate_allocation_strategy_(use_bitrate_allocation_strategy),
857 test_bitrate_from_(test_bitrate_from),
858 test_bitrate_to_(test_bitrate_to),
859 test_bitrate_step_(test_bitrate_step),
860 min_bwe_(min_bwe),
861 start_bwe_(start_bwe),
862 max_bwe_(max_bwe) {}
863
864 protected:
Artem Titov46c4e602018-08-17 14:26:54 +0200865 DefaultNetworkSimulationConfig GetFakeNetworkPipeConfig() {
866 DefaultNetworkSimulationConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100867 pipe_config.link_capacity_kbps = test_bitrate_from_;
868 return pipe_config;
869 }
870
871 test::PacketTransport* CreateSendTransport(
872 test::SingleThreadedTaskQueueForTesting* task_queue,
873 Call* sender_call) override {
Artem Titov631cafa2018-08-21 21:01:00 +0200874 auto network =
875 absl::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig());
876 send_simulated_network_ = network.get();
877 return new test::PacketTransport(
878 task_queue, sender_call, this, test::PacketTransport::kSender,
879 test::CallTest::payload_type_map_,
880 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
881 std::move(network)));
Alex Narestd0e196b2017-11-22 17:22:35 +0100882 }
883
884 test::PacketTransport* CreateReceiveTransport(
885 test::SingleThreadedTaskQueueForTesting* task_queue) override {
Artem Titov631cafa2018-08-21 21:01:00 +0200886 auto network =
887 absl::make_unique<SimulatedNetwork>(GetFakeNetworkPipeConfig());
888 receive_simulated_network_ = network.get();
889 return new test::PacketTransport(
890 task_queue, nullptr, this, test::PacketTransport::kReceiver,
891 test::CallTest::payload_type_map_,
892 absl::make_unique<FakeNetworkPipe>(Clock::GetRealTimeClock(),
893 std::move(network)));
Alex Narestd0e196b2017-11-22 17:22:35 +0100894 }
895
896 void PerformTest() override {
897 int last_passed_test_bitrate = -1;
898 for (int test_bitrate = test_bitrate_from_;
899 test_bitrate_from_ < test_bitrate_to_
900 ? test_bitrate <= test_bitrate_to_
901 : test_bitrate >= test_bitrate_to_;
902 test_bitrate += test_bitrate_step_) {
Artem Titov46c4e602018-08-17 14:26:54 +0200903 DefaultNetworkSimulationConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100904 pipe_config.link_capacity_kbps = test_bitrate;
Artem Titov631cafa2018-08-21 21:01:00 +0200905 send_simulated_network_->SetConfig(pipe_config);
906 receive_simulated_network_->SetConfig(pipe_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100907
908 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
909 kBitrateStabilizationMs);
910
911 int64_t avg_rtt = 0;
912 for (int i = 0; i < kBitrateMeasurements; i++) {
913 Call::Stats call_stats = sender_call_->GetStats();
914 avg_rtt += call_stats.rtt_ms;
915 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
916 kBitrateMeasurementMs);
917 }
918 avg_rtt = avg_rtt / kBitrateMeasurements;
919 if (avg_rtt > kMinGoodRttMs) {
920 break;
921 } else {
922 last_passed_test_bitrate = test_bitrate;
923 }
924 }
925 EXPECT_GT(last_passed_test_bitrate, -1)
926 << "Minimum supported bitrate out of the test scope";
Edward Lemur7f331fa2018-01-08 17:35:51 +0100927 webrtc::test::PrintResult(
928 "min_test_bitrate_",
929 use_bitrate_allocation_strategy_ ? "with_allocation_strategy"
930 : "no_allocation_strategy",
931 "min_bitrate", last_passed_test_bitrate, "kbps", false);
Alex Narestd0e196b2017-11-22 17:22:35 +0100932 }
933
934 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
935 sender_call_ = sender_call;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100936 BitrateConstraints bitrate_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100937 bitrate_config.min_bitrate_bps = min_bwe_;
938 bitrate_config.start_bitrate_bps = start_bwe_;
939 bitrate_config.max_bitrate_bps = max_bwe_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100940 sender_call->GetTransportControllerSend()->SetSdpBitrateParameters(
941 bitrate_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100942 if (use_bitrate_allocation_strategy_) {
943 sender_call->SetBitrateAllocationStrategy(
944 std::move(allocation_strategy_));
945 }
946 }
947
948 size_t GetNumVideoStreams() const override { return 1; }
949
950 size_t GetNumAudioStreams() const override { return 1; }
951
952 void ModifyAudioConfigs(
953 AudioSendStream::Config* send_config,
954 std::vector<AudioReceiveStream::Config>* receive_configs) override {
955 if (use_bitrate_allocation_strategy_) {
956 send_config->track_id = kAudioTrackId;
957 send_config->min_bitrate_bps = kOpusMinBitrateBps;
958 send_config->max_bitrate_bps = kOpusBitrateFbBps;
959 } else {
960 send_config->send_codec_spec->target_bitrate_bps =
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200961 absl::optional<int>(kOpusBitrateFbBps);
Alex Narestd0e196b2017-11-22 17:22:35 +0100962 }
963 }
964
965 private:
966 std::unique_ptr<rtc::BitrateAllocationStrategy> allocation_strategy_;
967 const bool use_bitrate_allocation_strategy_;
968 const int test_bitrate_from_;
969 const int test_bitrate_to_;
970 const int test_bitrate_step_;
971 const int min_bwe_;
972 const int start_bwe_;
973 const int max_bwe_;
Artem Titov631cafa2018-08-21 21:01:00 +0200974 SimulatedNetwork* send_simulated_network_;
975 SimulatedNetwork* receive_simulated_network_;
Alex Narestd0e196b2017-11-22 17:22:35 +0100976 Call* sender_call_;
977 } test(use_bitrate_allocation_strategy, test_bitrate_from, test_bitrate_to,
978 test_bitrate_step, min_bwe, start_bwe, max_bwe);
979
980 RunBaseTest(&test);
981}
982
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800983// TODO(bugs.webrtc.org/8878)
984#if defined(WEBRTC_MAC)
Yves Gerey665174f2018-06-19 15:03:05 +0200985#define MAYBE_MinVideoAndAudioBitrate DISABLED_MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800986#else
Yves Gerey665174f2018-06-19 15:03:05 +0200987#define MAYBE_MinVideoAndAudioBitrate MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800988#endif
989TEST_F(CallPerfTest, MAYBE_MinVideoAndAudioBitrate) {
Alex Narestd0e196b2017-11-22 17:22:35 +0100990 TestMinAudioVideoBitrate(false, 110, 40, -10, 10000, 70000, 200000);
991}
992TEST_F(CallPerfTest, MinVideoAndAudioBitrateWStrategy) {
993 TestMinAudioVideoBitrate(true, 110, 40, -10, 10000, 70000, 200000);
994}
995
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000996} // namespace webrtc