blob: 88ac2e8d3fb031f368c9256d1f6c472ace1451c9 [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,
62 float audio_rtp_speed);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000063
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000064 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
65
wu@webrtc.orgcd701192014-04-24 22:10:24 +000066 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
67 int threshold_ms,
68 int start_time_ms,
69 int run_time_ms);
Alex Narestd0e196b2017-11-22 17:22:35 +010070 void TestMinAudioVideoBitrate(bool use_bitrate_allocation_strategy,
71 int test_bitrate_from,
72 int test_bitrate_to,
73 int test_bitrate_step,
74 int min_bwe,
75 int start_bwe,
76 int max_bwe);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000077};
78
asaperssonf8cdd182016-03-15 01:00:47 -070079class VideoRtcpAndSyncObserver : public test::RtpRtcpObserver,
nisse7ade7b32016-03-23 04:48:10 -070080 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000081 static const int kInSyncThresholdMs = 50;
82 static const int kStartupTimeMs = 2000;
83 static const int kMinRunTimeMs = 30000;
84
85 public:
asaperssonf8cdd182016-03-15 01:00:47 -070086 explicit VideoRtcpAndSyncObserver(Clock* clock)
87 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs),
88 clock_(clock),
pbos@webrtc.org1d096902013-12-13 12:48:05 +000089 creation_time_ms_(clock_->TimeInMilliseconds()),
asaperssonf8cdd182016-03-15 01:00:47 -070090 first_time_in_sync_(-1),
91 receive_stream_(nullptr) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000092
nisseeb83a1a2016-03-21 01:27:56 -070093 void OnFrame(const VideoFrame& video_frame) override {
asaperssonf8cdd182016-03-15 01:00:47 -070094 VideoReceiveStream::Stats stats;
95 {
96 rtc::CritScope lock(&crit_);
97 if (receive_stream_)
98 stats = receive_stream_->GetStats();
99 }
100 if (stats.sync_offset_ms == std::numeric_limits<int>::max())
101 return;
102
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000103 int64_t now_ms = clock_->TimeInMilliseconds();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000104 int64_t time_since_creation = now_ms - creation_time_ms_;
105 // During the first couple of seconds audio and video can falsely be
106 // estimated as being synchronized. We don't want to trigger on those.
107 if (time_since_creation < kStartupTimeMs)
108 return;
asaperssonf8cdd182016-03-15 01:00:47 -0700109 if (std::abs(stats.sync_offset_ms) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000110 if (first_time_in_sync_ == -1) {
111 first_time_in_sync_ = now_ms;
112 webrtc::test::PrintResult("sync_convergence_time",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000113 "",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000114 "synchronization",
115 time_since_creation,
116 "ms",
117 false);
118 }
119 if (time_since_creation > kMinRunTimeMs)
Peter Boström5811a392015-12-10 13:02:50 +0100120 observation_complete_.Set();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000121 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200122 if (first_time_in_sync_ != -1)
123 sync_offset_ms_list_.push_back(stats.sync_offset_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000124 }
125
asaperssonf8cdd182016-03-15 01:00:47 -0700126 void set_receive_stream(VideoReceiveStream* receive_stream) {
127 rtc::CritScope lock(&crit_);
128 receive_stream_ = receive_stream;
129 }
130
danilchap46b89b92016-06-03 09:27:37 -0700131 void PrintResults() {
132 test::PrintResultList("stream_offset", "", "synchronization",
133 test::ValuesToString(sync_offset_ms_list_), "ms",
134 false);
135 }
136
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000137 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000138 Clock* const clock_;
stefanf116bd02015-10-27 08:29:42 -0700139 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000140 int64_t first_time_in_sync_;
asaperssonf8cdd182016-03-15 01:00:47 -0700141 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700142 VideoReceiveStream* receive_stream_ RTC_GUARDED_BY(crit_);
danilchap46b89b92016-06-03 09:27:37 -0700143 std::vector<int> sync_offset_ms_list_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000144};
145
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100146void CallPerfTest::TestAudioVideoSync(FecMode fec,
147 CreateOrder create_first,
danilchap9c6a0c72016-02-10 10:54:47 -0800148 float video_ntp_speed,
149 float video_rtp_speed,
150 float audio_rtp_speed) {
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 rtc::scoped_refptr<AudioProcessing> audio_processing;
163 VoiceEngine* voice_engine;
164 VoEBase* voe_base;
165 std::unique_ptr<FakeAudioDevice> fake_audio_device;
166 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock());
167
minyue20c84cc2017-04-10 16:57:57 -0700168 std::map<uint8_t, MediaType> audio_pt_map;
169 std::map<uint8_t, MediaType> video_pt_map;
minyue20c84cc2017-04-10 16:57:57 -0700170
eladalon413ee9a2017-08-22 04:02:52 -0700171 std::unique_ptr<test::PacketTransport> audio_send_transport;
172 std::unique_ptr<test::PacketTransport> video_send_transport;
173 std::unique_ptr<test::PacketTransport> receive_transport;
mflodman3d7db262016-04-29 00:57:13 -0700174
eladalon413ee9a2017-08-22 04:02:52 -0700175 AudioSendStream* audio_send_stream;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100176 AudioReceiveStream* audio_receive_stream;
eladalon413ee9a2017-08-22 04:02:52 -0700177 std::unique_ptr<DriftingClock> drifting_clock;
pbos8fc7fa72015-07-15 08:02:58 -0700178
eladalon413ee9a2017-08-22 04:02:52 -0700179 task_queue_.SendTask([&]() {
180 metrics::Reset();
181 audio_processing = AudioProcessing::Create();
182 voice_engine = VoiceEngine::Create();
183 voe_base = VoEBase::GetInterface(voice_engine);
184 fake_audio_device = rtc::MakeUnique<FakeAudioDevice>(
185 FakeAudioDevice::CreatePulsedNoiseCapturer(256, 48000),
186 FakeAudioDevice::CreateDiscardRenderer(48000), audio_rtp_speed);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100187 EXPECT_EQ(0, fake_audio_device->Init());
eladalon413ee9a2017-08-22 04:02:52 -0700188 EXPECT_EQ(0, voe_base->Init(fake_audio_device.get(), audio_processing.get(),
Rasmus Brandt31027342017-09-29 13:48:12 +0000189 decoder_factory_));
eladalon413ee9a2017-08-22 04:02:52 -0700190 VoEBase::ChannelConfig config;
191 config.enable_voice_pacing = true;
192 send_channel_id = voe_base->CreateChannel(config);
193 recv_channel_id = voe_base->CreateChannel();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000194
eladalon413ee9a2017-08-22 04:02:52 -0700195 AudioState::Config send_audio_state_config;
196 send_audio_state_config.voice_engine = voice_engine;
197 send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
198 send_audio_state_config.audio_processing = audio_processing;
199 Call::Config sender_config(event_log_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000200
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100201 auto audio_state = AudioState::Create(send_audio_state_config);
202 fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
203 sender_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700204 Call::Config receiver_config(event_log_.get());
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100205 receiver_config.audio_state = audio_state;
eladalon413ee9a2017-08-22 04:02:52 -0700206 CreateCalls(sender_config, receiver_config);
207
208 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
209 std::inserter(audio_pt_map, audio_pt_map.end()),
210 [](const std::pair<const uint8_t, MediaType>& pair) {
211 return pair.second == MediaType::AUDIO;
212 });
213 std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
214 std::inserter(video_pt_map, video_pt_map.end()),
215 [](const std::pair<const uint8_t, MediaType>& pair) {
216 return pair.second == MediaType::VIDEO;
217 });
218
219 audio_send_transport = rtc::MakeUnique<test::PacketTransport>(
220 &task_queue_, sender_call_.get(), &observer,
221 test::PacketTransport::kSender, audio_pt_map, audio_net_config);
222 audio_send_transport->SetReceiver(receiver_call_->Receiver());
223
224 video_send_transport = rtc::MakeUnique<test::PacketTransport>(
225 &task_queue_, sender_call_.get(), &observer,
226 test::PacketTransport::kSender, video_pt_map,
227 FakeNetworkPipe::Config());
228 video_send_transport->SetReceiver(receiver_call_->Receiver());
229
230 receive_transport = rtc::MakeUnique<test::PacketTransport>(
231 &task_queue_, receiver_call_.get(), &observer,
232 test::PacketTransport::kReceiver, payload_type_map_,
233 FakeNetworkPipe::Config());
234 receive_transport->SetReceiver(sender_call_->Receiver());
235
236 CreateSendConfig(1, 0, 0, video_send_transport.get());
237 CreateMatchingReceiveConfigs(receive_transport.get());
238
239 AudioSendStream::Config audio_send_config(audio_send_transport.get());
240 audio_send_config.voe_channel_id = send_channel_id;
241 audio_send_config.rtp.ssrc = kAudioSendSsrc;
242 audio_send_config.send_codec_spec =
243 rtc::Optional<AudioSendStream::Config::SendCodecSpec>(
244 {kAudioSendPayloadType, {"ISAC", 16000, 1}});
245 audio_send_config.encoder_factory = CreateBuiltinAudioEncoderFactory();
246 audio_send_stream = sender_call_->CreateAudioSendStream(audio_send_config);
247
248 video_send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
249 if (fec == FecMode::kOn) {
250 video_send_config_.rtp.ulpfec.red_payload_type = kRedPayloadType;
251 video_send_config_.rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
nisse3b3622f2017-09-26 02:49:21 -0700252 video_receive_configs_[0].rtp.red_payload_type = kRedPayloadType;
253 video_receive_configs_[0].rtp.ulpfec_payload_type = kUlpfecPayloadType;
eladalon413ee9a2017-08-22 04:02:52 -0700254 }
255 video_receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
256 video_receive_configs_[0].renderer = &observer;
257 video_receive_configs_[0].sync_group = kSyncGroup;
258
259 AudioReceiveStream::Config audio_recv_config;
260 audio_recv_config.rtp.remote_ssrc = kAudioSendSsrc;
261 audio_recv_config.rtp.local_ssrc = kAudioRecvSsrc;
262 audio_recv_config.voe_channel_id = recv_channel_id;
263 audio_recv_config.sync_group = kSyncGroup;
Rasmus Brandt31027342017-09-29 13:48:12 +0000264 audio_recv_config.decoder_factory = decoder_factory_;
eladalon413ee9a2017-08-22 04:02:52 -0700265 audio_recv_config.decoder_map = {
266 {kAudioSendPayloadType, {"ISAC", 16000, 1}}};
267
268 if (create_first == CreateOrder::kAudioFirst) {
269 audio_receive_stream =
270 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
271 CreateVideoStreams();
272 } else {
273 CreateVideoStreams();
274 audio_receive_stream =
275 receiver_call_->CreateAudioReceiveStream(audio_recv_config);
276 }
277 EXPECT_EQ(1u, video_receive_streams_.size());
278 observer.set_receive_stream(video_receive_streams_[0]);
279 drifting_clock = rtc::MakeUnique<DriftingClock>(clock_, video_ntp_speed);
280 CreateFrameGeneratorCapturerWithDrift(drifting_clock.get(), video_rtp_speed,
281 kDefaultFramerate, kDefaultWidth,
282 kDefaultHeight);
283
284 Start();
285
286 audio_send_stream->Start();
287 audio_receive_stream->Start();
288 });
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000289
Peter Boström5811a392015-12-10 13:02:50 +0100290 EXPECT_TRUE(observer.Wait())
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000291 << "Timed out while waiting for audio and video to be synchronized.";
292
eladalon413ee9a2017-08-22 04:02:52 -0700293 task_queue_.SendTask([&]() {
294 audio_send_stream->Stop();
295 audio_receive_stream->Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000296
eladalon413ee9a2017-08-22 04:02:52 -0700297 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000298
eladalon413ee9a2017-08-22 04:02:52 -0700299 DestroyStreams();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100300
eladalon413ee9a2017-08-22 04:02:52 -0700301 video_send_transport.reset();
302 audio_send_transport.reset();
303 receive_transport.reset();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100304
eladalon413ee9a2017-08-22 04:02:52 -0700305 sender_call_->DestroyAudioSendStream(audio_send_stream);
306 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000307
eladalon413ee9a2017-08-22 04:02:52 -0700308 voe_base->DeleteChannel(send_channel_id);
309 voe_base->DeleteChannel(recv_channel_id);
310 voe_base->Release();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200311
eladalon413ee9a2017-08-22 04:02:52 -0700312 DestroyCalls();
313
314 VoiceEngine::Delete(voice_engine);
315
316 fake_audio_device.reset();
317 });
asaperssonf8cdd182016-03-15 01:00:47 -0700318
danilchap46b89b92016-06-03 09:27:37 -0700319 observer.PrintResults();
ilnik5328b9e2017-02-21 05:20:28 -0800320
321 // In quick test synchronization may not be achieved in time.
sprange5d3a3e2017-03-01 06:20:56 -0800322 if (!field_trial::IsEnabled("WebRTC-QuickPerfTest")) {
ilnik5328b9e2017-02-21 05:20:28 -0800323 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
324 }
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000325}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000326
danilchapac287ee2016-02-29 12:17:04 -0800327TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoNtpDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100328 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
329 DriftingClock::PercentsFaster(10.0f),
danilchap9c6a0c72016-02-10 10:54:47 -0800330 DriftingClock::kNoDrift, DriftingClock::kNoDrift);
331}
332
danilchap9c6a0c72016-02-10 10:54:47 -0800333TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioFasterThanVideoDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100334 TestAudioVideoSync(FecMode::kOff, CreateOrder::kAudioFirst,
335 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800336 DriftingClock::PercentsSlower(30.0f),
337 DriftingClock::PercentsFaster(30.0f));
338}
339
340TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoFasterThanAudioDrift) {
Danil Chapovalovcde5d6b2016-02-15 11:14:58 +0100341 TestAudioVideoSync(FecMode::kOn, CreateOrder::kVideoFirst,
342 DriftingClock::kNoDrift,
danilchap9c6a0c72016-02-10 10:54:47 -0800343 DriftingClock::PercentsFaster(30.0f),
344 DriftingClock::PercentsSlower(30.0f));
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000345}
346
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000347void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
348 int threshold_ms,
349 int start_time_ms,
350 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000351 class CaptureNtpTimeObserver : public test::EndToEndTest,
nisse7ade7b32016-03-23 04:48:10 -0700352 public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000353 public:
stefane74eef12016-01-08 06:47:13 -0800354 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& net_config,
355 int threshold_ms,
356 int start_time_ms,
357 int run_time_ms)
stefanf116bd02015-10-27 08:29:42 -0700358 : EndToEndTest(kLongTimeoutMs),
stefane74eef12016-01-08 06:47:13 -0800359 net_config_(net_config),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000360 clock_(Clock::GetRealTimeClock()),
361 threshold_ms_(threshold_ms),
362 start_time_ms_(start_time_ms),
363 run_time_ms_(run_time_ms),
364 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000365 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000366 rtp_start_timestamp_set_(false),
367 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000368
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000369 private:
eladalon413ee9a2017-08-22 04:02:52 -0700370 test::PacketTransport* CreateSendTransport(
371 test::SingleThreadedTaskQueueForTesting* task_queue,
372 Call* sender_call) override {
373 return new test::PacketTransport(task_queue, sender_call, this,
minyue20c84cc2017-04-10 16:57:57 -0700374 test::PacketTransport::kSender,
375 payload_type_map_, net_config_);
stefane74eef12016-01-08 06:47:13 -0800376 }
377
eladalon413ee9a2017-08-22 04:02:52 -0700378 test::PacketTransport* CreateReceiveTransport(
379 test::SingleThreadedTaskQueueForTesting* task_queue) override {
380 return new test::PacketTransport(task_queue, nullptr, this,
minyue20c84cc2017-04-10 16:57:57 -0700381 test::PacketTransport::kReceiver,
382 payload_type_map_, net_config_);
Stefan Holmerea8c0f62016-01-13 08:58:38 +0100383 }
384
nisseeb83a1a2016-03-21 01:27:56 -0700385 void OnFrame(const VideoFrame& video_frame) override {
stefanf116bd02015-10-27 08:29:42 -0700386 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000387 if (video_frame.ntp_time_ms() <= 0) {
388 // Haven't got enough RTCP SR in order to calculate the capture ntp
389 // time.
390 return;
391 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000392
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000393 int64_t now_ms = clock_->TimeInMilliseconds();
394 int64_t time_since_creation = now_ms - creation_time_ms_;
395 if (time_since_creation < start_time_ms_) {
396 // Wait for |start_time_ms_| before start measuring.
397 return;
398 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000399
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000400 if (time_since_creation > run_time_ms_) {
Peter Boström5811a392015-12-10 13:02:50 +0100401 observation_complete_.Set();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000402 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000403
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000404 FrameCaptureTimeList::iterator iter =
405 capture_time_list_.find(video_frame.timestamp());
406 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000407
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000408 // The real capture time has been wrapped to uint32_t before converted
409 // to rtp timestamp in the sender side. So here we convert the estimated
410 // capture time to a uint32_t 90k timestamp also for comparing.
411 uint32_t estimated_capture_timestamp =
412 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
413 uint32_t real_capture_timestamp = iter->second;
414 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
415 time_offset_ms = time_offset_ms / 90;
danilchap46b89b92016-06-03 09:27:37 -0700416 time_offset_ms_list_.push_back(time_offset_ms);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000417
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000418 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
419 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000420
nisseef8b61e2016-04-29 06:09:15 -0700421 Action OnSendRtp(const uint8_t* packet, size_t length) override {
stefanf116bd02015-10-27 08:29:42 -0700422 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000423 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000424 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000425
426 if (!rtp_start_timestamp_set_) {
427 // Calculate the rtp timestamp offset in order to calculate the real
428 // capture time.
429 uint32_t first_capture_timestamp =
430 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
431 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
432 rtp_start_timestamp_set_ = true;
433 }
434
435 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
436 capture_time_list_.insert(
437 capture_time_list_.end(),
438 std::make_pair(header.timestamp, capture_timestamp));
439 return SEND_PACKET;
440 }
441
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000442 void OnFrameGeneratorCapturerCreated(
443 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000444 capturer_ = frame_generator_capturer;
445 }
446
stefanff483612015-12-21 03:14:00 -0800447 void ModifyVideoConfigs(
448 VideoSendStream::Config* send_config,
449 std::vector<VideoReceiveStream::Config>* receive_configs,
450 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000451 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000452 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000453 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000454 }
455
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000456 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100457 EXPECT_TRUE(Wait()) << "Timed out while waiting for "
458 "estimated capture NTP time to be "
459 "within bounds.";
danilchap46b89b92016-06-03 09:27:37 -0700460 test::PrintResultList("capture_ntp_time", "", "real - estimated",
461 test::ValuesToString(time_offset_ms_list_), "ms",
462 true);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000463 }
464
stefanf116bd02015-10-27 08:29:42 -0700465 rtc::CriticalSection crit_;
stefane74eef12016-01-08 06:47:13 -0800466 const FakeNetworkPipe::Config net_config_;
stefanf116bd02015-10-27 08:29:42 -0700467 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000468 int threshold_ms_;
469 int start_time_ms_;
470 int run_time_ms_;
471 int64_t creation_time_ms_;
472 test::FrameGeneratorCapturer* capturer_;
473 bool rtp_start_timestamp_set_;
474 uint32_t rtp_start_timestamp_;
475 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
danilchapa37de392017-09-09 04:17:22 -0700476 FrameCaptureTimeList capture_time_list_ RTC_GUARDED_BY(&crit_);
danilchap46b89b92016-06-03 09:27:37 -0700477 std::vector<int> time_offset_ms_list_;
stefane74eef12016-01-08 06:47:13 -0800478 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000479
stefane74eef12016-01-08 06:47:13 -0800480 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000481}
482
Alex Loiko5aea38c2017-09-27 13:10:28 +0200483// Flaky tests, disabled on Mac due to webrtc:8291.
484#if !(defined(WEBRTC_MAC))
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000485TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000486 FakeNetworkPipe::Config net_config;
487 net_config.queue_delay_ms = 100;
488 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
489 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000490 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000491 const int kStartTimeMs = 10000;
492 const int kRunTimeMs = 20000;
493 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
494}
495
wu@webrtc.org0224c202014-05-05 17:42:43 +0000496TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000497 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000498 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000499 net_config.delay_standard_deviation_ms = 10;
500 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
501 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000502 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000503 const int kStartTimeMs = 10000;
504 const int kRunTimeMs = 20000;
505 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
506}
Alex Loiko5aea38c2017-09-27 13:10:28 +0200507#endif
kthelgasonfa5fdce2017-02-27 00:15:31 -0800508
perkj803d97f2016-11-01 11:45:46 -0700509TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
sprangc5d62e22017-04-02 23:53:04 -0700510 // Minimal normal usage at the start, then 30s overuse to allow filter to
511 // settle, and then 80s underuse to allow plenty of time for rampup again.
512 test::ScopedFieldTrials fake_overuse_settings(
513 "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
514
perkj803d97f2016-11-01 11:45:46 -0700515 class LoadObserver : public test::SendTest,
516 public test::FrameGeneratorCapturer::SinkWantsObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000517 public:
sprangc5d62e22017-04-02 23:53:04 -0700518 LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kStart) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000519
perkj803d97f2016-11-01 11:45:46 -0700520 void OnFrameGeneratorCapturerCreated(
521 test::FrameGeneratorCapturer* frame_generator_capturer) override {
522 frame_generator_capturer->SetSinkWantsObserver(this);
kthelgasonfa5fdce2017-02-27 00:15:31 -0800523 // Set a high initial resolution to be sure that we can scale down.
524 frame_generator_capturer->ChangeResolution(1920, 1080);
perkj803d97f2016-11-01 11:45:46 -0700525 }
526
527 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
528 // is called.
sprangc5d62e22017-04-02 23:53:04 -0700529 // TODO(sprang): Add integration test for maintain-framerate mode?
perkj803d97f2016-11-01 11:45:46 -0700530 void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
531 const rtc::VideoSinkWants& wants) override {
532 // First expect CPU overuse. Then expect CPU underuse when the encoder
533 // delay has been decreased.
sprangc5d62e22017-04-02 23:53:04 -0700534 switch (test_phase_) {
535 case TestPhase::kStart:
536 if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
mflodmancc3d4422017-08-03 08:27:51 -0700537 // On adapting down, VideoStreamEncoder::VideoSourceProxy will set
538 // only the max pixel count, leaving the target unset.
sprangc5d62e22017-04-02 23:53:04 -0700539 test_phase_ = TestPhase::kAdaptedDown;
540 } else {
541 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
542 << wants.max_pixel_count << ", target res = "
543 << wants.target_pixel_count.value_or(-1)
544 << ", max fps = " << wants.max_framerate_fps;
545 }
546 break;
547 case TestPhase::kAdaptedDown:
548 // On adapting up, the adaptation counter will again be at zero, and
549 // so all constraints will be reset.
550 if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
551 !wants.target_pixel_count) {
552 test_phase_ = TestPhase::kAdaptedUp;
553 observation_complete_.Set();
554 } else {
555 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
556 << wants.max_pixel_count << ", target res = "
557 << wants.target_pixel_count.value_or(-1)
558 << ", max fps = " << wants.max_framerate_fps;
559 }
560 break;
561 case TestPhase::kAdaptedUp:
562 ADD_FAILURE() << "Got unexpected adaptation request, max res = "
563 << wants.max_pixel_count << ", target res = "
564 << wants.target_pixel_count.value_or(-1)
565 << ", max fps = " << wants.max_framerate_fps;
perkj803d97f2016-11-01 11:45:46 -0700566 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000567 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000568
stefanff483612015-12-21 03:14:00 -0800569 void ModifyVideoConfigs(
570 VideoSendStream::Config* send_config,
571 std::vector<VideoReceiveStream::Config>* receive_configs,
572 VideoEncoderConfig* encoder_config) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000573 }
574
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000575 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100576 EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000577 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000578
sprangc5d62e22017-04-02 23:53:04 -0700579 enum class TestPhase { kStart, kAdaptedDown, kAdaptedUp } test_phase_;
perkj803d97f2016-11-01 11:45:46 -0700580 } test;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000581
stefane74eef12016-01-08 06:47:13 -0800582 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000583}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000584
585void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
586 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000587 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000588 static const int kMinAcceptableTransmitBitrate = 130;
589 static const int kMaxAcceptableTransmitBitrate = 170;
590 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700591 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700592 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000593 public:
594 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000595 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000596 send_stream_(nullptr),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200597 converged_(false),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000598 pad_to_min_bitrate_(using_min_transmit_bitrate),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200599 min_acceptable_bitrate_(using_min_transmit_bitrate
600 ? kMinAcceptableTransmitBitrate
601 : (kMaxEncodeBitrateKbps -
602 kAcceptableBitrateErrorMargin / 2)),
603 max_acceptable_bitrate_(using_min_transmit_bitrate
604 ? kMaxAcceptableTransmitBitrate
605 : (kMaxEncodeBitrateKbps +
606 kAcceptableBitrateErrorMargin / 2)),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000607 num_bitrate_observations_in_range_(0) {}
608
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000609 private:
stefanf116bd02015-10-27 08:29:42 -0700610 // TODO(holmer): Run this with a timer instead of once per packet.
611 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000612 VideoSendStream::Stats stats = send_stream_->GetStats();
613 if (stats.substreams.size() > 0) {
kwibergaf476c72016-11-28 15:21:39 -0800614 RTC_DCHECK_EQ(1, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000615 int bitrate_kbps =
616 stats.substreams.begin()->second.total_bitrate_bps / 1000;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200617 if (bitrate_kbps > min_acceptable_bitrate_ &&
618 bitrate_kbps < max_acceptable_bitrate_) {
619 converged_ = true;
620 ++num_bitrate_observations_in_range_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000621 if (num_bitrate_observations_in_range_ ==
622 kNumBitrateObservationsInRange)
Peter Boström5811a392015-12-10 13:02:50 +0100623 observation_complete_.Set();
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000624 }
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200625 if (converged_)
626 bitrate_kbps_list_.push_back(bitrate_kbps);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000627 }
stefanf116bd02015-10-27 08:29:42 -0700628 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000629 }
630
stefanff483612015-12-21 03:14:00 -0800631 void OnVideoStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000632 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000633 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000634 send_stream_ = send_stream;
635 }
636
stefanff483612015-12-21 03:14:00 -0800637 void ModifyVideoConfigs(
638 VideoSendStream::Config* send_config,
639 std::vector<VideoReceiveStream::Config>* receive_configs,
640 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000641 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000642 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000643 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700644 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000645 }
646 }
647
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000648 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100649 EXPECT_TRUE(Wait()) << "Timeout while waiting for send-bitrate stats.";
danilchap46b89b92016-06-03 09:27:37 -0700650 test::PrintResultList(
651 "bitrate_stats_",
652 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
653 : "without_min_transmit_bitrate"),
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200654 "bitrate_kbps", test::ValuesToString(bitrate_kbps_list_), "kbps",
danilchap46b89b92016-06-03 09:27:37 -0700655 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000656 }
657
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000658 VideoSendStream* send_stream_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200659 bool converged_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000660 const bool pad_to_min_bitrate_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200661 const int min_acceptable_bitrate_;
662 const int max_acceptable_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000663 int num_bitrate_observations_in_range_;
Danil Chapovalov371b43b2016-06-16 09:58:44 +0200664 std::vector<size_t> bitrate_kbps_list_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000665 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000666
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000667 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
stefane74eef12016-01-08 06:47:13 -0800668 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000669}
670
671TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
672
673TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
674 TestMinTransmitBitrate(false);
675}
676
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000677TEST_F(CallPerfTest, KeepsHighBitrateWhenReconfiguringSender) {
678 static const uint32_t kInitialBitrateKbps = 400;
679 static const uint32_t kReconfigureThresholdKbps = 600;
680 static const uint32_t kPermittedReconfiguredBitrateDiffKbps = 100;
681
perkjfa10b552016-10-02 23:45:26 -0700682 class VideoStreamFactory
683 : public VideoEncoderConfig::VideoStreamFactoryInterface {
684 public:
685 VideoStreamFactory() {}
686
687 private:
688 std::vector<VideoStream> CreateEncoderStreams(
689 int width,
690 int height,
691 const VideoEncoderConfig& encoder_config) override {
692 std::vector<VideoStream> streams =
693 test::CreateVideoStreams(width, height, encoder_config);
694 streams[0].min_bitrate_bps = 50000;
695 streams[0].target_bitrate_bps = streams[0].max_bitrate_bps = 2000000;
696 return streams;
697 }
698 };
699
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000700 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
701 public:
702 BitrateObserver()
703 : EndToEndTest(kDefaultTimeoutMs),
704 FakeEncoder(Clock::GetRealTimeClock()),
Peter Boström5811a392015-12-10 13:02:50 +0100705 time_to_reconfigure_(false, false),
sprang867fb522015-08-03 04:38:41 -0700706 encoder_inits_(0),
Erik Språng08127a92016-11-16 16:41:30 +0100707 last_set_bitrate_kbps_(0),
708 send_stream_(nullptr),
709 frame_generator_(nullptr) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000710
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000711 int32_t InitEncode(const VideoCodec* config,
712 int32_t number_of_cores,
713 size_t max_payload_size) override {
perkjfa10b552016-10-02 23:45:26 -0700714 ++encoder_inits_;
715 if (encoder_inits_ == 1) {
emircan05a55b52016-10-28 14:06:29 -0700716 // First time initialization. Frame size is known.
Per21d45d22016-10-30 21:37:57 +0100717 // |expected_bitrate| is affected by bandwidth estimation before the
718 // first frame arrives to the encoder.
Erik Språng08127a92016-11-16 16:41:30 +0100719 uint32_t expected_bitrate = last_set_bitrate_kbps_ > 0
720 ? last_set_bitrate_kbps_
721 : kInitialBitrateKbps;
Per21d45d22016-10-30 21:37:57 +0100722 EXPECT_EQ(expected_bitrate, config->startBitrate)
723 << "Encoder not initialized at expected bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700724 EXPECT_EQ(kDefaultWidth, config->width);
725 EXPECT_EQ(kDefaultHeight, config->height);
Per21d45d22016-10-30 21:37:57 +0100726 } else if (encoder_inits_ == 2) {
perkjfa10b552016-10-02 23:45:26 -0700727 EXPECT_EQ(2 * kDefaultWidth, config->width);
728 EXPECT_EQ(2 * kDefaultHeight, config->height);
Erik Språng08127a92016-11-16 16:41:30 +0100729 EXPECT_GE(last_set_bitrate_kbps_, kReconfigureThresholdKbps);
Stefan Holmerf9b6e5e2017-02-06 17:17:57 +0100730 EXPECT_GT(
731 config->startBitrate,
732 last_set_bitrate_kbps_ - kPermittedReconfiguredBitrateDiffKbps)
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000733 << "Encoder reconfigured with bitrate too far away from last set.";
Peter Boström5811a392015-12-10 13:02:50 +0100734 observation_complete_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000735 }
736 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
737 }
738
Erik Språng08127a92016-11-16 16:41:30 +0100739 int32_t SetRateAllocation(const BitrateAllocation& rate_allocation,
740 uint32_t framerate) override {
741 last_set_bitrate_kbps_ = rate_allocation.get_sum_kbps();
Per21d45d22016-10-30 21:37:57 +0100742 if (encoder_inits_ == 1 &&
Erik Språng08127a92016-11-16 16:41:30 +0100743 rate_allocation.get_sum_kbps() > kReconfigureThresholdKbps) {
Peter Boström5811a392015-12-10 13:02:50 +0100744 time_to_reconfigure_.Set();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000745 }
Erik Språng08127a92016-11-16 16:41:30 +0100746 return FakeEncoder::SetRateAllocation(rate_allocation, framerate);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000747 }
748
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000749 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000750 Call::Config config = EndToEndTest::GetSenderCallConfig();
philipel4fb651d2017-04-10 03:54:05 -0700751 config.event_log = event_log_.get();
Stefan Holmere5904162015-03-26 11:11:06 +0100752 config.bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000753 return config;
754 }
755
stefanff483612015-12-21 03:14:00 -0800756 void ModifyVideoConfigs(
757 VideoSendStream::Config* send_config,
758 std::vector<VideoReceiveStream::Config>* receive_configs,
759 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000760 send_config->encoder_settings.encoder = this;
Per21d45d22016-10-30 21:37:57 +0100761 encoder_config->max_bitrate_bps = 2 * kReconfigureThresholdKbps * 1000;
perkjfa10b552016-10-02 23:45:26 -0700762 encoder_config->video_stream_factory =
763 new rtc::RefCountedObject<VideoStreamFactory>();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000764
perkj26091b12016-09-01 01:17:40 -0700765 encoder_config_ = encoder_config->Copy();
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000766 }
767
stefanff483612015-12-21 03:14:00 -0800768 void OnVideoStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000769 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000770 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000771 send_stream_ = send_stream;
772 }
773
perkjfa10b552016-10-02 23:45:26 -0700774 void OnFrameGeneratorCapturerCreated(
775 test::FrameGeneratorCapturer* frame_generator_capturer) override {
776 frame_generator_ = frame_generator_capturer;
777 }
778
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000779 void PerformTest() override {
Peter Boström5811a392015-12-10 13:02:50 +0100780 ASSERT_TRUE(time_to_reconfigure_.Wait(kDefaultTimeoutMs))
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000781 << "Timed out before receiving an initial high bitrate.";
perkjfa10b552016-10-02 23:45:26 -0700782 frame_generator_->ChangeResolution(kDefaultWidth * 2, kDefaultHeight * 2);
perkj26091b12016-09-01 01:17:40 -0700783 send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
Peter Boström5811a392015-12-10 13:02:50 +0100784 EXPECT_TRUE(Wait())
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000785 << "Timed out while waiting for a couple of high bitrate estimates "
786 "after reconfiguring the send stream.";
787 }
788
789 private:
Peter Boström5811a392015-12-10 13:02:50 +0100790 rtc::Event time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000791 int encoder_inits_;
Erik Språng08127a92016-11-16 16:41:30 +0100792 uint32_t last_set_bitrate_kbps_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000793 VideoSendStream* send_stream_;
perkjfa10b552016-10-02 23:45:26 -0700794 test::FrameGeneratorCapturer* frame_generator_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000795 VideoEncoderConfig encoder_config_;
796 } test;
797
stefane74eef12016-01-08 06:47:13 -0800798 RunBaseTest(&test);
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000799}
800
Alex Narestd0e196b2017-11-22 17:22:35 +0100801// Discovers the minimal supported audio+video bitrate. The test bitrate is
802// considered supported if Rtt does not go above 400ms with the network
803// contrained to the test bitrate.
804//
805// |use_bitrate_allocation_strategy| use AudioPriorityBitrateAllocationStrategy
806// |test_bitrate_from test_bitrate_to| bitrate constraint range
807// |test_bitrate_step| bitrate constraint update step during the test
808// |min_bwe max_bwe| BWE range
809// |start_bwe| initial BWE
810void CallPerfTest::TestMinAudioVideoBitrate(
811 bool use_bitrate_allocation_strategy,
812 int test_bitrate_from,
813 int test_bitrate_to,
814 int test_bitrate_step,
815 int min_bwe,
816 int start_bwe,
817 int max_bwe) {
818 static const std::string kAudioTrackId = "audio_track_0";
819 static constexpr uint32_t kSufficientAudioBitrateBps = 16000;
820 static constexpr int kOpusMinBitrateBps = 6000;
821 static constexpr int kOpusBitrateFbBps = 32000;
822 static constexpr int kBitrateStabilizationMs = 10000;
823 static constexpr int kBitrateMeasurements = 10;
824 static constexpr int kBitrateMeasurementMs = 1000;
825 static constexpr int kMinGoodRttMs = 400;
826
827 class MinVideoAndAudioBitrateTester : public test::EndToEndTest {
828 public:
829 MinVideoAndAudioBitrateTester(bool use_bitrate_allocation_strategy,
830 int test_bitrate_from,
831 int test_bitrate_to,
832 int test_bitrate_step,
833 int min_bwe,
834 int start_bwe,
835 int max_bwe)
836 : EndToEndTest(),
837 allocation_strategy_(new rtc::AudioPriorityBitrateAllocationStrategy(
838 kAudioTrackId,
839 kSufficientAudioBitrateBps)),
840 use_bitrate_allocation_strategy_(use_bitrate_allocation_strategy),
841 test_bitrate_from_(test_bitrate_from),
842 test_bitrate_to_(test_bitrate_to),
843 test_bitrate_step_(test_bitrate_step),
844 min_bwe_(min_bwe),
845 start_bwe_(start_bwe),
846 max_bwe_(max_bwe) {}
847
848 protected:
849 FakeNetworkPipe::Config GetFakeNetworkPipeConfig() {
850 FakeNetworkPipe::Config pipe_config;
851 pipe_config.link_capacity_kbps = test_bitrate_from_;
852 return pipe_config;
853 }
854
855 test::PacketTransport* CreateSendTransport(
856 test::SingleThreadedTaskQueueForTesting* task_queue,
857 Call* sender_call) override {
858 return send_transport_ = new test::PacketTransport(
859 task_queue, sender_call, this, test::PacketTransport::kSender,
860 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
861 }
862
863 test::PacketTransport* CreateReceiveTransport(
864 test::SingleThreadedTaskQueueForTesting* task_queue) override {
865 return receive_transport_ = new test::PacketTransport(
866 task_queue, nullptr, this, test::PacketTransport::kReceiver,
867 test::CallTest::payload_type_map_, GetFakeNetworkPipeConfig());
868 }
869
870 void PerformTest() override {
871 int last_passed_test_bitrate = -1;
872 for (int test_bitrate = test_bitrate_from_;
873 test_bitrate_from_ < test_bitrate_to_
874 ? test_bitrate <= test_bitrate_to_
875 : test_bitrate >= test_bitrate_to_;
876 test_bitrate += test_bitrate_step_) {
877 FakeNetworkPipe::Config pipe_config;
878 pipe_config.link_capacity_kbps = test_bitrate;
879 send_transport_->SetConfig(pipe_config);
880 receive_transport_->SetConfig(pipe_config);
881
882 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
883 kBitrateStabilizationMs);
884
885 int64_t avg_rtt = 0;
886 for (int i = 0; i < kBitrateMeasurements; i++) {
887 Call::Stats call_stats = sender_call_->GetStats();
888 avg_rtt += call_stats.rtt_ms;
889 rtc::ThreadManager::Instance()->CurrentThread()->SleepMs(
890 kBitrateMeasurementMs);
891 }
892 avg_rtt = avg_rtt / kBitrateMeasurements;
893 if (avg_rtt > kMinGoodRttMs) {
894 break;
895 } else {
896 last_passed_test_bitrate = test_bitrate;
897 }
898 }
899 EXPECT_GT(last_passed_test_bitrate, -1)
900 << "Minimum supported bitrate out of the test scope";
901 webrtc::test::PrintResult("min_test_bitrate_",
902 use_bitrate_allocation_strategy_
903 ? "with_allocation_strategy"
904 : "no_allocation_strategy",
905 "", last_passed_test_bitrate, "kbps", false);
906 }
907
908 void OnCallsCreated(Call* sender_call, Call* receiver_call) override {
909 sender_call_ = sender_call;
910 Call::Config::BitrateConfig bitrate_config;
911 bitrate_config.min_bitrate_bps = min_bwe_;
912 bitrate_config.start_bitrate_bps = start_bwe_;
913 bitrate_config.max_bitrate_bps = max_bwe_;
914 sender_call->SetBitrateConfig(bitrate_config);
915 if (use_bitrate_allocation_strategy_) {
916 sender_call->SetBitrateAllocationStrategy(
917 std::move(allocation_strategy_));
918 }
919 }
920
921 size_t GetNumVideoStreams() const override { return 1; }
922
923 size_t GetNumAudioStreams() const override { return 1; }
924
925 void ModifyAudioConfigs(
926 AudioSendStream::Config* send_config,
927 std::vector<AudioReceiveStream::Config>* receive_configs) override {
928 if (use_bitrate_allocation_strategy_) {
929 send_config->track_id = kAudioTrackId;
930 send_config->min_bitrate_bps = kOpusMinBitrateBps;
931 send_config->max_bitrate_bps = kOpusBitrateFbBps;
932 } else {
933 send_config->send_codec_spec->target_bitrate_bps =
934 rtc::Optional<int>(kOpusBitrateFbBps);
935 }
936 }
937
938 private:
939 std::unique_ptr<rtc::BitrateAllocationStrategy> allocation_strategy_;
940 const bool use_bitrate_allocation_strategy_;
941 const int test_bitrate_from_;
942 const int test_bitrate_to_;
943 const int test_bitrate_step_;
944 const int min_bwe_;
945 const int start_bwe_;
946 const int max_bwe_;
947 test::PacketTransport* send_transport_;
948 test::PacketTransport* receive_transport_;
949 Call* sender_call_;
950 } test(use_bitrate_allocation_strategy, test_bitrate_from, test_bitrate_to,
951 test_bitrate_step, min_bwe, start_bwe, max_bwe);
952
953 RunBaseTest(&test);
954}
955
956TEST_F(CallPerfTest, MinVideoAndAudioBitrate) {
957 TestMinAudioVideoBitrate(false, 110, 40, -10, 10000, 70000, 200000);
958}
959TEST_F(CallPerfTest, MinVideoAndAudioBitrateWStrategy) {
960 TestMinAudioVideoBitrate(true, 110, 40, -10, 10000, 70000, 200000);
961}
962
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000963} // namespace webrtc