blob: 00cfada83a876fa9a8fc5f39ea5d2e5fd467ae08 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "logging/rtc_event_log/rtc_event_log.h"
23#include "modules/audio_coding/include/audio_coding_module.h"
Artem Titov3faa8322018-03-07 14:44:00 +010024#include "modules/audio_device/include/test_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_mixer/audio_mixer_impl.h"
26#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Alex Narestd0e196b2017-11-22 17:22:35 +010027#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/thread_annotations.h"
30#include "system_wrappers/include/metrics_default.h"
31#include "test/call_test.h"
32#include "test/direct_transport.h"
33#include "test/drifting_clock.h"
Niels Möller4db138e2018-04-19 09:04:13 +020034#include "test/encoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "test/encoder_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "test/fake_encoder.h"
37#include "test/field_trial.h"
38#include "test/frame_generator.h"
39#include "test/frame_generator_capturer.h"
40#include "test/gtest.h"
41#include "test/rtp_rtcp_observer.h"
42#include "test/single_threaded_task_queue.h"
43#include "test/testsupport/fileutils.h"
44#include "test/testsupport/perf_test.h"
45#include "video/transport_adapter.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000046
danilchap9c6a0c72016-02-10 10:54:47 -080047using webrtc::test::DriftingClock;
danilchap9c6a0c72016-02-10 10:54:47 -080048
pbos@webrtc.org1d096902013-12-13 12:48:05 +000049namespace webrtc {
50
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000051class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000052 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020053 enum class FecMode { kOn, kOff };
54 enum class CreateOrder { kAudioFirst, kVideoFirst };
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +010055 void TestAudioVideoSync(FecMode fec,
56 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -080057 float video_ntp_speed,
58 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +010059 float audio_rtp_speed,
60 const std::string& test_label);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000061
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000062 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
63
Artem Titov46c4e602018-08-17 14:26:54 +020064 void TestCaptureNtpTime(const DefaultNetworkSimulationConfig& net_config,
wu@webrtc.orgcd701192014-04-24 22:10:24 +000065 int threshold_ms,
66 int start_time_ms,
67 int run_time_ms);
Alex Narestd0e196b2017-11-22 17:22:35 +010068 void TestMinAudioVideoBitrate(bool use_bitrate_allocation_strategy,
69 int test_bitrate_from,
70 int test_bitrate_to,
71 int test_bitrate_step,
72 int min_bwe,
73 int start_bwe,
74 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000075};
76
asaperssonf8cdd182016-03-15 01:00:47 -070077class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070078 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000079 static const int kInSyncThresholdMs = 50;
80 static const int kStartupTimeMs = 2000;
81 static const int kMinRunTimeMs = 30000;
82
83 public:
Edward Lemur947f3fe2017-12-28 15:50:33 +010084 explicit VideoRtcpAndSyncObserver(Clock* clock, const std::string& test_label)
asaperssonf8cdd182016-03-15 01:00:47 -070085 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
86 clock_(clock),
Edward Lemur947f3fe2017-12-28 15:50:33 +010087 test_label_(test_label),
pbos@webrtc.org1d096902013-12-13 12:48:05 +000088 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -070089 first_time_in_sync_(-1),
90 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000091
nisseeb83a1a2016-03-21 01:27:56 -070092 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -070093 VideoReceiveStream::Stats stats;
94 {
95 rtc::CritScope lock(&crit_);
96 if (receive_stream_)
97 stats = receive_stream_->GetStats();
98 }
99 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
100 return;
101
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000102 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000103 int64_t time_since_creation = now_ms - creation_time_ms_;
104 // During the first couple of seconds audio and video can falsely be
105 // estimated as being synchronized. We don't want to trigger on those.
106 if (time_since_creation < kStartupTimeMs)
107 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700108 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000109 if (first_time_in_sync_ == -1) {
110 first_time_in_sync_ = now_ms;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100111 webrtc::test::PrintResult("sync_convergence_time", test_label_,
112 "synchronization", time_since_creation, "ms",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000113 false);
114 }
115 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100116 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000117 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200118 if (first_time_in_sync_ != -1)
119 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000120 }
121
asaperssonf8cdd182016-03-15 01:00:47 -0700122 void set_receive_stream(VideoReceiveStream* receive_stream) {
123 rtc::CritScope lock(&crit_);
124 receive_stream_ = receive_stream;
125 }
126
danilchap46b89b92016-06-03 09:27:37 -0700127 void PrintResults() {
Edward Lemur947f3fe2017-12-28 15:50:33 +0100128 test::PrintResultList("stream_offset", test_label_, "synchronization",
Edward Lemur2f061682017-11-24 13:40:01 +0100129 sync_offset_ms_list_, "ms", false);
danilchap46b89b92016-06-03 09:27:37 -0700130 }
131
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000132 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000133 Clock* const clock_;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100134 std::string test_label_;
stefanf116bd02015-10-27 08:29:42 -0700135 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000136 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700137 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700138 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100139 std::vector<double> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000140};
141
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100142void CallPerfTest::TestAudioVideoSync(FecMode fec,
143 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800144 float video_ntp_speed,
145 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +0100146 float audio_rtp_speed,
147 const std::string& test_label) {
pbos8fc7fa72015-07-15 08:02:58 -0700148 const char* kSyncGroup = "av_sync";
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100149 const uint32_t kAudioSendSsrc = 1234;
150 const uint32_t kAudioRecvSsrc = 5678;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000151
Artem Titov46c4e602018-08-17 14:26:54 +0200152 DefaultNetworkSimulationConfig audio_net_config;
mflodman3d7db262016-04-29 00:57:13 -0700153 audio_net_config.queue_delay_ms = 500;
154 audio_net_config.loss_percent = 5;
minyue20c84cc2017-04-10 16:57:57 -0700155
Edward Lemur947f3fe2017-12-28 15:50:33 +0100156 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), test_label);
eladalon413ee9a2017-08-22 04:02:52 -0700157
minyue20c84cc2017-04-10 16:57:57 -0700158 std::map<uint8_t, MediaType> audio_pt_map;
159 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700160
eladalon413ee9a2017-08-22 04:02:52 -0700161 std::unique_ptr<test::PacketTransport> audio_send_transport;
162 std::unique_ptr<test::PacketTransport> video_send_transport;
163 std::unique_ptr<test::PacketTransport> receive_transport;
mflodman3d7db262016-04-29 00:57:13 -0700164
eladalon413ee9a2017-08-22 04:02:52 -0700165 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100166 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700167 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700168
eladalon413ee9a2017-08-22 04:02:52 -0700169 task_queue_.SendTask([&]() {
170 metrics::Reset();
Artem Titov3faa8322018-03-07 14:44:00 +0100171 rtc::scoped_refptr<TestAudioDeviceModule> fake_audio_device =
172 TestAudioDeviceModule::CreateTestAudioDeviceModule(
173 TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000),
174 TestAudioDeviceModule::CreateDiscardRenderer(48000),
175 audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100176 EXPECT_EQ(0, fake_audio_device->Init());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000177
eladalon413ee9a2017-08-22 04:02:52 -0700178 AudioState::Config send_audio_state_config;
eladalon413ee9a2017-08-22 04:02:52 -0700179 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
Ivo Creusen62337e52018-01-09 14:17:33 +0100180 send_audio_state_config.audio_processing =
181 AudioProcessingBuilder().Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100182 send_audio_state_config.audio_device_module = fake_audio_device;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200183 Call::Config sender_config(send_event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000184
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100185 auto audio_state = AudioState::Create(send_audio_state_config);
186 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
187 sender_config.audio_state = audio_state;
Sebastian Jansson8e6602f2018-07-13 10:43:20 +0200188 Call::Config receiver_config(recv_event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100189 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700190 CreateCalls(sender_config, receiver_config);
191
192 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
193 std::inserter(audio_pt_map, audio_pt_map.end()),
194 [](const std::pair<const uint8_t, MediaType>& pair) {
195 return pair.second == MediaType::AUDIO;
196 });
197 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
198 std::inserter(video_pt_map, video_pt_map.end()),
199 [](const std::pair<const uint8_t, MediaType>& pair) {
200 return pair.second == MediaType::VIDEO;
201 });
202
Karl Wiberg918f50c2018-07-05 11:40:33 +0200203 audio_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700204 &task_queue_, sender_call_.get(), &observer,
205 test::PacketTransport::kSender, audio_pt_map, audio_net_config);
206 audio_send_transport->SetReceiver(receiver_call_->Receiver());
207
Karl Wiberg918f50c2018-07-05 11:40:33 +0200208 video_send_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700209 &task_queue_, sender_call_.get(), &observer,
210 test::PacketTransport::kSender, video_pt_map,
Artem Titov46c4e602018-08-17 14:26:54 +0200211 DefaultNetworkSimulationConfig());
eladalon413ee9a2017-08-22 04:02:52 -0700212 video_send_transport->SetReceiver(receiver_call_->Receiver());
213
Karl Wiberg918f50c2018-07-05 11:40:33 +0200214 receive_transport = absl::make_unique<test::PacketTransport>(
eladalon413ee9a2017-08-22 04:02:52 -0700215 &task_queue_, receiver_call_.get(), &observer,
216 test::PacketTransport::kReceiver, payload_type_map_,
Artem Titov46c4e602018-08-17 14:26:54 +0200217 DefaultNetworkSimulationConfig());
eladalon413ee9a2017-08-22 04:02:52 -0700218 receive_transport->SetReceiver(sender_call_->Receiver());
219
220 CreateSendConfig(1, 0, 0, video_send_transport.get());
221 CreateMatchingReceiveConfigs(receive_transport.get());
222
223 AudioSendStream::Config audio_send_config(audio_send_transport.get());
eladalon413ee9a2017-08-22 04:02:52 -0700224 audio_send_config.rtp.ssrc = kAudioSendSsrc;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100225 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
226 kAudioSendPayloadType, {"ISAC", 16000, 1});
eladalon413ee9a2017-08-22 04:02:52 -0700227 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
228 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
229
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200230 GetVideoSendConfig()->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
eladalon413ee9a2017-08-22 04:02:52 -0700231 if (fec == FecMode::kOn) {
Sebastian Janssonf33905d2018-07-13 09:49:00 +0200232 GetVideoSendConfig()->rtp.ulpfec.red_payload_type = kRedPayloadType;
233 GetVideoSendConfig()->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700234 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
235 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700236 }
237 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
238 video_receive_configs_[0].renderer = &observer;
239 video_receive_configs_[0].sync_group = kSyncGroup;
240
241 AudioReceiveStream::Config audio_recv_config;
242 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
243 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
eladalon413ee9a2017-08-22 04:02:52 -0700244 audio_recv_config.sync_group = kSyncGroup;
Niels Möller2784a032018-03-28 14:16:04 +0200245 audio_recv_config.decoder_factory = audio_decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700246 audio_recv_config.decoder_map = {
247 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
248
249 if (create_first == CreateOrder::kAudioFirst) {
250 audio_receive_stream =
251 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
252 CreateVideoStreams();
253 } else {
254 CreateVideoStreams();
255 audio_receive_stream =
256 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
257 }
258 EXPECT_EQ(1u, video_receive_streams_.size());
259 observer.set_receive_stream(video_receive_streams_[0]);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200260 drifting_clock = absl::make_unique<DriftingClock>(clock_, video_ntp_speed);
eladalon413ee9a2017-08-22 04:02:52 -0700261 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
262 kDefaultFramerate, kDefaultWidth,
263 kDefaultHeight);
264
265 Start();
266
267 audio_send_stream->Start();
268 audio_receive_stream->Start();
269 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000270
Peter Boström5811a392015-12-10 13:02:50 +0100271 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000272 << "Timed out while waiting for audio and video to be synchronized.";
273
eladalon413ee9a2017-08-22 04:02:52 -0700274 task_queue_.SendTask([&]() {
275 audio_send_stream->Stop();
276 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000277
eladalon413ee9a2017-08-22 04:02:52 -0700278 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000279
eladalon413ee9a2017-08-22 04:02:52 -0700280 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100281
eladalon413ee9a2017-08-22 04:02:52 -0700282 video_send_transport.reset();
283 audio_send_transport.reset();
284 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100285
eladalon413ee9a2017-08-22 04:02:52 -0700286 sender_call_->DestroyAudioSendStream(audio_send_stream);
287 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000288
eladalon413ee9a2017-08-22 04:02:52 -0700289 DestroyCalls();
eladalon413ee9a2017-08-22 04:02:52 -0700290 });
asaperssonf8cdd182016-03-15 01:00:47 -0700291
danilchap46b89b92016-06-03 09:27:37 -0700292 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800293
294 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800295 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
ilnik5328b9e2017-02-21 05:20:28 -0800296 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
297 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000298}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000299
Niels Möller9a750612018-08-09 11:04:32 +0200300TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithoutClockDrift) {
301 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
302 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
303 DriftingClock::kNoDrift, "_video_no_drift");
304}
305
danilchapac287ee2016-02-29 12:17:04 -0800306TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100307 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
308 DriftingClock::PercentsFaster(10.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100309 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
310 "_video_ntp_drift");
danilchap9c6a0c72016-02-10 10:54:47 -0800311}
312
danilchap9c6a0c72016-02-10 10:54:47 -0800313TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100314 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
315 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800316 DriftingClock::PercentsSlower(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100317 DriftingClock::PercentsFaster(30.0f), "_audio_faster");
danilchap9c6a0c72016-02-10 10:54:47 -0800318}
319
320TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100321 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
322 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800323 DriftingClock::PercentsFaster(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100324 DriftingClock::PercentsSlower(30.0f), "_video_faster");
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000325}
326
Artem Titov46c4e602018-08-17 14:26:54 +0200327void CallPerfTest::TestCaptureNtpTime(
328 const DefaultNetworkSimulationConfig& net_config,
329 int threshold_ms,
330 int start_time_ms,
331 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000332 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700333 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000334 public:
Artem Titov46c4e602018-08-17 14:26:54 +0200335 CaptureNtpTimeObserver(const DefaultNetworkSimulationConfig& net_config,
stefane74eef12016-01-08 06:47:13 -0800336 int threshold_ms,
337 int start_time_ms,
338 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700339 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800340 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000341 clock_(Clock::GetRealTimeClock()),
342 threshold_ms_(threshold_ms),
343 start_time_ms_(start_time_ms),
344 run_time_ms_(run_time_ms),
345 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000346 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000347 rtp_start_timestamp_set_(false),
348 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000349
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000350 private:
eladalon413ee9a2017-08-22 04:02:52 -0700351 test::PacketTransport* CreateSendTransport(
352 test::SingleThreadedTaskQueueForTesting* task_queue,
353 Call* sender_call) override {
354 return new test::PacketTransport(task_queue, sender_call, this,
minyue20c84cc2017-04-10 16:57:57 -0700355 test::PacketTransport::kSender,
356 payload_type_map_, net_config_);
stefane74eef12016-01-08 06:47:13 -0800357 }
358
eladalon413ee9a2017-08-22 04:02:52 -0700359 test::PacketTransport* CreateReceiveTransport(
360 test::SingleThreadedTaskQueueForTesting* task_queue) override {
361 return new test::PacketTransport(task_queue, nullptr, this,
minyue20c84cc2017-04-10 16:57:57 -0700362 test::PacketTransport::kReceiver,
363 payload_type_map_, net_config_);
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100364 }
365
nisseeb83a1a2016-03-21 01:27:56 -0700366 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700367 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000368 if (video_frame.ntp_time_ms() <= 0) {
369 // Haven't got enough RTCP SR in order to calculate the capture ntp
370 // time.
371 return;
372 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000373
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000374 int64_t now_ms = clock_->TimeInMilliseconds();
375 int64_t time_since_creation = now_ms - creation_time_ms_;
376 if (time_since_creation < start_time_ms_) {
377 // Wait for |start_time_ms_| before start measuring.
378 return;
379 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000380
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000381 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100382 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000383 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000384
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000385 FrameCaptureTimeList::iterator iter =
386 capture_time_list_.find(video_frame.timestamp());
387 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000388
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000389 // The real capture time has been wrapped to uint32_t before converted
390 // to rtp timestamp in the sender side. So here we convert the estimated
391 // capture time to a uint32_t 90k timestamp also for comparing.
392 uint32_t estimated_capture_timestamp =
393 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
394 uint32_t real_capture_timestamp = iter->second;
395 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
396 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700397 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000398
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000399 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
400 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000401
nisseef8b61e2016-04-29 06:09:15 -0700402 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700403 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000404 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000405 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000406
407 if (!rtp_start_timestamp_set_) {
408 // Calculate the rtp timestamp offset in order to calculate the real
409 // capture time.
410 uint32_t first_capture_timestamp =
411 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
412 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
413 rtp_start_timestamp_set_ = true;
414 }
415
416 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
417 capture_time_list_.insert(
418 capture_time_list_.end(),
419 std::make_pair(header.timestamp, capture_timestamp));
420 return SEND_PACKET;
421 }
422
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000423 void OnFrameGeneratorCapturerCreated(
424 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000425 capturer_ = frame_generator_capturer;
426 }
427
stefanff483612015-12-21 03:14:00 -0800428 void ModifyVideoConfigs(
429 VideoSendStream::Config* send_config,
430 std::vector<VideoReceiveStream::Config>* receive_configs,
431 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000432 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000433 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000434 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000435 }
436
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000437 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100438 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
439 "estimated capture NTP time to be "
440 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700441 test::PrintResultList("capture_ntp_time", "", "real - estimated",
Edward Lemur2f061682017-11-24 13:40:01 +0100442 time_offset_ms_list_, "ms", true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000443 }
444
stefanf116bd02015-10-27 08:29:42 -0700445 rtc::CriticalSection crit_;
Artem Titov46c4e602018-08-17 14:26:54 +0200446 const DefaultNetworkSimulationConfig net_config_;
stefanf116bd02015-10-27 08:29:42 -0700447 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000448 int threshold_ms_;
449 int start_time_ms_;
450 int run_time_ms_;
451 int64_t creation_time_ms_;
452 test::FrameGeneratorCapturer* capturer_;
453 bool rtp_start_timestamp_set_;
454 uint32_t rtp_start_timestamp_;
455 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700456 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100457 std::vector<double> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800458 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000459
stefane74eef12016-01-08 06:47:13 -0800460 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000461}
462
Alex Loiko5aea38c2017-09-27 13:10:28 +0200463// Flaky tests, disabled on Mac due to webrtc:8291.
464#if !(defined(WEBRTC_MAC))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000465TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
Artem Titov46c4e602018-08-17 14:26:54 +0200466 DefaultNetworkSimulationConfig net_config;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000467 net_config.queue_delay_ms = 100;
468 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
469 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000470 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000471 const int kStartTimeMs = 10000;
472 const int kRunTimeMs = 20000;
473 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
474}
475
wu@webrtc.org0224c202014-05-05 17:42:43 +0000476TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
Artem Titov46c4e602018-08-17 14:26:54 +0200477 DefaultNetworkSimulationConfig net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000478 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000479 net_config.delay_standard_deviation_ms = 10;
480 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
481 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000482 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000483 const int kStartTimeMs = 10000;
484 const int kRunTimeMs = 20000;
485 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
486}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200487#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800488
perkj803d97f2016-11-01 11:45:46 -0700489TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700490 // Minimal normal usage at the start, then 30s overuse to allow filter to
491 // settle, and then 80s underuse to allow plenty of time for rampup again.
492 test::ScopedFieldTrials fake_overuse_settings(
493 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
494
perkj803d97f2016-11-01 11:45:46 -0700495 class LoadObserver : public test::SendTest,
496 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000497 public:
Mirko Bonadei948b7e32018-08-14 07:23:21 +0000498 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kStart) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000499
perkj803d97f2016-11-01 11:45:46 -0700500 void OnFrameGeneratorCapturerCreated(
501 test::FrameGeneratorCapturer* frame_generator_capturer) override {
502 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800503 // Set a high initial resolution to be sure that we can scale down.
504 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700505 }
506
507 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
508 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700509 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700510 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
511 const rtc::VideoSinkWants& wants) override {
Mirko Bonadei948b7e32018-08-14 07:23:21 +0000512 // First expect CPU overuse. Then expect CPU underuse when the encoder
perkj803d97f2016-11-01 11:45:46 -0700513 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700514 switch (test_phase_) {
515 case TestPhase::kStart:
516 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700517 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
518 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700519 test_phase_ = TestPhase::kAdaptedDown;
520 } else {
521 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
522 << wants.max_pixel_count << ", target res = "
523 << wants.target_pixel_count.value_or(-1)
524 << ", max fps = " << wants.max_framerate_fps;
525 }
526 break;
527 case TestPhase::kAdaptedDown:
528 // On adapting up, the adaptation counter will again be at zero, and
529 // so all constraints will be reset.
530 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
531 !wants.target_pixel_count) {
532 test_phase_ = TestPhase::kAdaptedUp;
533 observation_complete_.Set();
534 } else {
535 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
536 << wants.max_pixel_count << ", target res = "
537 << wants.target_pixel_count.value_or(-1)
538 << ", max fps = " << wants.max_framerate_fps;
539 }
540 break;
541 case TestPhase::kAdaptedUp:
542 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
543 << wants.max_pixel_count << ", target res = "
544 << wants.target_pixel_count.value_or(-1)
545 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700546 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000547 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000548
stefanff483612015-12-21 03:14:00 -0800549 void ModifyVideoConfigs(
550 VideoSendStream::Config* send_config,
551 std::vector<VideoReceiveStream::Config>* receive_configs,
Yves Gerey665174f2018-06-19 15:03:05 +0200552 VideoEncoderConfig* encoder_config) override {}
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000553
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000554 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100555 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000556 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000557
Mirko Bonadei948b7e32018-08-14 07:23:21 +0000558 enum class TestPhase { kStart, kAdaptedDown, kAdaptedUp } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700559 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000560
stefane74eef12016-01-08 06:47:13 -0800561 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000562}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000563
564void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
565 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000566 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000567 static const int kMinAcceptableTransmitBitrate = 130;
568 static const int kMaxAcceptableTransmitBitrate = 170;
569 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700570 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700571 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000572 public:
573 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000574 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000575 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200576 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000577 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200578 min_acceptable_bitrate_(using_min_transmit_bitrate
579 ? kMinAcceptableTransmitBitrate
580 : (kMaxEncodeBitrateKbps -
581 kAcceptableBitrateErrorMargin / 2)),
582 max_acceptable_bitrate_(using_min_transmit_bitrate
583 ? kMaxAcceptableTransmitBitrate
584 : (kMaxEncodeBitrateKbps +
585 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000586 num_bitrate_observations_in_range_(0) {}
587
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000588 private:
stefanf116bd02015-10-27 08:29:42 -0700589 // TODO(holmer): Run this with a timer instead of once per packet.
590 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000591 VideoSendStream::Stats stats = send_stream_->GetStats();
592 if (stats.substreams.size() > 0) {
kwibergaf476c72016-11-28 15:21:39 -0800593 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000594 int bitrate_kbps =
595 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200596 if (bitrate_kbps > min_acceptable_bitrate_ &&
597 bitrate_kbps < max_acceptable_bitrate_) {
598 converged_ = true;
599 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000600 if (num_bitrate_observations_in_range_ ==
601 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100602 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000603 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200604 if (converged_)
605 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000606 }
stefanf116bd02015-10-27 08:29:42 -0700607 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000608 }
609
stefanff483612015-12-21 03:14:00 -0800610 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000611 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000612 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000613 send_stream_ = send_stream;
614 }
615
stefanff483612015-12-21 03:14:00 -0800616 void ModifyVideoConfigs(
617 VideoSendStream::Config* send_config,
618 std::vector<VideoReceiveStream::Config>* receive_configs,
619 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000620 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000621 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000622 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700623 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000624 }
625 }
626
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000627 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100628 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700629 test::PrintResultList(
630 "bitrate_stats_",
631 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
632 : "without_min_transmit_bitrate"),
Edward Lemur2f061682017-11-24 13:40:01 +0100633 "bitrate_kbps", bitrate_kbps_list_, "kbps", false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000634 }
635
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000636 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200637 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000638 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200639 const int min_acceptable_bitrate_;
640 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000641 int num_bitrate_observations_in_range_;
Edward Lemur2f061682017-11-24 13:40:01 +0100642 std::vector<double> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000643 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000644
Niels Möller4db138e2018-04-19 09:04:13 +0200645 fake_encoder_max_bitrate_ = kMaxEncodeBitrateKbps;
stefane74eef12016-01-08 06:47:13 -0800646 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000647}
648
Yves Gerey665174f2018-06-19 15:03:05 +0200649TEST_F(CallPerfTest, PadsToMinTransmitBitrate) {
650 TestMinTransmitBitrate(true);
651}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000652
653TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
654 TestMinTransmitBitrate(false);
655}
656
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800657// TODO(bugs.webrtc.org/8878)
658#if defined(WEBRTC_MAC)
659#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
660 DISABLED_KeepsHighBitrateWhenReconfiguringSender
661#else
662#define MAYBE_KeepsHighBitrateWhenReconfiguringSender \
663 KeepsHighBitrateWhenReconfiguringSender
664#endif
665TEST_F(CallPerfTest, MAYBE_KeepsHighBitrateWhenReconfiguringSender) {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000666 static const uint32_t kInitialBitrateKbps = 400;
667 static const uint32_t kReconfigureThresholdKbps = 600;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000668
perkjfa10b552016-10-02 23:45:26 -0700669 class VideoStreamFactory
670 : public VideoEncoderConfig::VideoStreamFactoryInterface {
671 public:
672 VideoStreamFactory() {}
673
674 private:
675 std::vector<VideoStream> CreateEncoderStreams(
676 int width,
677 int height,
678 const VideoEncoderConfig& encoder_config) override {
679 std::vector<VideoStream> streams =
680 test::CreateVideoStreams(width, height, encoder_config);
681 streams[0].min_bitrate_bps = 50000;
682 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
683 return streams;
684 }
685 };
686
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000687 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
688 public:
689 BitrateObserver()
690 : EndToEndTest(kDefaultTimeoutMs),
691 FakeEncoder(Clock::GetRealTimeClock()),
Peter Boström5811a392015-12-10 13:02:50 +0100692 time_to_reconfigure_(false, false),
sprang867fb522015-08-03 04:38:41 -0700693 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100694 last_set_bitrate_kbps_(0),
695 send_stream_(nullptr),
Niels Möller4db138e2018-04-19 09:04:13 +0200696 frame_generator_(nullptr),
697 encoder_factory_(this) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000698
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000699 int32_t InitEncode(const VideoCodec* config,
700 int32_t number_of_cores,
701 size_t max_payload_size) override {
perkjfa10b552016-10-02 23:45:26 -0700702 ++encoder_inits_;
703 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700704 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100705 // |expected_bitrate| is affected by bandwidth estimation before the
706 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100707 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
708 ? last_set_bitrate_kbps_
709 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100710 EXPECT_EQ(expected_bitrate, config->startBitrate)
711 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700712 EXPECT_EQ(kDefaultWidth, config->width);
713 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100714 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700715 EXPECT_EQ(2 * kDefaultWidth, config->width);
716 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100717 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
philipel0676f222018-04-17 16:12:21 +0200718 EXPECT_GT(config->startBitrate, kReconfigureThresholdKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000719 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100720 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000721 }
722 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
723 }
724
Erik Språng566124a2018-04-23 12:32:22 +0200725 int32_t SetRateAllocation(const VideoBitrateAllocation& rate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100726 uint32_t framerate) override {
727 last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100728 if (encoder_inits_ == 1 &&
Erik Språng08127a92016-11-16 16:41:30 +0100729 rate_allocation.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100730 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000731 }
Erik Språng08127a92016-11-16 16:41:30 +0100732 return FakeEncoder::SetRateAllocation(rate_allocation, framerate);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000733 }
734
Sebastian Jansson72582242018-07-13 13:19:42 +0200735 void ModifySenderCallConfig(Call::Config* config) override {
736 config->bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000737 }
738
stefanff483612015-12-21 03:14:00 -0800739 void ModifyVideoConfigs(
740 VideoSendStream::Config* send_config,
741 std::vector<VideoReceiveStream::Config>* receive_configs,
742 VideoEncoderConfig* encoder_config) override {
Niels Möller4db138e2018-04-19 09:04:13 +0200743 send_config->encoder_settings.encoder_factory = &encoder_factory_;
Per21d45d22016-10-30 21:37:57 +0100744 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700745 encoder_config->video_stream_factory =
746 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000747
perkj26091b12016-09-01 01:17:40 -0700748 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000749 }
750
stefanff483612015-12-21 03:14:00 -0800751 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000752 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000753 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000754 send_stream_ = send_stream;
755 }
756
perkjfa10b552016-10-02 23:45:26 -0700757 void OnFrameGeneratorCapturerCreated(
758 test::FrameGeneratorCapturer* frame_generator_capturer) override {
759 frame_generator_ = frame_generator_capturer;
760 }
761
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000762 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100763 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000764 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700765 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700766 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100767 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000768 << "Timed out while waiting for a couple of high bitrate estimates "
769 "after reconfiguring the send stream.";
770 }
771
772 private:
Peter Boström5811a392015-12-10 13:02:50 +0100773 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000774 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100775 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000776 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700777 test::FrameGeneratorCapturer* frame_generator_;
Niels Möller4db138e2018-04-19 09:04:13 +0200778 test::EncoderProxyFactory encoder_factory_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000779 VideoEncoderConfig encoder_config_;
780 } test;
781
stefane74eef12016-01-08 06:47:13 -0800782 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000783}
784
Alex Narestd0e196b2017-11-22 17:22:35 +0100785// Discovers the minimal supported audio+video bitrate. The test bitrate is
786// considered supported if Rtt does not go above 400ms with the network
787// contrained to the test bitrate.
788//
789// |use_bitrate_allocation_strategy| use AudioPriorityBitrateAllocationStrategy
790// |test_bitrate_from test_bitrate_to| bitrate constraint range
791// |test_bitrate_step| bitrate constraint update step during the test
792// |min_bwe max_bwe| BWE range
793// |start_bwe| initial BWE
794void CallPerfTest::TestMinAudioVideoBitrate(
795 bool use_bitrate_allocation_strategy,
796 int test_bitrate_from,
797 int test_bitrate_to,
798 int test_bitrate_step,
799 int min_bwe,
800 int start_bwe,
801 int max_bwe) {
802 static const std::string kAudioTrackId = "audio_track_0";
803 static constexpr uint32_t kSufficientAudioBitrateBps = 16000;
804 static constexpr int kOpusMinBitrateBps = 6000;
805 static constexpr int kOpusBitrateFbBps = 32000;
806 static constexpr int kBitrateStabilizationMs = 10000;
807 static constexpr int kBitrateMeasurements = 10;
808 static constexpr int kBitrateMeasurementMs = 1000;
809 static constexpr int kMinGoodRttMs = 400;
810
811 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
812 public:
813 MinVideoAndAudioBitrateTester(bool use_bitrate_allocation_strategy,
814 int test_bitrate_from,
815 int test_bitrate_to,
816 int test_bitrate_step,
817 int min_bwe,
818 int start_bwe,
819 int max_bwe)
820 : EndToEndTest(),
821 allocation_strategy_(new rtc::AudioPriorityBitrateAllocationStrategy(
822 kAudioTrackId,
823 kSufficientAudioBitrateBps)),
824 use_bitrate_allocation_strategy_(use_bitrate_allocation_strategy),
825 test_bitrate_from_(test_bitrate_from),
826 test_bitrate_to_(test_bitrate_to),
827 test_bitrate_step_(test_bitrate_step),
828 min_bwe_(min_bwe),
829 start_bwe_(start_bwe),
830 max_bwe_(max_bwe) {}
831
832 protected:
Artem Titov46c4e602018-08-17 14:26:54 +0200833 DefaultNetworkSimulationConfig GetFakeNetworkPipeConfig() {
834 DefaultNetworkSimulationConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100835 pipe_config.link_capacity_kbps = test_bitrate_from_;
836 return pipe_config;
837 }
838
839 test::PacketTransport* CreateSendTransport(
840 test::SingleThreadedTaskQueueForTesting* task_queue,
841 Call* sender_call) override {
842 return send_transport_ = new test::PacketTransport(
843 task_queue, sender_call, this, test::PacketTransport::kSender,
844 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
845 }
846
847 test::PacketTransport* CreateReceiveTransport(
848 test::SingleThreadedTaskQueueForTesting* task_queue) override {
849 return receive_transport_ = new test::PacketTransport(
850 task_queue, nullptr, this, test::PacketTransport::kReceiver,
851 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
852 }
853
854 void PerformTest() override {
855 int last_passed_test_bitrate = -1;
856 for (int test_bitrate = test_bitrate_from_;
857 test_bitrate_from_ < test_bitrate_to_
858 ? test_bitrate <= test_bitrate_to_
859 : test_bitrate >= test_bitrate_to_;
860 test_bitrate += test_bitrate_step_) {
Artem Titov46c4e602018-08-17 14:26:54 +0200861 DefaultNetworkSimulationConfig pipe_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100862 pipe_config.link_capacity_kbps = test_bitrate;
863 send_transport_->SetConfig(pipe_config);
864 receive_transport_->SetConfig(pipe_config);
865
866 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
867 kBitrateStabilizationMs);
868
869 int64_t avg_rtt = 0;
870 for (int i = 0; i < kBitrateMeasurements; i++) {
871 Call::Stats call_stats = sender_call_->GetStats();
872 avg_rtt += call_stats.rtt_ms;
873 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
874 kBitrateMeasurementMs);
875 }
876 avg_rtt = avg_rtt / kBitrateMeasurements;
877 if (avg_rtt > kMinGoodRttMs) {
878 break;
879 } else {
880 last_passed_test_bitrate = test_bitrate;
881 }
882 }
883 EXPECT_GT(last_passed_test_bitrate, -1)
884 << "Minimum supported bitrate out of the test scope";
Edward Lemur7f331fa2018-01-08 17:35:51 +0100885 webrtc::test::PrintResult(
886 "min_test_bitrate_",
887 use_bitrate_allocation_strategy_ ? "with_allocation_strategy"
888 : "no_allocation_strategy",
889 "min_bitrate", last_passed_test_bitrate, "kbps", false);
Alex Narestd0e196b2017-11-22 17:22:35 +0100890 }
891
892 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
893 sender_call_ = sender_call;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100894 BitrateConstraints bitrate_config;
Alex Narestd0e196b2017-11-22 17:22:35 +0100895 bitrate_config.min_bitrate_bps = min_bwe_;
896 bitrate_config.start_bitrate_bps = start_bwe_;
897 bitrate_config.max_bitrate_bps = max_bwe_;
Sebastian Jansson8f83b422018-02-21 13:07:13 +0100898 sender_call->GetTransportControllerSend()->SetSdpBitrateParameters(
899 bitrate_config);
Alex Narestd0e196b2017-11-22 17:22:35 +0100900 if (use_bitrate_allocation_strategy_) {
901 sender_call->SetBitrateAllocationStrategy(
902 std::move(allocation_strategy_));
903 }
904 }
905
906 size_t GetNumVideoStreams() const override { return 1; }
907
908 size_t GetNumAudioStreams() const override { return 1; }
909
910 void ModifyAudioConfigs(
911 AudioSendStream::Config* send_config,
912 std::vector<AudioReceiveStream::Config>* receive_configs) override {
913 if (use_bitrate_allocation_strategy_) {
914 send_config->track_id = kAudioTrackId;
915 send_config->min_bitrate_bps = kOpusMinBitrateBps;
916 send_config->max_bitrate_bps = kOpusBitrateFbBps;
917 } else {
918 send_config->send_codec_spec->target_bitrate_bps =
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200919 absl::optional<int>(kOpusBitrateFbBps);
Alex Narestd0e196b2017-11-22 17:22:35 +0100920 }
921 }
922
923 private:
924 std::unique_ptr<rtc::BitrateAllocationStrategy> allocation_strategy_;
925 const bool use_bitrate_allocation_strategy_;
926 const int test_bitrate_from_;
927 const int test_bitrate_to_;
928 const int test_bitrate_step_;
929 const int min_bwe_;
930 const int start_bwe_;
931 const int max_bwe_;
932 test::PacketTransport* send_transport_;
933 test::PacketTransport* receive_transport_;
934 Call* sender_call_;
935 } test(use_bitrate_allocation_strategy, test_bitrate_from, test_bitrate_to,
936 test_bitrate_step, min_bwe, start_bwe, max_bwe);
937
938 RunBaseTest(&test);
939}
940
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800941// TODO(bugs.webrtc.org/8878)
942#if defined(WEBRTC_MAC)
Yves Gerey665174f2018-06-19 15:03:05 +0200943#define MAYBE_MinVideoAndAudioBitrate DISABLED_MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800944#else
Yves Gerey665174f2018-06-19 15:03:05 +0200945#define MAYBE_MinVideoAndAudioBitrate MinVideoAndAudioBitrate
Taylor Brandstetter85904f42018-02-16 10:11:49 -0800946#endif
947TEST_F(CallPerfTest, MAYBE_MinVideoAndAudioBitrate) {
Alex Narestd0e196b2017-11-22 17:22:35 +0100948 TestMinAudioVideoBitrate(false, 110, 40, -10, 10000, 70000, 200000);
949}
950TEST_F(CallPerfTest, MinVideoAndAudioBitrateWStrategy) {
951 TestMinAudioVideoBitrate(true, 110, 40, -10, 10000, 70000, 200000);
952}
953
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000954} // namespace webrtc