blob: cf65ea0f859758c1fdccc273507815ddda349f97 [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 */
pbos@webrtc.org1d096902013-12-13 12:48:05 +000010#include <algorithm>
11#include <sstream>
12#include <string>
13
14#include "testing/gtest/include/gtest/gtest.h"
15
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +000016#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000018#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000019#include "webrtc/call.h"
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +000020#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
22#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
23#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000024#include "webrtc/system_wrappers/interface/rtp_to_ntp.h"
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000025#include "webrtc/test/call_test.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000026#include "webrtc/test/direct_transport.h"
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000027#include "webrtc/test/encoder_settings.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000028#include "webrtc/test/fake_audio_device.h"
29#include "webrtc/test/fake_decoder.h"
30#include "webrtc/test/fake_encoder.h"
31#include "webrtc/test/frame_generator.h"
32#include "webrtc/test/frame_generator_capturer.h"
33#include "webrtc/test/rtp_rtcp_observer.h"
34#include "webrtc/test/testsupport/fileutils.h"
35#include "webrtc/test/testsupport/perf_test.h"
36#include "webrtc/video/transport_adapter.h"
37#include "webrtc/voice_engine/include/voe_base.h"
38#include "webrtc/voice_engine/include/voe_codec.h"
39#include "webrtc/voice_engine/include/voe_network.h"
40#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
41#include "webrtc/voice_engine/include/voe_video_sync.h"
42
43namespace webrtc {
44
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000045class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000046 protected:
pbos8fc7fa72015-07-15 08:02:58 -070047 void TestAudioVideoSync(bool fec, bool create_audio_first);
stefan@webrtc.org01581da2014-09-04 06:48:14 +000048
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000049 void TestCpuOveruse(LoadObserver::Load tested_load, int encode_delay_ms);
50
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000051 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
52
wu@webrtc.orgcd701192014-04-24 22:10:24 +000053 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
54 int threshold_ms,
55 int start_time_ms,
56 int run_time_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000057};
58
59class SyncRtcpObserver : public test::RtpRtcpObserver {
60 public:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000061 explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config)
Peter Boströmf2f82832015-05-01 13:00:41 +020062 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000063
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000065 RTCPUtility::RTCPParserV2 parser(packet, length, true);
66 EXPECT_TRUE(parser.IsValid());
67
68 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
Erik Språng242e22b2015-05-11 10:17:43 +020069 packet_type != RTCPUtility::RTCPPacketTypes::kInvalid;
pbos@webrtc.org1d096902013-12-13 12:48:05 +000070 packet_type = parser.Iterate()) {
Erik Språng242e22b2015-05-11 10:17:43 +020071 if (packet_type == RTCPUtility::RTCPPacketTypes::kSr) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000072 const RTCPUtility::RTCPPacket& packet = parser.Packet();
wu@webrtc.org66773a02014-05-07 17:09:44 +000073 RtcpMeasurement ntp_rtp_pair(
pbos@webrtc.org1d096902013-12-13 12:48:05 +000074 packet.SR.NTPMostSignificant,
75 packet.SR.NTPLeastSignificant,
76 packet.SR.RTPTimestamp);
77 StoreNtpRtpPair(ntp_rtp_pair);
78 }
79 }
80 return SEND_PACKET;
81 }
82
83 int64_t RtpTimestampToNtp(uint32_t timestamp) const {
Peter Boströmf2f82832015-05-01 13:00:41 +020084 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000085 int64_t timestamp_in_ms = -1;
86 if (ntp_rtp_pairs_.size() == 2) {
87 // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
88 // RTCP sender where it sends RTCP SR before any RTP packets, which leads
89 // to a bogus NTP/RTP mapping.
wu@webrtc.org66773a02014-05-07 17:09:44 +000090 RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000091 return timestamp_in_ms;
92 }
93 return -1;
94 }
95
96 private:
wu@webrtc.org66773a02014-05-07 17:09:44 +000097 void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
Peter Boströmf2f82832015-05-01 13:00:41 +020098 rtc::CritScope lock(&crit_);
wu@webrtc.org66773a02014-05-07 17:09:44 +000099 for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000100 it != ntp_rtp_pairs_.end();
101 ++it) {
102 if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
103 ntp_rtp_pair.ntp_frac == it->ntp_frac) {
104 // This RTCP has already been added to the list.
105 return;
106 }
107 }
108 // We need two RTCP SR reports to map between RTP and NTP. More than two
109 // will not improve the mapping.
110 if (ntp_rtp_pairs_.size() == 2) {
111 ntp_rtp_pairs_.pop_back();
112 }
113 ntp_rtp_pairs_.push_front(ntp_rtp_pair);
114 }
115
Peter Boströmf2f82832015-05-01 13:00:41 +0200116 mutable rtc::CriticalSection crit_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000117 RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000118};
119
120class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
121 static const int kInSyncThresholdMs = 50;
122 static const int kStartupTimeMs = 2000;
123 static const int kMinRunTimeMs = 30000;
124
125 public:
126 VideoRtcpAndSyncObserver(Clock* clock,
127 int voe_channel,
128 VoEVideoSync* voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000129 SyncRtcpObserver* audio_observer)
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000130 : SyncRtcpObserver(FakeNetworkPipe::Config()),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000131 clock_(clock),
132 voe_channel_(voe_channel),
133 voe_sync_(voe_sync),
134 audio_observer_(audio_observer),
135 creation_time_ms_(clock_->TimeInMilliseconds()),
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000136 first_time_in_sync_(-1) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000137
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700138 void RenderFrame(const VideoFrame& video_frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000139 int time_to_render_ms) override {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000140 int64_t now_ms = clock_->TimeInMilliseconds();
141 uint32_t playout_timestamp = 0;
142 if (voe_sync_->GetPlayoutTimestamp(voe_channel_, playout_timestamp) != 0)
143 return;
144 int64_t latest_audio_ntp =
145 audio_observer_->RtpTimestampToNtp(playout_timestamp);
146 int64_t latest_video_ntp = RtpTimestampToNtp(video_frame.timestamp());
147 if (latest_audio_ntp < 0 || latest_video_ntp < 0)
148 return;
149 int time_until_render_ms =
150 std::max(0, static_cast<int>(video_frame.render_time_ms() - now_ms));
151 latest_video_ntp += time_until_render_ms;
152 int64_t stream_offset = latest_audio_ntp - latest_video_ntp;
153 std::stringstream ss;
154 ss << stream_offset;
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000155 webrtc::test::PrintResult("stream_offset",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000156 "",
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000157 "synchronization",
158 ss.str(),
159 "ms",
160 false);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000161 int64_t time_since_creation = now_ms - creation_time_ms_;
162 // During the first couple of seconds audio and video can falsely be
163 // estimated as being synchronized. We don't want to trigger on those.
164 if (time_since_creation < kStartupTimeMs)
165 return;
pbos@webrtc.orgb5f30292014-03-13 08:53:39 +0000166 if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000167 if (first_time_in_sync_ == -1) {
168 first_time_in_sync_ = now_ms;
169 webrtc::test::PrintResult("sync_convergence_time",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000170 "",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000171 "synchronization",
172 time_since_creation,
173 "ms",
174 false);
175 }
176 if (time_since_creation > kMinRunTimeMs)
177 observation_complete_->Set();
178 }
179 }
180
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000181 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000182
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000183 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000184 Clock* const clock_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000185 int voe_channel_;
186 VoEVideoSync* voe_sync_;
187 SyncRtcpObserver* audio_observer_;
188 int64_t creation_time_ms_;
189 int64_t first_time_in_sync_;
190};
191
pbos8fc7fa72015-07-15 08:02:58 -0700192void CallPerfTest::TestAudioVideoSync(bool fec, bool create_audio_first) {
193 const char* kSyncGroup = "av_sync";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000194 class AudioPacketReceiver : public PacketReceiver {
195 public:
196 AudioPacketReceiver(int channel, VoENetwork* voe_network)
197 : channel_(channel),
198 voe_network_(voe_network),
199 parser_(RtpHeaderParser::Create()) {}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200200 DeliveryStatus DeliverPacket(MediaType media_type, const uint8_t* packet,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000201 size_t length) override {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200202 EXPECT_TRUE(media_type == MediaType::ANY ||
203 media_type == MediaType::AUDIO);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000204 int ret;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000205 if (parser_->IsRtcp(packet, length)) {
206 ret = voe_network_->ReceivedRTCPPacket(channel_, packet, length);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000207 } else {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000208 ret = voe_network_->ReceivedRTPPacket(channel_, packet, length,
209 PacketTime());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000210 }
211 return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
212 }
213
214 private:
215 int channel_;
216 VoENetwork* voe_network_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000217 rtc::scoped_ptr<RtpHeaderParser> parser_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000218 };
219
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000220 VoiceEngine* voice_engine = VoiceEngine::Create();
221 VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
222 VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
223 VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine);
224 VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine);
225 const std::string audio_filename =
226 test::ResourcePath("voice_engine/audio_long16", "pcm");
227 ASSERT_STRNE("", audio_filename.c_str());
228 test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(),
229 audio_filename);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000230 EXPECT_EQ(0, voe_base->Init(&fake_audio_device, nullptr));
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000231 int channel = voe_base->CreateChannel();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000232
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000233 FakeNetworkPipe::Config net_config;
234 net_config.queue_delay_ms = 500;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000235 net_config.loss_percent = 5;
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000236 SyncRtcpObserver audio_observer(net_config);
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000237 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
238 channel,
239 voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000240 &audio_observer);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000241
242 Call::Config receiver_config(observer.ReceiveTransport());
243 receiver_config.voice_engine = voice_engine;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000244 CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
245
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000246 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
247 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
248
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000249 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000250 audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
251
252 internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000253 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000254 EXPECT_EQ(0,
255 voe_network->RegisterExternalTransport(channel, transport_adapter));
256
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000257 observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000258
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000259 test::FakeDecoder fake_decoder;
260
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000261 CreateSendConfig(1);
262 CreateMatchingReceiveConfigs();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000263
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000264 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
265 if (fec) {
266 send_config_.rtp.fec.red_payload_type = kRedPayloadType;
267 send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
268 receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
269 receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
270 }
271 receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000272 receive_configs_[0].renderer = &observer;
pbos8fc7fa72015-07-15 08:02:58 -0700273 receive_configs_[0].sync_group = kSyncGroup;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000274
pbos8fc7fa72015-07-15 08:02:58 -0700275 AudioReceiveStream::Config audio_config;
276 audio_config.voe_channel_id = channel;
277 audio_config.sync_group = kSyncGroup;
278
279 AudioReceiveStream* audio_receive_stream = nullptr;
280
281 if (create_audio_first) {
282 audio_receive_stream =
283 receiver_call_->CreateAudioReceiveStream(audio_config);
284 CreateStreams();
285 } else {
286 CreateStreams();
287 audio_receive_stream =
288 receiver_call_->CreateAudioReceiveStream(audio_config);
289 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000290
291 CreateFrameGeneratorCapturer();
292
293 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000294
295 fake_audio_device.Start();
296 EXPECT_EQ(0, voe_base->StartPlayout(channel));
297 EXPECT_EQ(0, voe_base->StartReceive(channel));
298 EXPECT_EQ(0, voe_base->StartSend(channel));
299
300 EXPECT_EQ(kEventSignaled, observer.Wait())
301 << "Timed out while waiting for audio and video to be synchronized.";
302
303 EXPECT_EQ(0, voe_base->StopSend(channel));
304 EXPECT_EQ(0, voe_base->StopReceive(channel));
305 EXPECT_EQ(0, voe_base->StopPlayout(channel));
306 fake_audio_device.Stop();
307
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000308 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000309 observer.StopSending();
310 audio_observer.StopSending();
311
312 voe_base->DeleteChannel(channel);
313 voe_base->Release();
314 voe_codec->Release();
315 voe_network->Release();
316 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000317
318 DestroyStreams();
319
pbos8fc7fa72015-07-15 08:02:58 -0700320 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
321
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000322 VoiceEngine::Delete(voice_engine);
323}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000324
pbos8fc7fa72015-07-15 08:02:58 -0700325TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioCreatedFirst) {
326 TestAudioVideoSync(false, true);
327}
328
329TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoCreatedFirst) {
330 TestAudioVideoSync(false, false);
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000331}
332
333TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
pbos8fc7fa72015-07-15 08:02:58 -0700334 TestAudioVideoSync(true, false);
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000335}
336
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000337void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
338 int threshold_ms,
339 int start_time_ms,
340 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000341 class CaptureNtpTimeObserver : public test::EndToEndTest,
342 public VideoRenderer {
343 public:
344 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
345 int threshold_ms,
346 int start_time_ms,
347 int run_time_ms)
348 : EndToEndTest(kLongTimeoutMs, config),
349 clock_(Clock::GetRealTimeClock()),
350 threshold_ms_(threshold_ms),
351 start_time_ms_(start_time_ms),
352 run_time_ms_(run_time_ms),
353 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000354 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000355 rtp_start_timestamp_set_(false),
356 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000357
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000358 private:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700359 void RenderFrame(const VideoFrame& video_frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000360 int time_to_render_ms) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000361 if (video_frame.ntp_time_ms() <= 0) {
362 // Haven't got enough RTCP SR in order to calculate the capture ntp
363 // time.
364 return;
365 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000366
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000367 int64_t now_ms = clock_->TimeInMilliseconds();
368 int64_t time_since_creation = now_ms - creation_time_ms_;
369 if (time_since_creation < start_time_ms_) {
370 // Wait for |start_time_ms_| before start measuring.
371 return;
372 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000373
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000374 if (time_since_creation > run_time_ms_) {
375 observation_complete_->Set();
376 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000377
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000378 FrameCaptureTimeList::iterator iter =
379 capture_time_list_.find(video_frame.timestamp());
380 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000381
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000382 // The real capture time has been wrapped to uint32_t before converted
383 // to rtp timestamp in the sender side. So here we convert the estimated
384 // capture time to a uint32_t 90k timestamp also for comparing.
385 uint32_t estimated_capture_timestamp =
386 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
387 uint32_t real_capture_timestamp = iter->second;
388 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
389 time_offset_ms = time_offset_ms / 90;
390 std::stringstream ss;
391 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000392
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000393 webrtc::test::PrintResult(
394 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
395 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
396 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000397
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000398 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000399
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000400 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
401 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000402 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000403
404 if (!rtp_start_timestamp_set_) {
405 // Calculate the rtp timestamp offset in order to calculate the real
406 // capture time.
407 uint32_t first_capture_timestamp =
408 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
409 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
410 rtp_start_timestamp_set_ = true;
411 }
412
413 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
414 capture_time_list_.insert(
415 capture_time_list_.end(),
416 std::make_pair(header.timestamp, capture_timestamp));
417 return SEND_PACKET;
418 }
419
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000420 void OnFrameGeneratorCapturerCreated(
421 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000422 capturer_ = frame_generator_capturer;
423 }
424
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000425 void ModifyConfigs(VideoSendStream::Config* send_config,
426 std::vector<VideoReceiveStream::Config>* receive_configs,
427 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000428 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000429 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000430 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000431 }
432
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000433 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000434 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
435 "estimated capture NTP time to be "
436 "within bounds.";
437 }
438
439 Clock* clock_;
440 int threshold_ms_;
441 int start_time_ms_;
442 int run_time_ms_;
443 int64_t creation_time_ms_;
444 test::FrameGeneratorCapturer* capturer_;
445 bool rtp_start_timestamp_set_;
446 uint32_t rtp_start_timestamp_;
447 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
448 FrameCaptureTimeList capture_time_list_;
449 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
450
451 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000452}
453
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000454TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000455 FakeNetworkPipe::Config net_config;
456 net_config.queue_delay_ms = 100;
457 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
458 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000459 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000460 const int kStartTimeMs = 10000;
461 const int kRunTimeMs = 20000;
462 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
463}
464
wu@webrtc.org0224c202014-05-05 17:42:43 +0000465TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000466 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000467 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000468 net_config.delay_standard_deviation_ms = 10;
469 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
470 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000471 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000472 const int kStartTimeMs = 10000;
473 const int kRunTimeMs = 20000;
474 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
475}
476
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000477void CallPerfTest::TestCpuOveruse(LoadObserver::Load tested_load,
478 int encode_delay_ms) {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000479 class LoadObserver : public test::SendTest, public webrtc::LoadObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000480 public:
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000481 LoadObserver(LoadObserver::Load tested_load, int encode_delay_ms)
482 : SendTest(kLongTimeoutMs),
483 tested_load_(tested_load),
484 encoder_(Clock::GetRealTimeClock(), encode_delay_ms) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000485
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000486 void OnLoadUpdate(Load load) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000487 if (load == tested_load_)
488 observation_complete_->Set();
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000489 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000490
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000491 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000492 Call::Config config(SendTransport());
493 config.overuse_callback = this;
494 return config;
495 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000496
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000497 void ModifyConfigs(VideoSendStream::Config* send_config,
498 std::vector<VideoReceiveStream::Config>* receive_configs,
499 VideoEncoderConfig* encoder_config) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000500 send_config->encoder_settings.encoder = &encoder_;
501 }
502
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000503 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000504 EXPECT_EQ(kEventSignaled, Wait())
505 << "Timed out before receiving an overuse callback.";
506 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000507
508 LoadObserver::Load tested_load_;
509 test::DelayedEncoder encoder_;
510 } test(tested_load, encode_delay_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000511
512 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000513}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000514
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000515TEST_F(CallPerfTest, ReceivesCpuUnderuse) {
516 const int kEncodeDelayMs = 2;
517 TestCpuOveruse(LoadObserver::kUnderuse, kEncodeDelayMs);
518}
519
520TEST_F(CallPerfTest, ReceivesCpuOveruse) {
521 const int kEncodeDelayMs = 35;
522 TestCpuOveruse(LoadObserver::kOveruse, kEncodeDelayMs);
523}
524
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000525void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
526 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000527 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000528 static const int kMinAcceptableTransmitBitrate = 130;
529 static const int kMaxAcceptableTransmitBitrate = 170;
530 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700531 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000532 class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000533 public:
534 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000535 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000536 send_stream_(nullptr),
537 send_transport_receiver_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000538 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000539 num_bitrate_observations_in_range_(0) {}
540
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000541 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000542 void SetReceivers(PacketReceiver* send_transport_receiver,
543 PacketReceiver* receive_transport_receiver) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000544 send_transport_receiver_ = send_transport_receiver;
545 test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
546 }
547
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200548 DeliveryStatus DeliverPacket(MediaType media_type, const uint8_t* packet,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000549 size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000550 VideoSendStream::Stats stats = send_stream_->GetStats();
551 if (stats.substreams.size() > 0) {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000552 DCHECK_EQ(1u, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000553 int bitrate_kbps =
554 stats.substreams.begin()->second.total_bitrate_bps / 1000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000555 if (bitrate_kbps > 0) {
556 test::PrintResult(
557 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000558 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
559 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000560 "bitrate_kbps",
561 static_cast<size_t>(bitrate_kbps),
562 "kbps",
563 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000564 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000565 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
566 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
567 ++num_bitrate_observations_in_range_;
568 }
569 } else {
570 // Expect bitrate stats to roughly match the max encode bitrate.
sprang867fb522015-08-03 04:38:41 -0700571 if (bitrate_kbps > (kMaxEncodeBitrateKbps -
572 kAcceptableBitrateErrorMargin / 2) &&
573 bitrate_kbps < (kMaxEncodeBitrateKbps +
574 kAcceptableBitrateErrorMargin / 2)) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000575 ++num_bitrate_observations_in_range_;
576 }
577 }
578 if (num_bitrate_observations_in_range_ ==
579 kNumBitrateObservationsInRange)
580 observation_complete_->Set();
581 }
582 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200583 return send_transport_receiver_->DeliverPacket(media_type, packet,
584 length);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000585 }
586
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000587 void OnStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000588 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000589 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000590 send_stream_ = send_stream;
591 }
592
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000593 void ModifyConfigs(VideoSendStream::Config* send_config,
594 std::vector<VideoReceiveStream::Config>* receive_configs,
595 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000596 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000597 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000598 } else {
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000599 DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000600 }
601 }
602
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000603 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000604 EXPECT_EQ(kEventSignaled, Wait())
605 << "Timeout while waiting for send-bitrate stats.";
606 }
607
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000608 VideoSendStream* send_stream_;
609 PacketReceiver* send_transport_receiver_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000610 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000611 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000612 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000613
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000614 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000615 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000616}
617
618TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
619
620TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
621 TestMinTransmitBitrate(false);
622}
623
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000624TEST_F(CallPerfTest, KeepsHighBitrateWhenReconfiguringSender) {
625 static const uint32_t kInitialBitrateKbps = 400;
626 static const uint32_t kReconfigureThresholdKbps = 600;
627 static const uint32_t kPermittedReconfiguredBitrateDiffKbps = 100;
628
629 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
630 public:
631 BitrateObserver()
632 : EndToEndTest(kDefaultTimeoutMs),
633 FakeEncoder(Clock::GetRealTimeClock()),
634 time_to_reconfigure_(webrtc::EventWrapper::Create()),
sprang867fb522015-08-03 04:38:41 -0700635 encoder_inits_(0),
636 last_set_bitrate_(0),
637 send_stream_(nullptr) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000638
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000639 int32_t InitEncode(const VideoCodec* config,
640 int32_t number_of_cores,
641 size_t max_payload_size) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000642 if (encoder_inits_ == 0) {
643 EXPECT_EQ(kInitialBitrateKbps, config->startBitrate)
644 << "Encoder not initialized at expected bitrate.";
645 }
646 ++encoder_inits_;
647 if (encoder_inits_ == 2) {
648 EXPECT_GE(last_set_bitrate_, kReconfigureThresholdKbps);
649 EXPECT_NEAR(config->startBitrate,
650 last_set_bitrate_,
651 kPermittedReconfiguredBitrateDiffKbps)
652 << "Encoder reconfigured with bitrate too far away from last set.";
653 observation_complete_->Set();
654 }
655 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
656 }
657
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000658 int32_t SetRates(uint32_t new_target_bitrate_kbps,
659 uint32_t framerate) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000660 last_set_bitrate_ = new_target_bitrate_kbps;
661 if (encoder_inits_ == 1 &&
662 new_target_bitrate_kbps > kReconfigureThresholdKbps) {
663 time_to_reconfigure_->Set();
664 }
665 return FakeEncoder::SetRates(new_target_bitrate_kbps, framerate);
666 }
667
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000668 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000669 Call::Config config = EndToEndTest::GetSenderCallConfig();
Stefan Holmere5904162015-03-26 11:11:06 +0100670 config.bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000671 return config;
672 }
673
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000674 void ModifyConfigs(VideoSendStream::Config* send_config,
675 std::vector<VideoReceiveStream::Config>* receive_configs,
676 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000677 send_config->encoder_settings.encoder = this;
678 encoder_config->streams[0].min_bitrate_bps = 50000;
679 encoder_config->streams[0].target_bitrate_bps =
680 encoder_config->streams[0].max_bitrate_bps = 2000000;
681
682 encoder_config_ = *encoder_config;
683 }
684
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000685 void OnStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000686 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000687 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000688 send_stream_ = send_stream;
689 }
690
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000691 void PerformTest() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000692 ASSERT_EQ(kEventSignaled, time_to_reconfigure_->Wait(kDefaultTimeoutMs))
693 << "Timed out before receiving an initial high bitrate.";
694 encoder_config_.streams[0].width *= 2;
695 encoder_config_.streams[0].height *= 2;
696 EXPECT_TRUE(send_stream_->ReconfigureVideoEncoder(encoder_config_));
697 EXPECT_EQ(kEventSignaled, Wait())
698 << "Timed out while waiting for a couple of high bitrate estimates "
699 "after reconfiguring the send stream.";
700 }
701
702 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000703 rtc::scoped_ptr<webrtc::EventWrapper> time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000704 int encoder_inits_;
705 uint32_t last_set_bitrate_;
706 VideoSendStream* send_stream_;
707 VideoEncoderConfig encoder_config_;
708 } test;
709
710 RunBaseTest(&test);
711}
712
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000713} // namespace webrtc