blob: dd70e320b4f9db01a707daf53877faed997a8b1c [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"
Erik Språngef75ebe2018-05-15 15:18:36 +020018#include "api/video/video_bitrate_allocation.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020019#include "api/video_codecs/video_encoder_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/call.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "logging/rtc_event_log/rtc_event_log.h"
22#include "modules/audio_coding/include/audio_coding_module.h"
Artem Titov3faa8322018-03-07 14:44:00 +010023#include "modules/audio_device/include/test_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_mixer/audio_mixer_impl.h"
25#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Alex Narestd0e196b2017-11-22 17:22:35 +010026#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/thread_annotations.h"
29#include "system_wrappers/include/metrics_default.h"
30#include "test/call_test.h"
31#include "test/direct_transport.h"
32#include "test/drifting_clock.h"
Niels Möller4db138e2018-04-19 09:04:13 +020033#include "test/encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "test/encoder_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "test/fake_encoder.h"
36#include "test/field_trial.h"
37#include "test/frame_generator.h"
38#include "test/frame_generator_capturer.h"
39#include "test/gtest.h"
40#include "test/rtp_rtcp_observer.h"
41#include "test/single_threaded_task_queue.h"
42#include "test/testsupport/fileutils.h"
43#include "test/testsupport/perf_test.h"
44#include "video/transport_adapter.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000045
danilchap9c6a0c72016-02-10 10:54:47 -080046using webrtc::test::DriftingClock;
danilchap9c6a0c72016-02-10 10:54:47 -080047
pbos@webrtc.org1d096902013-12-13 12:48:05 +000048namespace webrtc {
49
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000050class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000051 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020052 enum class FecMode { kOn, kOff };
53 enum class CreateOrder { kAudioFirst, kVideoFirst };
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +010054 void TestAudioVideoSync(FecMode fec,
55 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -080056 float video_ntp_speed,
57 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +010058 float audio_rtp_speed,
59 const std::string& test_label);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000060
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000061 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
62
wu@webrtc.orgcd701192014-04-24 22:10:24 +000063 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
64 int threshold_ms,
65 int start_time_ms,
66 int run_time_ms);
Alex Narestd0e196b2017-11-22 17:22:35 +010067 void TestMinAudioVideoBitrate(bool use_bitrate_allocation_strategy,
68 int test_bitrate_from,
69 int test_bitrate_to,
70 int test_bitrate_step,
71 int min_bwe,
72 int start_bwe,
73 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000074};
75
asaperssonf8cdd182016-03-15 01:00:47 -070076class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070077 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000078 static const int kInSyncThresholdMs = 50;
79 static const int kStartupTimeMs = 2000;
80 static const int kMinRunTimeMs = 30000;
81
82 public:
Edward Lemur947f3fe2017-12-28 15:50:33 +010083 explicit VideoRtcpAndSyncObserver(Clock* clock, const std::string& test_label)
asaperssonf8cdd182016-03-15 01:00:47 -070084 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
85 clock_(clock),
Edward Lemur947f3fe2017-12-28 15:50:33 +010086 test_label_(test_label),
pbos@webrtc.org1d096902013-12-13 12:48:05 +000087 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -070088 first_time_in_sync_(-1),
89 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000090
nisseeb83a1a2016-03-21 01:27:56 -070091 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -070092 VideoReceiveStream::Stats stats;
93 {
94 rtc::CritScope lock(&crit_);
95 if (receive_stream_)
96 stats = receive_stream_->GetStats();
97 }
98 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
99 return;
100
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000101 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000102 int64_t time_since_creation = now_ms - creation_time_ms_;
103 // During the first couple of seconds audio and video can falsely be
104 // estimated as being synchronized. We don't want to trigger on those.
105 if (time_since_creation < kStartupTimeMs)
106 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700107 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000108 if (first_time_in_sync_ == -1) {
109 first_time_in_sync_ = now_ms;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100110 webrtc::test::PrintResult("sync_convergence_time", test_label_,
111 "synchronization", time_since_creation, "ms",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000112 false);
113 }
114 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100115 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000116 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200117 if (first_time_in_sync_ != -1)
118 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000119 }
120
asaperssonf8cdd182016-03-15 01:00:47 -0700121 void set_receive_stream(VideoReceiveStream* receive_stream) {
122 rtc::CritScope lock(&crit_);
123 receive_stream_ = receive_stream;
124 }
125
danilchap46b89b92016-06-03 09:27:37 -0700126 void PrintResults() {
Edward Lemur947f3fe2017-12-28 15:50:33 +0100127 test::PrintResultList("stream_offset", test_label_, "synchronization",
Edward Lemur2f061682017-11-24 13:40:01 +0100128 sync_offset_ms_list_, "ms", false);
danilchap46b89b92016-06-03 09:27:37 -0700129 }
130
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000131 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000132 Clock* const clock_;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100133 std::string test_label_;
stefanf116bd02015-10-27 08:29:42 -0700134 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000135 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700136 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700137 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100138 std::vector<double> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000139};
140
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100141void CallPerfTest::TestAudioVideoSync(FecMode fec,
142 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800143 float video_ntp_speed,
144 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +0100145 float audio_rtp_speed,
146 const std::string& test_label) {
pbos8fc7fa72015-07-15 08:02:58 -0700147 const char* kSyncGroup = "av_sync";
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100148 const uint32_t kAudioSendSsrc = 1234;
149 const uint32_t kAudioRecvSsrc = 5678;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000150
mflodman3d7db262016-04-29 00:57:13 -0700151 FakeNetworkPipe::Config audio_net_config;
152 audio_net_config.queue_delay_ms = 500;
153 audio_net_config.loss_percent = 5;
minyue20c84cc2017-04-10 16:57:57 -0700154
Edward Lemur947f3fe2017-12-28 15:50:33 +0100155 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), test_label);
eladalon413ee9a2017-08-22 04:02:52 -0700156
minyue20c84cc2017-04-10 16:57:57 -0700157 std::map<uint8_t, MediaType> audio_pt_map;
158 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700159
eladalon413ee9a2017-08-22 04:02:52 -0700160 std::unique_ptr<test::PacketTransport> audio_send_transport;
161 std::unique_ptr<test::PacketTransport> video_send_transport;
162 std::unique_ptr<test::PacketTransport> receive_transport;
mflodman3d7db262016-04-29 00:57:13 -0700163
eladalon413ee9a2017-08-22 04:02:52 -0700164 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100165 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700166 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700167
eladalon413ee9a2017-08-22 04:02:52 -0700168 task_queue_.SendTask([&]() {
169 metrics::Reset();
Artem Titov3faa8322018-03-07 14:44:00 +0100170 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device =
171 TestAudioDeviceModule::CreateTestAudioDeviceModule(
172 TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000),
173 TestAudioDeviceModule::CreateDiscardRenderer(48000),
174 audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100175 EXPECT_EQ(0, fake_audio_device->Init());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000176
eladalon413ee9a2017-08-22 04:02:52 -0700177 AudioState::Config send_audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -0700178 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +0100179 send_audio_state_config.audio_processing =
180 AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100181 send_audio_state_config.audio_device_module = fake_audio_device;
eladalon413ee9a2017-08-22 04:02:52 -0700182 Call::Config sender_config(event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000183
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100184 auto audio_state = AudioState::Create(send_audio_state_config);
185 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
186 sender_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700187 Call::Config receiver_config(event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100188 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700189 CreateCalls(sender_config, receiver_config);
190
191 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
192 std::inserter(audio_pt_map, audio_pt_map.end()),
193 [](const std::pair<const uint8_t, MediaType>& pair) {
194 return pair.second == MediaType::AUDIO;
195 });
196 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
197 std::inserter(video_pt_map, video_pt_map.end()),
198 [](const std::pair<const uint8_t, MediaType>& pair) {
199 return pair.second == MediaType::VIDEO;
200 });
201
Karl Wiberg918f50c2018-07-05 11:40:33 +0200202 audio_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700203 &task_queue_, sender_call_.get(), &observer,
204 test::PacketTransport::kSender, audio_pt_map, audio_net_config);
205 audio_send_transport->SetReceiver(receiver_call_->Receiver());
206
Karl Wiberg918f50c2018-07-05 11:40:33 +0200207 video_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700208 &task_queue_, sender_call_.get(), &observer,
209 test::PacketTransport::kSender, video_pt_map,
210 FakeNetworkPipe::Config());
211 video_send_transport->SetReceiver(receiver_call_->Receiver());
212
Karl Wiberg918f50c2018-07-05 11:40:33 +0200213 receive_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700214 &task_queue_, receiver_call_.get(), &observer,
215 test::PacketTransport::kReceiver, payload_type_map_,
216 FakeNetworkPipe::Config());
217 receive_transport->SetReceiver(sender_call_->Receiver());
218
219 CreateSendConfig(1, 0, 0, video_send_transport.get());
220 CreateMatchingReceiveConfigs(receive_transport.get());
221
222 AudioSendStream::Config audio_send_config(audio_send_transport.get());
eladalon413ee9a2017-08-22 04:02:52 -0700223 audio_send_config.rtp.ssrc = kAudioSendSsrc;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100224 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
225 kAudioSendPayloadType, {"ISAC", 16000, 1});
eladalon413ee9a2017-08-22 04:02:52 -0700226 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
227 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
228
229 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
230 if (fec == FecMode::kOn) {
231 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
232 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700233 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
234 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700235 }
236 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
237 video_receive_configs_[0].renderer = &observer;
238 video_receive_configs_[0].sync_group = kSyncGroup;
239
240 AudioReceiveStream::Config audio_recv_config;
241 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
242 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
eladalon413ee9a2017-08-22 04:02:52 -0700243 audio_recv_config.sync_group = kSyncGroup;
Niels Möller2784a032018-03-28 14:16:04 +0200244 audio_recv_config.decoder_factory = audio_decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700245 audio_recv_config.decoder_map = {
246 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
247
248 if (create_first == CreateOrder::kAudioFirst) {
249 audio_receive_stream =
250 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
251 CreateVideoStreams();
252 } else {
253 CreateVideoStreams();
254 audio_receive_stream =
255 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
256 }
257 EXPECT_EQ(1u, video_receive_streams_.size());
258 observer.set_receive_stream(video_receive_streams_[0]);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200259 drifting_clock = absl::make_unique<DriftingClock>(clock_, video_ntp_speed);
eladalon413ee9a2017-08-22 04:02:52 -0700260 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
261 kDefaultFramerate, kDefaultWidth,
262 kDefaultHeight);
263
264 Start();
265
266 audio_send_stream->Start();
267 audio_receive_stream->Start();
268 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000269
Peter Boström5811a392015-12-10 13:02:50 +0100270 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000271 << "Timed out while waiting for audio and video to be synchronized.";
272
eladalon413ee9a2017-08-22 04:02:52 -0700273 task_queue_.SendTask([&]() {
274 audio_send_stream->Stop();
275 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000276
eladalon413ee9a2017-08-22 04:02:52 -0700277 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000278
eladalon413ee9a2017-08-22 04:02:52 -0700279 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100280
eladalon413ee9a2017-08-22 04:02:52 -0700281 video_send_transport.reset();
282 audio_send_transport.reset();
283 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100284
eladalon413ee9a2017-08-22 04:02:52 -0700285 sender_call_->DestroyAudioSendStream(audio_send_stream);
286 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000287
eladalon413ee9a2017-08-22 04:02:52 -0700288 DestroyCalls();
eladalon413ee9a2017-08-22 04:02:52 -0700289 });
asaperssonf8cdd182016-03-15 01:00:47 -0700290
danilchap46b89b92016-06-03 09:27:37 -0700291 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800292
293 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800294 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
ilnik5328b9e2017-02-21 05:20:28 -0800295 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
296 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000297}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000298
danilchapac287ee2016-02-29 12:17:04 -0800299TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100300 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
301 DriftingClock::PercentsFaster(10.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100302 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
303 "_video_ntp_drift");
danilchap9c6a0c72016-02-10 10:54:47 -0800304}
305
danilchap9c6a0c72016-02-10 10:54:47 -0800306TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100307 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
308 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800309 DriftingClock::PercentsSlower(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100310 DriftingClock::PercentsFaster(30.0f), "_audio_faster");
danilchap9c6a0c72016-02-10 10:54:47 -0800311}
312
313TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100314 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
315 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800316 DriftingClock::PercentsFaster(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100317 DriftingClock::PercentsSlower(30.0f), "_video_faster");
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000318}
319
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000320void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
321 int threshold_ms,
322 int start_time_ms,
323 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000324 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700325 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000326 public:
stefane74eef12016-01-08 06:47:13 -0800327 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& net_config,
328 int threshold_ms,
329 int start_time_ms,
330 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700331 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800332 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000333 clock_(Clock::GetRealTimeClock()),
334 threshold_ms_(threshold_ms),
335 start_time_ms_(start_time_ms),
336 run_time_ms_(run_time_ms),
337 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000338 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000339 rtp_start_timestamp_set_(false),
340 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000341
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000342 private:
eladalon413ee9a2017-08-22 04:02:52 -0700343 test::PacketTransport* CreateSendTransport(
344 test::SingleThreadedTaskQueueForTesting* task_queue,
345 Call* sender_call) override {
346 return new test::PacketTransport(task_queue, sender_call, this,
minyue20c84cc2017-04-10 16:57:57 -0700347 test::PacketTransport::kSender,
348 payload_type_map_, net_config_);
stefane74eef12016-01-08 06:47:13 -0800349 }
350
eladalon413ee9a2017-08-22 04:02:52 -0700351 test::PacketTransport* CreateReceiveTransport(
352 test::SingleThreadedTaskQueueForTesting* task_queue) override {
353 return new test::PacketTransport(task_queue, nullptr, this,
minyue20c84cc2017-04-10 16:57:57 -0700354 test::PacketTransport::kReceiver,
355 payload_type_map_, net_config_);
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100356 }
357
nisseeb83a1a2016-03-21 01:27:56 -0700358 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700359 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000360 if (video_frame.ntp_time_ms() <= 0) {
361 // Haven't got enough RTCP SR in order to calculate the capture ntp
362 // time.
363 return;
364 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000365
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000366 int64_t now_ms = clock_->TimeInMilliseconds();
367 int64_t time_since_creation = now_ms - creation_time_ms_;
368 if (time_since_creation < start_time_ms_) {
369 // Wait for |start_time_ms_| before start measuring.
370 return;
371 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000372
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000373 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100374 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000375 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000376
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000377 FrameCaptureTimeList::iterator iter =
378 capture_time_list_.find(video_frame.timestamp());
379 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000380
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000381 // The real capture time has been wrapped to uint32_t before converted
382 // to rtp timestamp in the sender side. So here we convert the estimated
383 // capture time to a uint32_t 90k timestamp also for comparing.
384 uint32_t estimated_capture_timestamp =
385 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
386 uint32_t real_capture_timestamp = iter->second;
387 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
388 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700389 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000390
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000391 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
392 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000393
nisseef8b61e2016-04-29 06:09:15 -0700394 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700395 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000396 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000397 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000398
399 if (!rtp_start_timestamp_set_) {
400 // Calculate the rtp timestamp offset in order to calculate the real
401 // capture time.
402 uint32_t first_capture_timestamp =
403 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
404 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
405 rtp_start_timestamp_set_ = true;
406 }
407
408 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
409 capture_time_list_.insert(
410 capture_time_list_.end(),
411 std::make_pair(header.timestamp, capture_timestamp));
412 return SEND_PACKET;
413 }
414
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000415 void OnFrameGeneratorCapturerCreated(
416 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000417 capturer_ = frame_generator_capturer;
418 }
419
stefanff483612015-12-21 03:14:00 -0800420 void ModifyVideoConfigs(
421 VideoSendStream::Config* send_config,
422 std::vector<VideoReceiveStream::Config>* receive_configs,
423 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000424 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000425 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000426 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000427 }
428
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000429 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100430 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
431 "estimated capture NTP time to be "
432 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700433 test::PrintResultList("capture_ntp_time", "", "real - estimated",
Edward Lemur2f061682017-11-24 13:40:01 +0100434 time_offset_ms_list_, "ms", true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000435 }
436
stefanf116bd02015-10-27 08:29:42 -0700437 rtc::CriticalSection crit_;
stefane74eef12016-01-08 06:47:13 -0800438 const FakeNetworkPipe::Config net_config_;
stefanf116bd02015-10-27 08:29:42 -0700439 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000440 int threshold_ms_;
441 int start_time_ms_;
442 int run_time_ms_;
443 int64_t creation_time_ms_;
444 test::FrameGeneratorCapturer* capturer_;
445 bool rtp_start_timestamp_set_;
446 uint32_t rtp_start_timestamp_;
447 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700448 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100449 std::vector<double> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800450 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000451
stefane74eef12016-01-08 06:47:13 -0800452 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000453}
454
Alex Loiko5aea38c2017-09-27 13:10:28 +0200455// Flaky tests, disabled on Mac due to webrtc:8291.
456#if !(defined(WEBRTC_MAC))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000457TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000458 FakeNetworkPipe::Config net_config;
459 net_config.queue_delay_ms = 100;
460 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
461 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000462 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000463 const int kStartTimeMs = 10000;
464 const int kRunTimeMs = 20000;
465 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
466}
467
wu@webrtc.org0224c202014-05-05 17:42:43 +0000468TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000469 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000470 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000471 net_config.delay_standard_deviation_ms = 10;
472 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
473 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000474 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000475 const int kStartTimeMs = 10000;
476 const int kRunTimeMs = 20000;
477 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
478}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200479#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800480
perkj803d97f2016-11-01 11:45:46 -0700481TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700482 // Minimal normal usage at the start, then 30s overuse to allow filter to
483 // settle, and then 80s underuse to allow plenty of time for rampup again.
484 test::ScopedFieldTrials fake_overuse_settings(
485 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
486
perkj803d97f2016-11-01 11:45:46 -0700487 class LoadObserver : public test::SendTest,
488 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000489 public:
sprangc5d62e22017-04-02 23:53:04 -0700490 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kStart) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000491
perkj803d97f2016-11-01 11:45:46 -0700492 void OnFrameGeneratorCapturerCreated(
493 test::FrameGeneratorCapturer* frame_generator_capturer) override {
494 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800495 // Set a high initial resolution to be sure that we can scale down.
496 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700497 }
498
499 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
500 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700501 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700502 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
503 const rtc::VideoSinkWants& wants) override {
504 // First expect CPU overuse. Then expect CPU underuse when the encoder
505 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700506 switch (test_phase_) {
507 case TestPhase::kStart:
508 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700509 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
510 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700511 test_phase_ = TestPhase::kAdaptedDown;
512 } else {
513 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
514 << wants.max_pixel_count << ", target res = "
515 << wants.target_pixel_count.value_or(-1)
516 << ", max fps = " << wants.max_framerate_fps;
517 }
518 break;
519 case TestPhase::kAdaptedDown:
520 // On adapting up, the adaptation counter will again be at zero, and
521 // so all constraints will be reset.
522 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
523 !wants.target_pixel_count) {
524 test_phase_ = TestPhase::kAdaptedUp;
525 observation_complete_.Set();
526 } else {
527 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
528 << wants.max_pixel_count << ", target res = "
529 << wants.target_pixel_count.value_or(-1)
530 << ", max fps = " << wants.max_framerate_fps;
531 }
532 break;
533 case TestPhase::kAdaptedUp:
534 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
535 << wants.max_pixel_count << ", target res = "
536 << wants.target_pixel_count.value_or(-1)
537 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700538 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000539 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000540
stefanff483612015-12-21 03:14:00 -0800541 void ModifyVideoConfigs(
542 VideoSendStream::Config* send_config,
543 std::vector<VideoReceiveStream::Config>* receive_configs,
Yves Gerey665174f2018-06-19 15:03:05 +0200544 VideoEncoderConfig* encoder_config) override {}
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000545
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000546 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100547 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000548 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000549
sprangc5d62e22017-04-02 23:53:04 -0700550 enum class TestPhase { kStart, kAdaptedDown, kAdaptedUp } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700551 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000552
stefane74eef12016-01-08 06:47:13 -0800553 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000554}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000555
556void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
557 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000558 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000559 static const int kMinAcceptableTransmitBitrate = 130;
560 static const int kMaxAcceptableTransmitBitrate = 170;
561 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700562 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700563 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000564 public:
565 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000566 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000567 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200568 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000569 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200570 min_acceptable_bitrate_(using_min_transmit_bitrate
571 ? kMinAcceptableTransmitBitrate
572 : (kMaxEncodeBitrateKbps -
573 kAcceptableBitrateErrorMargin / 2)),
574 max_acceptable_bitrate_(using_min_transmit_bitrate
575 ? kMaxAcceptableTransmitBitrate
576 : (kMaxEncodeBitrateKbps +
577 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000578 num_bitrate_observations_in_range_(0) {}
579
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000580 private:
stefanf116bd02015-10-27 08:29:42 -0700581 // TODO(holmer): Run this with a timer instead of once per packet.
582 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000583 VideoSendStream::Stats stats = send_stream_->GetStats();
584 if (stats.substreams.size() > 0) {
kwibergaf476c72016-11-28 15:21:39 -0800585 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000586 int bitrate_kbps =
587 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200588 if (bitrate_kbps > min_acceptable_bitrate_ &&
589 bitrate_kbps < max_acceptable_bitrate_) {
590 converged_ = true;
591 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000592 if (num_bitrate_observations_in_range_ ==
593 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100594 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000595 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200596 if (converged_)
597 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000598 }
stefanf116bd02015-10-27 08:29:42 -0700599 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000600 }
601
stefanff483612015-12-21 03:14:00 -0800602 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000603 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000604 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000605 send_stream_ = send_stream;
606 }
607
stefanff483612015-12-21 03:14:00 -0800608 void ModifyVideoConfigs(
609 VideoSendStream::Config* send_config,
610 std::vector<VideoReceiveStream::Config>* receive_configs,
611 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000612 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000613 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000614 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700615 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000616 }
617 }
618
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000619 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100620 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700621 test::PrintResultList(
622 "bitrate_stats_",
623 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
624 : "without_min_transmit_bitrate"),
Edward Lemur2f061682017-11-24 13:40:01 +0100625 "bitrate_kbps", bitrate_kbps_list_, "kbps", false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000626 }
627
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000628 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200629 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000630 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200631 const int min_acceptable_bitrate_;
632 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000633 int num_bitrate_observations_in_range_;
Edward Lemur2f061682017-11-24 13:40:01 +0100634 std::vector<double> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000635 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000636
Niels Möller4db138e2018-04-19 09:04:13 +0200637 fake_encoder_max_bitrate_ = kMaxEncodeBitrateKbps;
stefane74eef12016-01-08 06:47:13 -0800638 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000639}
640
Yves Gerey665174f2018-06-19 15:03:05 +0200641TEST_F(CallPerfTest, PadsToMinTransmitBitrate) {
642 TestMinTransmitBitrate(true);
643}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000644
645TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
646 TestMinTransmitBitrate(false);
647}
648
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800649// TODO(bugs.webrtc.org/8878)
650#if defined(WEBRTC_MAC)
651#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
652 DISABLED_KeepsHighBitrateWhenReconfiguringSender
653#else
654#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
655 KeepsHighBitrateWhenReconfiguringSender
656#endif
657TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000658 static const uint32_t kInitialBitrateKbps = 400;
659 static const uint32_t kReconfigureThresholdKbps = 600;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000660
perkjfa10b552016-10-02 23:45:26 -0700661 class VideoStreamFactory
662 : public VideoEncoderConfig::VideoStreamFactoryInterface {
663 public:
664 VideoStreamFactory() {}
665
666 private:
667 std::vector<VideoStream> CreateEncoderStreams(
668 int width,
669 int height,
670 const VideoEncoderConfig& encoder_config) override {
671 std::vector<VideoStream> streams =
672 test::CreateVideoStreams(width, height, encoder_config);
673 streams[0].min_bitrate_bps = 50000;
674 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
675 return streams;
676 }
677 };
678
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000679 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
680 public:
681 BitrateObserver()
682 : EndToEndTest(kDefaultTimeoutMs),
683 FakeEncoder(Clock::GetRealTimeClock()),
Peter Boström5811a392015-12-10 13:02:50 +0100684 time_to_reconfigure_(false, false),
sprang867fb522015-08-03 04:38:41 -0700685 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100686 last_set_bitrate_kbps_(0),
687 send_stream_(nullptr),
Niels Möller4db138e2018-04-19 09:04:13 +0200688 frame_generator_(nullptr),
689 encoder_factory_(this) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000690
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000691 int32_t InitEncode(const VideoCodec* config,
692 int32_t number_of_cores,
693 size_t max_payload_size) override {
perkjfa10b552016-10-02 23:45:26 -0700694 ++encoder_inits_;
695 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700696 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100697 // |expected_bitrate| is affected by bandwidth estimation before the
698 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100699 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
700 ? last_set_bitrate_kbps_
701 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100702 EXPECT_EQ(expected_bitrate, config->startBitrate)
703 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700704 EXPECT_EQ(kDefaultWidth, config->width);
705 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100706 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700707 EXPECT_EQ(2 * kDefaultWidth, config->width);
708 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100709 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
philipel0676f222018-04-17 16:12:21 +0200710 EXPECT_GT(config->startBitrate, kReconfigureThresholdKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000711 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100712 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000713 }
714 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
715 }
716
Erik Språng566124a2018-04-23 12:32:22 +0200717 int32_t SetRateAllocation(const VideoBitrateAllocation& rate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100718 uint32_t framerate) override {
719 last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100720 if (encoder_inits_ == 1 &&
Erik Språng08127a92016-11-16 16:41:30 +0100721 rate_allocation.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100722 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000723 }
Erik Språng08127a92016-11-16 16:41:30 +0100724 return FakeEncoder::SetRateAllocation(rate_allocation, framerate);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000725 }
726
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000727 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000728 Call::Config config = EndToEndTest::GetSenderCallConfig();
philipel4fb651d2017-04-10 03:54:05 -0700729 config.event_log = event_log_.get();
Stefan Holmere5904162015-03-26 11:11:06 +0100730 config.bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000731 return config;
732 }
733
stefanff483612015-12-21 03:14:00 -0800734 void ModifyVideoConfigs(
735 VideoSendStream::Config* send_config,
736 std::vector<VideoReceiveStream::Config>* receive_configs,
737 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200738 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Per21d45d22016-10-30 21:37:57 +0100739 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700740 encoder_config->video_stream_factory =
741 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000742
perkj26091b12016-09-01 01:17:40 -0700743 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000744 }
745
stefanff483612015-12-21 03:14:00 -0800746 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000747 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000748 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000749 send_stream_ = send_stream;
750 }
751
perkjfa10b552016-10-02 23:45:26 -0700752 void OnFrameGeneratorCapturerCreated(
753 test::FrameGeneratorCapturer* frame_generator_capturer) override {
754 frame_generator_ = frame_generator_capturer;
755 }
756
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000757 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100758 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000759 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700760 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700761 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100762 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000763 << "Timed out while waiting for a couple of high bitrate estimates "
764 "after reconfiguring the send stream.";
765 }
766
767 private:
Peter Boström5811a392015-12-10 13:02:50 +0100768 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000769 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100770 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000771 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700772 test::FrameGeneratorCapturer* frame_generator_;
Niels Möller4db138e2018-04-19 09:04:13 +0200773 test::EncoderProxyFactory encoder_factory_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000774 VideoEncoderConfig encoder_config_;
775 } test;
776
stefane74eef12016-01-08 06:47:13 -0800777 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000778}
779
Alex Narestd0e196b2017-11-22 17:22:35 +0100780// Discovers the minimal supported audio+video bitrate. The test bitrate is
781// considered supported if Rtt does not go above 400ms with the network
782// contrained to the test bitrate.
783//
784// |use_bitrate_allocation_strategy| use AudioPriorityBitrateAllocationStrategy
785// |test_bitrate_from test_bitrate_to| bitrate constraint range
786// |test_bitrate_step| bitrate constraint update step during the test
787// |min_bwe max_bwe| BWE range
788// |start_bwe| initial BWE
789void CallPerfTest::TestMinAudioVideoBitrate(
790 bool use_bitrate_allocation_strategy,
791 int test_bitrate_from,
792 int test_bitrate_to,
793 int test_bitrate_step,
794 int min_bwe,
795 int start_bwe,
796 int max_bwe) {
797 static const std::string kAudioTrackId = "audio_track_0";
798 static constexpr uint32_t kSufficientAudioBitrateBps = 16000;
799 static constexpr int kOpusMinBitrateBps = 6000;
800 static constexpr int kOpusBitrateFbBps = 32000;
801 static constexpr int kBitrateStabilizationMs = 10000;
802 static constexpr int kBitrateMeasurements = 10;
803 static constexpr int kBitrateMeasurementMs = 1000;
804 static constexpr int kMinGoodRttMs = 400;
805
806 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
807 public:
808 MinVideoAndAudioBitrateTester(bool use_bitrate_allocation_strategy,
809 int test_bitrate_from,
810 int test_bitrate_to,
811 int test_bitrate_step,
812 int min_bwe,
813 int start_bwe,
814 int max_bwe)
815 : EndToEndTest(),
816 allocation_strategy_(new rtc::AudioPriorityBitrateAllocationStrategy(
817 kAudioTrackId,
818 kSufficientAudioBitrateBps)),
819 use_bitrate_allocation_strategy_(use_bitrate_allocation_strategy),
820 test_bitrate_from_(test_bitrate_from),
821 test_bitrate_to_(test_bitrate_to),
822 test_bitrate_step_(test_bitrate_step),
823 min_bwe_(min_bwe),
824 start_bwe_(start_bwe),
825 max_bwe_(max_bwe) {}
826
827 protected:
828 FakeNetworkPipe::Config GetFakeNetworkPipeConfig() {
829 FakeNetworkPipe::Config pipe_config;
830 pipe_config.link_capacity_kbps = test_bitrate_from_;
831 return pipe_config;
832 }
833
834 test::PacketTransport* CreateSendTransport(
835 test::SingleThreadedTaskQueueForTesting* task_queue,
836 Call* sender_call) override {
837 return send_transport_ = new test::PacketTransport(
838 task_queue, sender_call, this, test::PacketTransport::kSender,
839 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
840 }
841
842 test::PacketTransport* CreateReceiveTransport(
843 test::SingleThreadedTaskQueueForTesting* task_queue) override {
844 return receive_transport_ = new test::PacketTransport(
845 task_queue, nullptr, this, test::PacketTransport::kReceiver,
846 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
847 }
848
849 void PerformTest() override {
850 int last_passed_test_bitrate = -1;
851 for (int test_bitrate = test_bitrate_from_;
852 test_bitrate_from_ < test_bitrate_to_
853 ? test_bitrate <= test_bitrate_to_
854 : test_bitrate >= test_bitrate_to_;
855 test_bitrate += test_bitrate_step_) {
856 FakeNetworkPipe::Config pipe_config;
857 pipe_config.link_capacity_kbps = test_bitrate;
858 send_transport_->SetConfig(pipe_config);
859 receive_transport_->SetConfig(pipe_config);
860
861 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
862 kBitrateStabilizationMs);
863
864 int64_t avg_rtt = 0;
865 for (int i = 0; i < kBitrateMeasurements; i++) {
866 Call::Stats call_stats = sender_call_->GetStats();
867 avg_rtt += call_stats.rtt_ms;
868 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
869 kBitrateMeasurementMs);
870 }
871 avg_rtt = avg_rtt / kBitrateMeasurements;
872 if (avg_rtt > kMinGoodRttMs) {
873 break;
874 } else {
875 last_passed_test_bitrate = test_bitrate;
876 }
877 }
878 EXPECT_GT(last_passed_test_bitrate, -1)
879 << "Minimum supported bitrate out of the test scope";
Edward Lemur7f331fa2018-01-08 17:35:51 +0100880 webrtc::test::PrintResult(
881 "min_test_bitrate_",
882 use_bitrate_allocation_strategy_ ? "with_allocation_strategy"
883 : "no_allocation_strategy",
884 "min_bitrate", last_passed_test_bitrate, "kbps", false);
Alex Narestd0e196b2017-11-22 17:22:35 +0100885 }
886
887 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
888 sender_call_ = sender_call;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100889 BitrateConstraints bitrate_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100890 bitrate_config.min_bitrate_bps = min_bwe_;
891 bitrate_config.start_bitrate_bps = start_bwe_;
892 bitrate_config.max_bitrate_bps = max_bwe_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100893 sender_call->GetTransportControllerSend()->SetSdpBitrateParameters(
894 bitrate_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100895 if (use_bitrate_allocation_strategy_) {
896 sender_call->SetBitrateAllocationStrategy(
897 std::move(allocation_strategy_));
898 }
899 }
900
901 size_t GetNumVideoStreams() const override { return 1; }
902
903 size_t GetNumAudioStreams() const override { return 1; }
904
905 void ModifyAudioConfigs(
906 AudioSendStream::Config* send_config,
907 std::vector<AudioReceiveStream::Config>* receive_configs) override {
908 if (use_bitrate_allocation_strategy_) {
909 send_config->track_id = kAudioTrackId;
910 send_config->min_bitrate_bps = kOpusMinBitrateBps;
911 send_config->max_bitrate_bps = kOpusBitrateFbBps;
912 } else {
913 send_config->send_codec_spec->target_bitrate_bps =
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200914 absl::optional<int>(kOpusBitrateFbBps);
Alex Narestd0e196b2017-11-22 17:22:35 +0100915 }
916 }
917
918 private:
919 std::unique_ptr<rtc::BitrateAllocationStrategy> allocation_strategy_;
920 const bool use_bitrate_allocation_strategy_;
921 const int test_bitrate_from_;
922 const int test_bitrate_to_;
923 const int test_bitrate_step_;
924 const int min_bwe_;
925 const int start_bwe_;
926 const int max_bwe_;
927 test::PacketTransport* send_transport_;
928 test::PacketTransport* receive_transport_;
929 Call* sender_call_;
930 } test(use_bitrate_allocation_strategy, test_bitrate_from, test_bitrate_to,
931 test_bitrate_step, min_bwe, start_bwe, max_bwe);
932
933 RunBaseTest(&test);
934}
935
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800936// TODO(bugs.webrtc.org/8878)
937#if defined(WEBRTC_MAC)
Yves Gerey665174f2018-06-19 15:03:05 +0200938#define MAYBE_MinVideoAndAudioBitrate DISABLED_MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800939#else
Yves Gerey665174f2018-06-19 15:03:05 +0200940#define MAYBE_MinVideoAndAudioBitrate MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800941#endif
942TEST_F(CallPerfTest, MAYBE_MinVideoAndAudioBitrate) {
Alex Narestd0e196b2017-11-22 17:22:35 +0100943 TestMinAudioVideoBitrate(false, 110, 40, -10, 10000, 70000, 200000);
944}
945TEST_F(CallPerfTest, MinVideoAndAudioBitrateWStrategy) {
946 TestMinAudioVideoBitrate(true, 110, 40, -10, 10000, 70000, 200000);
947}
948
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000949} // namespace webrtc