blob: f0c781c450a77630cc33ffd796595005e2bbf13d [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/audio_codecs/builtin_audio_encoder_factory.h"
17#include "call/call.h"
18#include "call/video_config.h"
19#include "logging/rtc_event_log/rtc_event_log.h"
20#include "modules/audio_coding/include/audio_coding_module.h"
21#include "modules/audio_mixer/audio_mixer_impl.h"
22#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Alex Narestd0e196b2017-11-22 17:22:35 +010023#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
25#include "rtc_base/ptr_util.h"
26#include "rtc_base/thread_annotations.h"
27#include "system_wrappers/include/metrics_default.h"
28#include "test/call_test.h"
29#include "test/direct_transport.h"
30#include "test/drifting_clock.h"
31#include "test/encoder_settings.h"
32#include "test/fake_audio_device.h"
33#include "test/fake_encoder.h"
34#include "test/field_trial.h"
35#include "test/frame_generator.h"
36#include "test/frame_generator_capturer.h"
37#include "test/gtest.h"
38#include "test/rtp_rtcp_observer.h"
39#include "test/single_threaded_task_queue.h"
40#include "test/testsupport/fileutils.h"
41#include "test/testsupport/perf_test.h"
42#include "video/transport_adapter.h"
43#include "voice_engine/include/voe_base.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000044
danilchap9c6a0c72016-02-10 10:54:47 -080045using webrtc::test::DriftingClock;
46using webrtc::test::FakeAudioDevice;
47
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:
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +010052 enum class FecMode {
53 kOn, kOff
54 };
55 enum class CreateOrder {
56 kAudioFirst, kVideoFirst
57 };
58 void TestAudioVideoSync(FecMode fec,
59 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -080060 float video_ntp_speed,
61 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +010062 float audio_rtp_speed,
63 const std::string& test_label);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000064
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000065 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
66
wu@webrtc.orgcd701192014-04-24 22:10:24 +000067 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
68 int threshold_ms,
69 int start_time_ms,
70 int run_time_ms);
Alex Narestd0e196b2017-11-22 17:22:35 +010071 void TestMinAudioVideoBitrate(bool use_bitrate_allocation_strategy,
72 int test_bitrate_from,
73 int test_bitrate_to,
74 int test_bitrate_step,
75 int min_bwe,
76 int start_bwe,
77 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000078};
79
asaperssonf8cdd182016-03-15 01:00:47 -070080class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070081 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000082 static const int kInSyncThresholdMs = 50;
83 static const int kStartupTimeMs = 2000;
84 static const int kMinRunTimeMs = 30000;
85
86 public:
Edward Lemur947f3fe2017-12-28 15:50:33 +010087 explicit VideoRtcpAndSyncObserver(Clock* clock, const std::string& test_label)
asaperssonf8cdd182016-03-15 01:00:47 -070088 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
89 clock_(clock),
Edward Lemur947f3fe2017-12-28 15:50:33 +010090 test_label_(test_label),
pbos@webrtc.org1d096902013-12-13 12:48:05 +000091 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -070092 first_time_in_sync_(-1),
93 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000094
nisseeb83a1a2016-03-21 01:27:56 -070095 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -070096 VideoReceiveStream::Stats stats;
97 {
98 rtc::CritScope lock(&crit_);
99 if (receive_stream_)
100 stats = receive_stream_->GetStats();
101 }
102 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
103 return;
104
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000105 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000106 int64_t time_since_creation = now_ms - creation_time_ms_;
107 // During the first couple of seconds audio and video can falsely be
108 // estimated as being synchronized. We don't want to trigger on those.
109 if (time_since_creation < kStartupTimeMs)
110 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700111 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000112 if (first_time_in_sync_ == -1) {
113 first_time_in_sync_ = now_ms;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100114 webrtc::test::PrintResult("sync_convergence_time", test_label_,
115 "synchronization", time_since_creation, "ms",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000116 false);
117 }
118 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100119 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000120 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200121 if (first_time_in_sync_ != -1)
122 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000123 }
124
asaperssonf8cdd182016-03-15 01:00:47 -0700125 void set_receive_stream(VideoReceiveStream* receive_stream) {
126 rtc::CritScope lock(&crit_);
127 receive_stream_ = receive_stream;
128 }
129
danilchap46b89b92016-06-03 09:27:37 -0700130 void PrintResults() {
Edward Lemur947f3fe2017-12-28 15:50:33 +0100131 test::PrintResultList("stream_offset", test_label_, "synchronization",
Edward Lemur2f061682017-11-24 13:40:01 +0100132 sync_offset_ms_list_, "ms", false);
danilchap46b89b92016-06-03 09:27:37 -0700133 }
134
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000135 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000136 Clock* const clock_;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100137 std::string test_label_;
stefanf116bd02015-10-27 08:29:42 -0700138 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000139 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700140 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700141 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100142 std::vector<double> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000143};
144
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100145void CallPerfTest::TestAudioVideoSync(FecMode fec,
146 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800147 float video_ntp_speed,
148 float video_rtp_speed,
Edward Lemur947f3fe2017-12-28 15:50:33 +0100149 float audio_rtp_speed,
150 const std::string& test_label) {
pbos8fc7fa72015-07-15 08:02:58 -0700151 const char* kSyncGroup = "av_sync";
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100152 const uint32_t kAudioSendSsrc = 1234;
153 const uint32_t kAudioRecvSsrc = 5678;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000154
eladalon413ee9a2017-08-22 04:02:52 -0700155 int send_channel_id;
156 int recv_channel_id;
asaperssonf8cdd182016-03-15 01:00:47 -0700157
mflodman3d7db262016-04-29 00:57:13 -0700158 FakeNetworkPipe::Config audio_net_config;
159 audio_net_config.queue_delay_ms = 500;
160 audio_net_config.loss_percent = 5;
minyue20c84cc2017-04-10 16:57:57 -0700161
eladalon413ee9a2017-08-22 04:02:52 -0700162 VoiceEngine* voice_engine;
163 VoEBase* voe_base;
Edward Lemur947f3fe2017-12-28 15:50:33 +0100164 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), test_label);
eladalon413ee9a2017-08-22 04:02:52 -0700165
minyue20c84cc2017-04-10 16:57:57 -0700166 std::map<uint8_t, MediaType> audio_pt_map;
167 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700168
eladalon413ee9a2017-08-22 04:02:52 -0700169 std::unique_ptr<test::PacketTransport> audio_send_transport;
170 std::unique_ptr<test::PacketTransport> video_send_transport;
171 std::unique_ptr<test::PacketTransport> receive_transport;
mflodman3d7db262016-04-29 00:57:13 -0700172
eladalon413ee9a2017-08-22 04:02:52 -0700173 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100174 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700175 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700176
eladalon413ee9a2017-08-22 04:02:52 -0700177 task_queue_.SendTask([&]() {
178 metrics::Reset();
eladalon413ee9a2017-08-22 04:02:52 -0700179 voice_engine = VoiceEngine::Create();
180 voe_base = VoEBase::GetInterface(voice_engine);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100181 rtc::scoped_refptr<FakeAudioDevice> fake_audio_device =
182 new rtc::RefCountedObject<FakeAudioDevice>(
183 FakeAudioDevice::CreatePulsedNoiseCapturer(256, 48000),
184 FakeAudioDevice::CreateDiscardRenderer(48000), audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100185 EXPECT_EQ(0, fake_audio_device->Init());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100186 EXPECT_EQ(0, voe_base->Init(fake_audio_device.get(), nullptr,
Rasmus Brandt31027342017-09-29 13:48:12 +0000187 decoder_factory_));
eladalon413ee9a2017-08-22 04:02:52 -0700188 VoEBase::ChannelConfig config;
189 config.enable_voice_pacing = true;
190 send_channel_id = voe_base->CreateChannel(config);
191 recv_channel_id = voe_base->CreateChannel();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000192
eladalon413ee9a2017-08-22 04:02:52 -0700193 AudioState::Config send_audio_state_config;
194 send_audio_state_config.voice_engine = voice_engine;
195 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100196 send_audio_state_config.audio_processing = AudioProcessing::Create();
197 send_audio_state_config.audio_device_module = fake_audio_device;
eladalon413ee9a2017-08-22 04:02:52 -0700198 Call::Config sender_config(event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000199
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100200 auto audio_state = AudioState::Create(send_audio_state_config);
201 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
202 sender_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700203 Call::Config receiver_config(event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100204 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700205 CreateCalls(sender_config, receiver_config);
206
207 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
208 std::inserter(audio_pt_map, audio_pt_map.end()),
209 [](const std::pair<const uint8_t, MediaType>& pair) {
210 return pair.second == MediaType::AUDIO;
211 });
212 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
213 std::inserter(video_pt_map, video_pt_map.end()),
214 [](const std::pair<const uint8_t, MediaType>& pair) {
215 return pair.second == MediaType::VIDEO;
216 });
217
218 audio_send_transport = rtc::MakeUnique<test::PacketTransport>(
219 &task_queue_, sender_call_.get(), &observer,
220 test::PacketTransport::kSender, audio_pt_map, audio_net_config);
221 audio_send_transport->SetReceiver(receiver_call_->Receiver());
222
223 video_send_transport = rtc::MakeUnique<test::PacketTransport>(
224 &task_queue_, sender_call_.get(), &observer,
225 test::PacketTransport::kSender, video_pt_map,
226 FakeNetworkPipe::Config());
227 video_send_transport->SetReceiver(receiver_call_->Receiver());
228
229 receive_transport = rtc::MakeUnique<test::PacketTransport>(
230 &task_queue_, receiver_call_.get(), &observer,
231 test::PacketTransport::kReceiver, payload_type_map_,
232 FakeNetworkPipe::Config());
233 receive_transport->SetReceiver(sender_call_->Receiver());
234
235 CreateSendConfig(1, 0, 0, video_send_transport.get());
236 CreateMatchingReceiveConfigs(receive_transport.get());
237
238 AudioSendStream::Config audio_send_config(audio_send_transport.get());
239 audio_send_config.voe_channel_id = send_channel_id;
240 audio_send_config.rtp.ssrc = kAudioSendSsrc;
241 audio_send_config.send_codec_spec =
242 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
243 {kAudioSendPayloadType, {"ISAC", 16000, 1}});
244 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
245 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
246
247 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
248 if (fec == FecMode::kOn) {
249 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
250 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700251 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
252 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700253 }
254 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
255 video_receive_configs_[0].renderer = &observer;
256 video_receive_configs_[0].sync_group = kSyncGroup;
257
258 AudioReceiveStream::Config audio_recv_config;
259 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
260 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
261 audio_recv_config.voe_channel_id = recv_channel_id;
262 audio_recv_config.sync_group = kSyncGroup;
Rasmus Brandt31027342017-09-29 13:48:12 +0000263 audio_recv_config.decoder_factory = decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700264 audio_recv_config.decoder_map = {
265 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
266
267 if (create_first == CreateOrder::kAudioFirst) {
268 audio_receive_stream =
269 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
270 CreateVideoStreams();
271 } else {
272 CreateVideoStreams();
273 audio_receive_stream =
274 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
275 }
276 EXPECT_EQ(1u, video_receive_streams_.size());
277 observer.set_receive_stream(video_receive_streams_[0]);
278 drifting_clock = rtc::MakeUnique<DriftingClock>(clock_, video_ntp_speed);
279 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
280 kDefaultFramerate, kDefaultWidth,
281 kDefaultHeight);
282
283 Start();
284
285 audio_send_stream->Start();
286 audio_receive_stream->Start();
287 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000288
Peter Boström5811a392015-12-10 13:02:50 +0100289 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000290 << "Timed out while waiting for audio and video to be synchronized.";
291
eladalon413ee9a2017-08-22 04:02:52 -0700292 task_queue_.SendTask([&]() {
293 audio_send_stream->Stop();
294 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000295
eladalon413ee9a2017-08-22 04:02:52 -0700296 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000297
eladalon413ee9a2017-08-22 04:02:52 -0700298 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100299
eladalon413ee9a2017-08-22 04:02:52 -0700300 video_send_transport.reset();
301 audio_send_transport.reset();
302 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100303
eladalon413ee9a2017-08-22 04:02:52 -0700304 sender_call_->DestroyAudioSendStream(audio_send_stream);
305 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000306
eladalon413ee9a2017-08-22 04:02:52 -0700307 voe_base->DeleteChannel(send_channel_id);
308 voe_base->DeleteChannel(recv_channel_id);
309 voe_base->Release();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200310
eladalon413ee9a2017-08-22 04:02:52 -0700311 DestroyCalls();
312
313 VoiceEngine::Delete(voice_engine);
eladalon413ee9a2017-08-22 04:02:52 -0700314 });
asaperssonf8cdd182016-03-15 01:00:47 -0700315
danilchap46b89b92016-06-03 09:27:37 -0700316 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800317
318 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800319 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
ilnik5328b9e2017-02-21 05:20:28 -0800320 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
321 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000322}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000323
danilchapac287ee2016-02-29 12:17:04 -0800324TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100325 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
326 DriftingClock::PercentsFaster(10.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100327 DriftingClock::kNoDrift, DriftingClock::kNoDrift,
328 "_video_ntp_drift");
danilchap9c6a0c72016-02-10 10:54:47 -0800329}
330
danilchap9c6a0c72016-02-10 10:54:47 -0800331TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100332 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
333 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800334 DriftingClock::PercentsSlower(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100335 DriftingClock::PercentsFaster(30.0f), "_audio_faster");
danilchap9c6a0c72016-02-10 10:54:47 -0800336}
337
338TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100339 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
340 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800341 DriftingClock::PercentsFaster(30.0f),
Edward Lemur947f3fe2017-12-28 15:50:33 +0100342 DriftingClock::PercentsSlower(30.0f), "_video_faster");
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000343}
344
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000345void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
346 int threshold_ms,
347 int start_time_ms,
348 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000349 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700350 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000351 public:
stefane74eef12016-01-08 06:47:13 -0800352 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& net_config,
353 int threshold_ms,
354 int start_time_ms,
355 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700356 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800357 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000358 clock_(Clock::GetRealTimeClock()),
359 threshold_ms_(threshold_ms),
360 start_time_ms_(start_time_ms),
361 run_time_ms_(run_time_ms),
362 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000363 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000364 rtp_start_timestamp_set_(false),
365 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000366
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000367 private:
eladalon413ee9a2017-08-22 04:02:52 -0700368 test::PacketTransport* CreateSendTransport(
369 test::SingleThreadedTaskQueueForTesting* task_queue,
370 Call* sender_call) override {
371 return new test::PacketTransport(task_queue, sender_call, this,
minyue20c84cc2017-04-10 16:57:57 -0700372 test::PacketTransport::kSender,
373 payload_type_map_, net_config_);
stefane74eef12016-01-08 06:47:13 -0800374 }
375
eladalon413ee9a2017-08-22 04:02:52 -0700376 test::PacketTransport* CreateReceiveTransport(
377 test::SingleThreadedTaskQueueForTesting* task_queue) override {
378 return new test::PacketTransport(task_queue, nullptr, this,
minyue20c84cc2017-04-10 16:57:57 -0700379 test::PacketTransport::kReceiver,
380 payload_type_map_, net_config_);
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100381 }
382
nisseeb83a1a2016-03-21 01:27:56 -0700383 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700384 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000385 if (video_frame.ntp_time_ms() <= 0) {
386 // Haven't got enough RTCP SR in order to calculate the capture ntp
387 // time.
388 return;
389 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000390
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000391 int64_t now_ms = clock_->TimeInMilliseconds();
392 int64_t time_since_creation = now_ms - creation_time_ms_;
393 if (time_since_creation < start_time_ms_) {
394 // Wait for |start_time_ms_| before start measuring.
395 return;
396 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000397
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000398 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100399 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000400 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000401
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000402 FrameCaptureTimeList::iterator iter =
403 capture_time_list_.find(video_frame.timestamp());
404 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000405
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000406 // The real capture time has been wrapped to uint32_t before converted
407 // to rtp timestamp in the sender side. So here we convert the estimated
408 // capture time to a uint32_t 90k timestamp also for comparing.
409 uint32_t estimated_capture_timestamp =
410 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
411 uint32_t real_capture_timestamp = iter->second;
412 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
413 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700414 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000415
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000416 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
417 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000418
nisseef8b61e2016-04-29 06:09:15 -0700419 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700420 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000421 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000422 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000423
424 if (!rtp_start_timestamp_set_) {
425 // Calculate the rtp timestamp offset in order to calculate the real
426 // capture time.
427 uint32_t first_capture_timestamp =
428 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
429 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
430 rtp_start_timestamp_set_ = true;
431 }
432
433 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
434 capture_time_list_.insert(
435 capture_time_list_.end(),
436 std::make_pair(header.timestamp, capture_timestamp));
437 return SEND_PACKET;
438 }
439
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000440 void OnFrameGeneratorCapturerCreated(
441 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000442 capturer_ = frame_generator_capturer;
443 }
444
stefanff483612015-12-21 03:14:00 -0800445 void ModifyVideoConfigs(
446 VideoSendStream::Config* send_config,
447 std::vector<VideoReceiveStream::Config>* receive_configs,
448 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000449 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000450 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000451 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000452 }
453
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000454 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100455 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
456 "estimated capture NTP time to be "
457 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700458 test::PrintResultList("capture_ntp_time", "", "real - estimated",
Edward Lemur2f061682017-11-24 13:40:01 +0100459 time_offset_ms_list_, "ms", true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000460 }
461
stefanf116bd02015-10-27 08:29:42 -0700462 rtc::CriticalSection crit_;
stefane74eef12016-01-08 06:47:13 -0800463 const FakeNetworkPipe::Config net_config_;
stefanf116bd02015-10-27 08:29:42 -0700464 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000465 int threshold_ms_;
466 int start_time_ms_;
467 int run_time_ms_;
468 int64_t creation_time_ms_;
469 test::FrameGeneratorCapturer* capturer_;
470 bool rtp_start_timestamp_set_;
471 uint32_t rtp_start_timestamp_;
472 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700473 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
Edward Lemur2f061682017-11-24 13:40:01 +0100474 std::vector<double> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800475 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000476
stefane74eef12016-01-08 06:47:13 -0800477 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000478}
479
Alex Loiko5aea38c2017-09-27 13:10:28 +0200480// Flaky tests, disabled on Mac due to webrtc:8291.
481#if !(defined(WEBRTC_MAC))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000482TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000483 FakeNetworkPipe::Config net_config;
484 net_config.queue_delay_ms = 100;
485 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
486 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000487 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000488 const int kStartTimeMs = 10000;
489 const int kRunTimeMs = 20000;
490 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
491}
492
wu@webrtc.org0224c202014-05-05 17:42:43 +0000493TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000494 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000495 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000496 net_config.delay_standard_deviation_ms = 10;
497 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
498 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000499 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000500 const int kStartTimeMs = 10000;
501 const int kRunTimeMs = 20000;
502 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
503}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200504#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800505
perkj803d97f2016-11-01 11:45:46 -0700506TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700507 // Minimal normal usage at the start, then 30s overuse to allow filter to
508 // settle, and then 80s underuse to allow plenty of time for rampup again.
509 test::ScopedFieldTrials fake_overuse_settings(
510 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
511
perkj803d97f2016-11-01 11:45:46 -0700512 class LoadObserver : public test::SendTest,
513 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000514 public:
sprangc5d62e22017-04-02 23:53:04 -0700515 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kStart) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000516
perkj803d97f2016-11-01 11:45:46 -0700517 void OnFrameGeneratorCapturerCreated(
518 test::FrameGeneratorCapturer* frame_generator_capturer) override {
519 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800520 // Set a high initial resolution to be sure that we can scale down.
521 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700522 }
523
524 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
525 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700526 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700527 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
528 const rtc::VideoSinkWants& wants) override {
529 // First expect CPU overuse. Then expect CPU underuse when the encoder
530 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700531 switch (test_phase_) {
532 case TestPhase::kStart:
533 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700534 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
535 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700536 test_phase_ = TestPhase::kAdaptedDown;
537 } else {
538 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
539 << wants.max_pixel_count << ", target res = "
540 << wants.target_pixel_count.value_or(-1)
541 << ", max fps = " << wants.max_framerate_fps;
542 }
543 break;
544 case TestPhase::kAdaptedDown:
545 // On adapting up, the adaptation counter will again be at zero, and
546 // so all constraints will be reset.
547 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
548 !wants.target_pixel_count) {
549 test_phase_ = TestPhase::kAdaptedUp;
550 observation_complete_.Set();
551 } else {
552 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
553 << wants.max_pixel_count << ", target res = "
554 << wants.target_pixel_count.value_or(-1)
555 << ", max fps = " << wants.max_framerate_fps;
556 }
557 break;
558 case TestPhase::kAdaptedUp:
559 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
560 << wants.max_pixel_count << ", target res = "
561 << wants.target_pixel_count.value_or(-1)
562 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700563 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000564 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000565
stefanff483612015-12-21 03:14:00 -0800566 void ModifyVideoConfigs(
567 VideoSendStream::Config* send_config,
568 std::vector<VideoReceiveStream::Config>* receive_configs,
569 VideoEncoderConfig* encoder_config) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000570 }
571
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000572 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100573 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000574 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000575
sprangc5d62e22017-04-02 23:53:04 -0700576 enum class TestPhase { kStart, kAdaptedDown, kAdaptedUp } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700577 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000578
stefane74eef12016-01-08 06:47:13 -0800579 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000580}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000581
582void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
583 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000584 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000585 static const int kMinAcceptableTransmitBitrate = 130;
586 static const int kMaxAcceptableTransmitBitrate = 170;
587 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700588 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700589 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000590 public:
591 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000592 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000593 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200594 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000595 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200596 min_acceptable_bitrate_(using_min_transmit_bitrate
597 ? kMinAcceptableTransmitBitrate
598 : (kMaxEncodeBitrateKbps -
599 kAcceptableBitrateErrorMargin / 2)),
600 max_acceptable_bitrate_(using_min_transmit_bitrate
601 ? kMaxAcceptableTransmitBitrate
602 : (kMaxEncodeBitrateKbps +
603 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000604 num_bitrate_observations_in_range_(0) {}
605
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000606 private:
stefanf116bd02015-10-27 08:29:42 -0700607 // TODO(holmer): Run this with a timer instead of once per packet.
608 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000609 VideoSendStream::Stats stats = send_stream_->GetStats();
610 if (stats.substreams.size() > 0) {
kwibergaf476c72016-11-28 15:21:39 -0800611 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000612 int bitrate_kbps =
613 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200614 if (bitrate_kbps > min_acceptable_bitrate_ &&
615 bitrate_kbps < max_acceptable_bitrate_) {
616 converged_ = true;
617 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000618 if (num_bitrate_observations_in_range_ ==
619 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100620 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000621 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200622 if (converged_)
623 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000624 }
stefanf116bd02015-10-27 08:29:42 -0700625 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000626 }
627
stefanff483612015-12-21 03:14:00 -0800628 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000629 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000630 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000631 send_stream_ = send_stream;
632 }
633
stefanff483612015-12-21 03:14:00 -0800634 void ModifyVideoConfigs(
635 VideoSendStream::Config* send_config,
636 std::vector<VideoReceiveStream::Config>* receive_configs,
637 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000638 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000639 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000640 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700641 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000642 }
643 }
644
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000645 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100646 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700647 test::PrintResultList(
648 "bitrate_stats_",
649 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
650 : "without_min_transmit_bitrate"),
Edward Lemur2f061682017-11-24 13:40:01 +0100651 "bitrate_kbps", bitrate_kbps_list_, "kbps", false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000652 }
653
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000654 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200655 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000656 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200657 const int min_acceptable_bitrate_;
658 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000659 int num_bitrate_observations_in_range_;
Edward Lemur2f061682017-11-24 13:40:01 +0100660 std::vector<double> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000661 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000662
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000663 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
stefane74eef12016-01-08 06:47:13 -0800664 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000665}
666
667TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
668
669TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
670 TestMinTransmitBitrate(false);
671}
672
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000673TEST_F(CallPerfTest, KeepsHighBitrateWhenReconfiguringSender) {
674 static const uint32_t kInitialBitrateKbps = 400;
675 static const uint32_t kReconfigureThresholdKbps = 600;
676 static const uint32_t kPermittedReconfiguredBitrateDiffKbps = 100;
677
perkjfa10b552016-10-02 23:45:26 -0700678 class VideoStreamFactory
679 : public VideoEncoderConfig::VideoStreamFactoryInterface {
680 public:
681 VideoStreamFactory() {}
682
683 private:
684 std::vector<VideoStream> CreateEncoderStreams(
685 int width,
686 int height,
687 const VideoEncoderConfig& encoder_config) override {
688 std::vector<VideoStream> streams =
689 test::CreateVideoStreams(width, height, encoder_config);
690 streams[0].min_bitrate_bps = 50000;
691 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
692 return streams;
693 }
694 };
695
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000696 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
697 public:
698 BitrateObserver()
699 : EndToEndTest(kDefaultTimeoutMs),
700 FakeEncoder(Clock::GetRealTimeClock()),
Peter Boström5811a392015-12-10 13:02:50 +0100701 time_to_reconfigure_(false, false),
sprang867fb522015-08-03 04:38:41 -0700702 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100703 last_set_bitrate_kbps_(0),
704 send_stream_(nullptr),
705 frame_generator_(nullptr) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000706
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000707 int32_t InitEncode(const VideoCodec* config,
708 int32_t number_of_cores,
709 size_t max_payload_size) override {
perkjfa10b552016-10-02 23:45:26 -0700710 ++encoder_inits_;
711 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700712 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100713 // |expected_bitrate| is affected by bandwidth estimation before the
714 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100715 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
716 ? last_set_bitrate_kbps_
717 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100718 EXPECT_EQ(expected_bitrate, config->startBitrate)
719 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700720 EXPECT_EQ(kDefaultWidth, config->width);
721 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100722 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700723 EXPECT_EQ(2 * kDefaultWidth, config->width);
724 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100725 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
Stefan Holmerf9b6e5e2017-02-06 17:17:57 +0100726 EXPECT_GT(
727 config->startBitrate,
728 last_set_bitrate_kbps_ - kPermittedReconfiguredBitrateDiffKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000729 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100730 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000731 }
732 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
733 }
734
Erik Språng08127a92016-11-16 16:41:30 +0100735 int32_t SetRateAllocation(const BitrateAllocation& rate_allocation,
736 uint32_t framerate) override {
737 last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100738 if (encoder_inits_ == 1 &&
Erik Språng08127a92016-11-16 16:41:30 +0100739 rate_allocation.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100740 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000741 }
Erik Språng08127a92016-11-16 16:41:30 +0100742 return FakeEncoder::SetRateAllocation(rate_allocation, framerate);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000743 }
744
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000745 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000746 Call::Config config = EndToEndTest::GetSenderCallConfig();
philipel4fb651d2017-04-10 03:54:05 -0700747 config.event_log = event_log_.get();
Stefan Holmere5904162015-03-26 11:11:06 +0100748 config.bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000749 return config;
750 }
751
stefanff483612015-12-21 03:14:00 -0800752 void ModifyVideoConfigs(
753 VideoSendStream::Config* send_config,
754 std::vector<VideoReceiveStream::Config>* receive_configs,
755 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000756 send_config->encoder_settings.encoder = this;
Per21d45d22016-10-30 21:37:57 +0100757 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700758 encoder_config->video_stream_factory =
759 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000760
perkj26091b12016-09-01 01:17:40 -0700761 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000762 }
763
stefanff483612015-12-21 03:14:00 -0800764 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000765 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000766 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000767 send_stream_ = send_stream;
768 }
769
perkjfa10b552016-10-02 23:45:26 -0700770 void OnFrameGeneratorCapturerCreated(
771 test::FrameGeneratorCapturer* frame_generator_capturer) override {
772 frame_generator_ = frame_generator_capturer;
773 }
774
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000775 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100776 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000777 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700778 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700779 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100780 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000781 << "Timed out while waiting for a couple of high bitrate estimates "
782 "after reconfiguring the send stream.";
783 }
784
785 private:
Peter Boström5811a392015-12-10 13:02:50 +0100786 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000787 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100788 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000789 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700790 test::FrameGeneratorCapturer* frame_generator_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000791 VideoEncoderConfig encoder_config_;
792 } test;
793
stefane74eef12016-01-08 06:47:13 -0800794 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000795}
796
Alex Narestd0e196b2017-11-22 17:22:35 +0100797// Discovers the minimal supported audio+video bitrate. The test bitrate is
798// considered supported if Rtt does not go above 400ms with the network
799// contrained to the test bitrate.
800//
801// |use_bitrate_allocation_strategy| use AudioPriorityBitrateAllocationStrategy
802// |test_bitrate_from test_bitrate_to| bitrate constraint range
803// |test_bitrate_step| bitrate constraint update step during the test
804// |min_bwe max_bwe| BWE range
805// |start_bwe| initial BWE
806void CallPerfTest::TestMinAudioVideoBitrate(
807 bool use_bitrate_allocation_strategy,
808 int test_bitrate_from,
809 int test_bitrate_to,
810 int test_bitrate_step,
811 int min_bwe,
812 int start_bwe,
813 int max_bwe) {
814 static const std::string kAudioTrackId = "audio_track_0";
815 static constexpr uint32_t kSufficientAudioBitrateBps = 16000;
816 static constexpr int kOpusMinBitrateBps = 6000;
817 static constexpr int kOpusBitrateFbBps = 32000;
818 static constexpr int kBitrateStabilizationMs = 10000;
819 static constexpr int kBitrateMeasurements = 10;
820 static constexpr int kBitrateMeasurementMs = 1000;
821 static constexpr int kMinGoodRttMs = 400;
822
823 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
824 public:
825 MinVideoAndAudioBitrateTester(bool use_bitrate_allocation_strategy,
826 int test_bitrate_from,
827 int test_bitrate_to,
828 int test_bitrate_step,
829 int min_bwe,
830 int start_bwe,
831 int max_bwe)
832 : EndToEndTest(),
833 allocation_strategy_(new rtc::AudioPriorityBitrateAllocationStrategy(
834 kAudioTrackId,
835 kSufficientAudioBitrateBps)),
836 use_bitrate_allocation_strategy_(use_bitrate_allocation_strategy),
837 test_bitrate_from_(test_bitrate_from),
838 test_bitrate_to_(test_bitrate_to),
839 test_bitrate_step_(test_bitrate_step),
840 min_bwe_(min_bwe),
841 start_bwe_(start_bwe),
842 max_bwe_(max_bwe) {}
843
844 protected:
845 FakeNetworkPipe::Config GetFakeNetworkPipeConfig() {
846 FakeNetworkPipe::Config pipe_config;
847 pipe_config.link_capacity_kbps = test_bitrate_from_;
848 return pipe_config;
849 }
850
851 test::PacketTransport* CreateSendTransport(
852 test::SingleThreadedTaskQueueForTesting* task_queue,
853 Call* sender_call) override {
854 return send_transport_ = new test::PacketTransport(
855 task_queue, sender_call, this, test::PacketTransport::kSender,
856 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
857 }
858
859 test::PacketTransport* CreateReceiveTransport(
860 test::SingleThreadedTaskQueueForTesting* task_queue) override {
861 return receive_transport_ = new test::PacketTransport(
862 task_queue, nullptr, this, test::PacketTransport::kReceiver,
863 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
864 }
865
866 void PerformTest() override {
867 int last_passed_test_bitrate = -1;
868 for (int test_bitrate = test_bitrate_from_;
869 test_bitrate_from_ < test_bitrate_to_
870 ? test_bitrate <= test_bitrate_to_
871 : test_bitrate >= test_bitrate_to_;
872 test_bitrate += test_bitrate_step_) {
873 FakeNetworkPipe::Config pipe_config;
874 pipe_config.link_capacity_kbps = test_bitrate;
875 send_transport_->SetConfig(pipe_config);
876 receive_transport_->SetConfig(pipe_config);
877
878 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
879 kBitrateStabilizationMs);
880
881 int64_t avg_rtt = 0;
882 for (int i = 0; i < kBitrateMeasurements; i++) {
883 Call::Stats call_stats = sender_call_->GetStats();
884 avg_rtt += call_stats.rtt_ms;
885 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
886 kBitrateMeasurementMs);
887 }
888 avg_rtt = avg_rtt / kBitrateMeasurements;
889 if (avg_rtt > kMinGoodRttMs) {
890 break;
891 } else {
892 last_passed_test_bitrate = test_bitrate;
893 }
894 }
895 EXPECT_GT(last_passed_test_bitrate, -1)
896 << "Minimum supported bitrate out of the test scope";
897 webrtc::test::PrintResult("min_test_bitrate_",
898 use_bitrate_allocation_strategy_
899 ? "with_allocation_strategy"
900 : "no_allocation_strategy",
901 "", last_passed_test_bitrate, "kbps", false);
902 }
903
904 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
905 sender_call_ = sender_call;
906 Call::Config::BitrateConfig bitrate_config;
907 bitrate_config.min_bitrate_bps = min_bwe_;
908 bitrate_config.start_bitrate_bps = start_bwe_;
909 bitrate_config.max_bitrate_bps = max_bwe_;
910 sender_call->SetBitrateConfig(bitrate_config);
911 if (use_bitrate_allocation_strategy_) {
912 sender_call->SetBitrateAllocationStrategy(
913 std::move(allocation_strategy_));
914 }
915 }
916
917 size_t GetNumVideoStreams() const override { return 1; }
918
919 size_t GetNumAudioStreams() const override { return 1; }
920
921 void ModifyAudioConfigs(
922 AudioSendStream::Config* send_config,
923 std::vector<AudioReceiveStream::Config>* receive_configs) override {
924 if (use_bitrate_allocation_strategy_) {
925 send_config->track_id = kAudioTrackId;
926 send_config->min_bitrate_bps = kOpusMinBitrateBps;
927 send_config->max_bitrate_bps = kOpusBitrateFbBps;
928 } else {
929 send_config->send_codec_spec->target_bitrate_bps =
930 rtc::Optional<int>(kOpusBitrateFbBps);
931 }
932 }
933
934 private:
935 std::unique_ptr<rtc::BitrateAllocationStrategy> allocation_strategy_;
936 const bool use_bitrate_allocation_strategy_;
937 const int test_bitrate_from_;
938 const int test_bitrate_to_;
939 const int test_bitrate_step_;
940 const int min_bwe_;
941 const int start_bwe_;
942 const int max_bwe_;
943 test::PacketTransport* send_transport_;
944 test::PacketTransport* receive_transport_;
945 Call* sender_call_;
946 } test(use_bitrate_allocation_strategy, test_bitrate_from, test_bitrate_to,
947 test_bitrate_step, min_bwe, start_bwe, max_bwe);
948
949 RunBaseTest(&test);
950}
951
952TEST_F(CallPerfTest, MinVideoAndAudioBitrate) {
953 TestMinAudioVideoBitrate(false, 110, 40, -10, 10000, 70000, 200000);
954}
955TEST_F(CallPerfTest, MinVideoAndAudioBitrateWStrategy) {
956 TestMinAudioVideoBitrate(true, 110, 40, -10, 10000, 70000, 200000);
957}
958
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000959} // namespace webrtc