blob: 9f3dbf241acd424da4184eda11a1c4de2c999510 [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 */
10#include <assert.h>
11
12#include <algorithm>
13#include <sstream>
14#include <string>
15
16#include "testing/gtest/include/gtest/gtest.h"
17
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000018#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000019#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000020#include "webrtc/call.h"
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +000021#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000022#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
23#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
24#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000025#include "webrtc/system_wrappers/interface/rtp_to_ntp.h"
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000026#include "webrtc/test/call_test.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000027#include "webrtc/test/direct_transport.h"
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000028#include "webrtc/test/encoder_settings.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000029#include "webrtc/test/fake_audio_device.h"
30#include "webrtc/test/fake_decoder.h"
31#include "webrtc/test/fake_encoder.h"
32#include "webrtc/test/frame_generator.h"
33#include "webrtc/test/frame_generator_capturer.h"
34#include "webrtc/test/rtp_rtcp_observer.h"
35#include "webrtc/test/testsupport/fileutils.h"
36#include "webrtc/test/testsupport/perf_test.h"
37#include "webrtc/video/transport_adapter.h"
38#include "webrtc/voice_engine/include/voe_base.h"
39#include "webrtc/voice_engine/include/voe_codec.h"
40#include "webrtc/voice_engine/include/voe_network.h"
41#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
42#include "webrtc/voice_engine/include/voe_video_sync.h"
43
44namespace webrtc {
45
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000046class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000047 protected:
stefan@webrtc.org01581da2014-09-04 06:48:14 +000048 void TestAudioVideoSync(bool fec);
49
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000050 void TestCpuOveruse(LoadObserver::Load tested_load, int encode_delay_ms);
51
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000052 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
53
wu@webrtc.orgcd701192014-04-24 22:10:24 +000054 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
55 int threshold_ms,
56 int start_time_ms,
57 int run_time_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000058};
59
60class SyncRtcpObserver : public test::RtpRtcpObserver {
61 public:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000062 explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000063 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000064 crit_(CriticalSectionWrapper::CreateCriticalSection()) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000065
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000066 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000067 RTCPUtility::RTCPParserV2 parser(packet, length, true);
68 EXPECT_TRUE(parser.IsValid());
69
70 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
71 packet_type != RTCPUtility::kRtcpNotValidCode;
72 packet_type = parser.Iterate()) {
73 if (packet_type == RTCPUtility::kRtcpSrCode) {
74 const RTCPUtility::RTCPPacket& packet = parser.Packet();
wu@webrtc.org66773a02014-05-07 17:09:44 +000075 RtcpMeasurement ntp_rtp_pair(
pbos@webrtc.org1d096902013-12-13 12:48:05 +000076 packet.SR.NTPMostSignificant,
77 packet.SR.NTPLeastSignificant,
78 packet.SR.RTPTimestamp);
79 StoreNtpRtpPair(ntp_rtp_pair);
80 }
81 }
82 return SEND_PACKET;
83 }
84
85 int64_t RtpTimestampToNtp(uint32_t timestamp) const {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000086 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +000087 int64_t timestamp_in_ms = -1;
88 if (ntp_rtp_pairs_.size() == 2) {
89 // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
90 // RTCP sender where it sends RTCP SR before any RTP packets, which leads
91 // to a bogus NTP/RTP mapping.
wu@webrtc.org66773a02014-05-07 17:09:44 +000092 RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000093 return timestamp_in_ms;
94 }
95 return -1;
96 }
97
98 private:
wu@webrtc.org66773a02014-05-07 17:09:44 +000099 void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000100 CriticalSectionScoped lock(crit_.get());
wu@webrtc.org66773a02014-05-07 17:09:44 +0000101 for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000102 it != ntp_rtp_pairs_.end();
103 ++it) {
104 if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
105 ntp_rtp_pair.ntp_frac == it->ntp_frac) {
106 // This RTCP has already been added to the list.
107 return;
108 }
109 }
110 // We need two RTCP SR reports to map between RTP and NTP. More than two
111 // will not improve the mapping.
112 if (ntp_rtp_pairs_.size() == 2) {
113 ntp_rtp_pairs_.pop_back();
114 }
115 ntp_rtp_pairs_.push_front(ntp_rtp_pair);
116 }
117
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000118 const rtc::scoped_ptr<CriticalSectionWrapper> crit_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000119 RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000120};
121
122class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
123 static const int kInSyncThresholdMs = 50;
124 static const int kStartupTimeMs = 2000;
125 static const int kMinRunTimeMs = 30000;
126
127 public:
128 VideoRtcpAndSyncObserver(Clock* clock,
129 int voe_channel,
130 VoEVideoSync* voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000131 SyncRtcpObserver* audio_observer)
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000132 : SyncRtcpObserver(FakeNetworkPipe::Config()),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000133 clock_(clock),
134 voe_channel_(voe_channel),
135 voe_sync_(voe_sync),
136 audio_observer_(audio_observer),
137 creation_time_ms_(clock_->TimeInMilliseconds()),
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000138 first_time_in_sync_(-1) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000139
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000140 void RenderFrame(const I420VideoFrame& video_frame,
141 int time_to_render_ms) override {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000142 int64_t now_ms = clock_->TimeInMilliseconds();
143 uint32_t playout_timestamp = 0;
144 if (voe_sync_->GetPlayoutTimestamp(voe_channel_, playout_timestamp) != 0)
145 return;
146 int64_t latest_audio_ntp =
147 audio_observer_->RtpTimestampToNtp(playout_timestamp);
148 int64_t latest_video_ntp = RtpTimestampToNtp(video_frame.timestamp());
149 if (latest_audio_ntp < 0 || latest_video_ntp < 0)
150 return;
151 int time_until_render_ms =
152 std::max(0, static_cast<int>(video_frame.render_time_ms() - now_ms));
153 latest_video_ntp += time_until_render_ms;
154 int64_t stream_offset = latest_audio_ntp - latest_video_ntp;
155 std::stringstream ss;
156 ss << stream_offset;
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000157 webrtc::test::PrintResult("stream_offset",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000158 "",
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000159 "synchronization",
160 ss.str(),
161 "ms",
162 false);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000163 int64_t time_since_creation = now_ms - creation_time_ms_;
164 // During the first couple of seconds audio and video can falsely be
165 // estimated as being synchronized. We don't want to trigger on those.
166 if (time_since_creation < kStartupTimeMs)
167 return;
pbos@webrtc.orgb5f30292014-03-13 08:53:39 +0000168 if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000169 if (first_time_in_sync_ == -1) {
170 first_time_in_sync_ = now_ms;
171 webrtc::test::PrintResult("sync_convergence_time",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000172 "",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000173 "synchronization",
174 time_since_creation,
175 "ms",
176 false);
177 }
178 if (time_since_creation > kMinRunTimeMs)
179 observation_complete_->Set();
180 }
181 }
182
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000183 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000184
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000185 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000186 Clock* const clock_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000187 int voe_channel_;
188 VoEVideoSync* voe_sync_;
189 SyncRtcpObserver* audio_observer_;
190 int64_t creation_time_ms_;
191 int64_t first_time_in_sync_;
192};
193
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000194void CallPerfTest::TestAudioVideoSync(bool fec) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000195 class AudioPacketReceiver : public PacketReceiver {
196 public:
197 AudioPacketReceiver(int channel, VoENetwork* voe_network)
198 : channel_(channel),
199 voe_network_(voe_network),
200 parser_(RtpHeaderParser::Create()) {}
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000201 DeliveryStatus DeliverPacket(const uint8_t* packet,
202 size_t length) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000203 int ret;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000204 if (parser_->IsRtcp(packet, length)) {
205 ret = voe_network_->ReceivedRTCPPacket(channel_, packet, length);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000206 } else {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000207 ret = voe_network_->ReceivedRTPPacket(channel_, packet, length,
208 PacketTime());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000209 }
210 return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
211 }
212
213 private:
214 int channel_;
215 VoENetwork* voe_network_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000216 rtc::scoped_ptr<RtpHeaderParser> parser_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000217 };
218
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000219 VoiceEngine* voice_engine = VoiceEngine::Create();
220 VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
221 VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
222 VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine);
223 VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine);
224 const std::string audio_filename =
225 test::ResourcePath("voice_engine/audio_long16", "pcm");
226 ASSERT_STRNE("", audio_filename.c_str());
227 test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(),
228 audio_filename);
229 EXPECT_EQ(0, voe_base->Init(&fake_audio_device, NULL));
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000230 int channel = voe_base->CreateChannel();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000231
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000232 FakeNetworkPipe::Config net_config;
233 net_config.queue_delay_ms = 500;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000234 net_config.loss_percent = 5;
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000235 SyncRtcpObserver audio_observer(net_config);
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000236 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
237 channel,
238 voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000239 &audio_observer);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000240
241 Call::Config receiver_config(observer.ReceiveTransport());
242 receiver_config.voice_engine = voice_engine;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000243 CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
244
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000245 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
246 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
247
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000248 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000249 audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
250
251 internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000252 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000253 EXPECT_EQ(0,
254 voe_network->RegisterExternalTransport(channel, transport_adapter));
255
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000256 observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000257
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000258 test::FakeDecoder fake_decoder;
259
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000260 CreateSendConfig(1);
261 CreateMatchingReceiveConfigs();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000262
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000263 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
264 if (fec) {
265 send_config_.rtp.fec.red_payload_type = kRedPayloadType;
266 send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
267 receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
268 receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
269 }
270 receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000271 receive_configs_[0].renderer = &observer;
272 receive_configs_[0].audio_channel_id = channel;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000273
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000274 CreateStreams();
275
276 CreateFrameGeneratorCapturer();
277
278 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000279
280 fake_audio_device.Start();
281 EXPECT_EQ(0, voe_base->StartPlayout(channel));
282 EXPECT_EQ(0, voe_base->StartReceive(channel));
283 EXPECT_EQ(0, voe_base->StartSend(channel));
284
285 EXPECT_EQ(kEventSignaled, observer.Wait())
286 << "Timed out while waiting for audio and video to be synchronized.";
287
288 EXPECT_EQ(0, voe_base->StopSend(channel));
289 EXPECT_EQ(0, voe_base->StopReceive(channel));
290 EXPECT_EQ(0, voe_base->StopPlayout(channel));
291 fake_audio_device.Stop();
292
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000293 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000294 observer.StopSending();
295 audio_observer.StopSending();
296
297 voe_base->DeleteChannel(channel);
298 voe_base->Release();
299 voe_codec->Release();
300 voe_network->Release();
301 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000302
303 DestroyStreams();
304
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000305 VoiceEngine::Delete(voice_engine);
306}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000307
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000308TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
309 TestAudioVideoSync(false);
310}
311
312TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
313 TestAudioVideoSync(true);
314}
315
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000316void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
317 int threshold_ms,
318 int start_time_ms,
319 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000320 class CaptureNtpTimeObserver : public test::EndToEndTest,
321 public VideoRenderer {
322 public:
323 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
324 int threshold_ms,
325 int start_time_ms,
326 int run_time_ms)
327 : EndToEndTest(kLongTimeoutMs, config),
328 clock_(Clock::GetRealTimeClock()),
329 threshold_ms_(threshold_ms),
330 start_time_ms_(start_time_ms),
331 run_time_ms_(run_time_ms),
332 creation_time_ms_(clock_->TimeInMilliseconds()),
333 capturer_(NULL),
334 rtp_start_timestamp_set_(false),
335 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000336
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000337 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000338 void RenderFrame(const I420VideoFrame& video_frame,
339 int time_to_render_ms) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000340 if (video_frame.ntp_time_ms() <= 0) {
341 // Haven't got enough RTCP SR in order to calculate the capture ntp
342 // time.
343 return;
344 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000345
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000346 int64_t now_ms = clock_->TimeInMilliseconds();
347 int64_t time_since_creation = now_ms - creation_time_ms_;
348 if (time_since_creation < start_time_ms_) {
349 // Wait for |start_time_ms_| before start measuring.
350 return;
351 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000352
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000353 if (time_since_creation > run_time_ms_) {
354 observation_complete_->Set();
355 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000356
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000357 FrameCaptureTimeList::iterator iter =
358 capture_time_list_.find(video_frame.timestamp());
359 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000360
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000361 // The real capture time has been wrapped to uint32_t before converted
362 // to rtp timestamp in the sender side. So here we convert the estimated
363 // capture time to a uint32_t 90k timestamp also for comparing.
364 uint32_t estimated_capture_timestamp =
365 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
366 uint32_t real_capture_timestamp = iter->second;
367 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
368 time_offset_ms = time_offset_ms / 90;
369 std::stringstream ss;
370 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000371
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000372 webrtc::test::PrintResult(
373 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
374 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
375 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000376
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000377 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000378
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000379 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
380 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000381 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000382
383 if (!rtp_start_timestamp_set_) {
384 // Calculate the rtp timestamp offset in order to calculate the real
385 // capture time.
386 uint32_t first_capture_timestamp =
387 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
388 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
389 rtp_start_timestamp_set_ = true;
390 }
391
392 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
393 capture_time_list_.insert(
394 capture_time_list_.end(),
395 std::make_pair(header.timestamp, capture_timestamp));
396 return SEND_PACKET;
397 }
398
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000399 void OnFrameGeneratorCapturerCreated(
400 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000401 capturer_ = frame_generator_capturer;
402 }
403
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000404 void ModifyConfigs(VideoSendStream::Config* send_config,
405 std::vector<VideoReceiveStream::Config>* receive_configs,
406 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000407 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000408 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000409 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000410 }
411
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000412 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000413 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
414 "estimated capture NTP time to be "
415 "within bounds.";
416 }
417
418 Clock* clock_;
419 int threshold_ms_;
420 int start_time_ms_;
421 int run_time_ms_;
422 int64_t creation_time_ms_;
423 test::FrameGeneratorCapturer* capturer_;
424 bool rtp_start_timestamp_set_;
425 uint32_t rtp_start_timestamp_;
426 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
427 FrameCaptureTimeList capture_time_list_;
428 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
429
430 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000431}
432
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000433TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000434 FakeNetworkPipe::Config net_config;
435 net_config.queue_delay_ms = 100;
436 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
437 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000438 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000439 const int kStartTimeMs = 10000;
440 const int kRunTimeMs = 20000;
441 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
442}
443
wu@webrtc.org0224c202014-05-05 17:42:43 +0000444TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000445 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000446 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000447 net_config.delay_standard_deviation_ms = 10;
448 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
449 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000450 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000451 const int kStartTimeMs = 10000;
452 const int kRunTimeMs = 20000;
453 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
454}
455
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000456void CallPerfTest::TestCpuOveruse(LoadObserver::Load tested_load,
457 int encode_delay_ms) {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000458 class LoadObserver : public test::SendTest, public webrtc::LoadObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000459 public:
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000460 LoadObserver(LoadObserver::Load tested_load, int encode_delay_ms)
461 : SendTest(kLongTimeoutMs),
462 tested_load_(tested_load),
463 encoder_(Clock::GetRealTimeClock(), encode_delay_ms) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000464
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000465 void OnLoadUpdate(Load load) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000466 if (load == tested_load_)
467 observation_complete_->Set();
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000468 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000469
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000470 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000471 Call::Config config(SendTransport());
472 config.overuse_callback = this;
473 return config;
474 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000475
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000476 void ModifyConfigs(VideoSendStream::Config* send_config,
477 std::vector<VideoReceiveStream::Config>* receive_configs,
478 VideoEncoderConfig* encoder_config) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000479 send_config->encoder_settings.encoder = &encoder_;
480 }
481
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000482 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000483 EXPECT_EQ(kEventSignaled, Wait())
484 << "Timed out before receiving an overuse callback.";
485 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000486
487 LoadObserver::Load tested_load_;
488 test::DelayedEncoder encoder_;
489 } test(tested_load, encode_delay_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000490
491 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000492}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000493
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000494TEST_F(CallPerfTest, ReceivesCpuUnderuse) {
495 const int kEncodeDelayMs = 2;
496 TestCpuOveruse(LoadObserver::kUnderuse, kEncodeDelayMs);
497}
498
499TEST_F(CallPerfTest, ReceivesCpuOveruse) {
500 const int kEncodeDelayMs = 35;
501 TestCpuOveruse(LoadObserver::kOveruse, kEncodeDelayMs);
502}
503
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000504void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
505 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000506 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000507 static const int kMinAcceptableTransmitBitrate = 130;
508 static const int kMaxAcceptableTransmitBitrate = 170;
509 static const int kNumBitrateObservationsInRange = 100;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000510 class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000511 public:
512 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000513 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000514 send_stream_(NULL),
515 send_transport_receiver_(NULL),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000516 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000517 num_bitrate_observations_in_range_(0) {}
518
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000519 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000520 void SetReceivers(PacketReceiver* send_transport_receiver,
521 PacketReceiver* receive_transport_receiver) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000522 send_transport_receiver_ = send_transport_receiver;
523 test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
524 }
525
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000526 DeliveryStatus DeliverPacket(const uint8_t* packet,
527 size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000528 VideoSendStream::Stats stats = send_stream_->GetStats();
529 if (stats.substreams.size() > 0) {
530 assert(stats.substreams.size() == 1);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000531 int bitrate_kbps =
532 stats.substreams.begin()->second.total_bitrate_bps / 1000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000533 if (bitrate_kbps > 0) {
534 test::PrintResult(
535 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000536 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
537 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000538 "bitrate_kbps",
539 static_cast<size_t>(bitrate_kbps),
540 "kbps",
541 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000542 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000543 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
544 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
545 ++num_bitrate_observations_in_range_;
546 }
547 } else {
548 // Expect bitrate stats to roughly match the max encode bitrate.
549 if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 &&
550 bitrate_kbps < kMaxEncodeBitrateKbps + 5) {
551 ++num_bitrate_observations_in_range_;
552 }
553 }
554 if (num_bitrate_observations_in_range_ ==
555 kNumBitrateObservationsInRange)
556 observation_complete_->Set();
557 }
558 }
559 return send_transport_receiver_->DeliverPacket(packet, length);
560 }
561
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000562 void OnStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000563 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000564 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000565 send_stream_ = send_stream;
566 }
567
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000568 void ModifyConfigs(VideoSendStream::Config* send_config,
569 std::vector<VideoReceiveStream::Config>* receive_configs,
570 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000571 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000572 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000573 } else {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000574 assert(encoder_config->min_transmit_bitrate_bps == 0);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000575 }
576 }
577
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000578 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000579 EXPECT_EQ(kEventSignaled, Wait())
580 << "Timeout while waiting for send-bitrate stats.";
581 }
582
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000583 VideoSendStream* send_stream_;
584 PacketReceiver* send_transport_receiver_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000585 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000586 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000587 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000588
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000589 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000590 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000591}
592
593TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
594
595TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
596 TestMinTransmitBitrate(false);
597}
598
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000599TEST_F(CallPerfTest, KeepsHighBitrateWhenReconfiguringSender) {
600 static const uint32_t kInitialBitrateKbps = 400;
601 static const uint32_t kReconfigureThresholdKbps = 600;
602 static const uint32_t kPermittedReconfiguredBitrateDiffKbps = 100;
603
604 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
605 public:
606 BitrateObserver()
607 : EndToEndTest(kDefaultTimeoutMs),
608 FakeEncoder(Clock::GetRealTimeClock()),
609 time_to_reconfigure_(webrtc::EventWrapper::Create()),
610 encoder_inits_(0) {}
611
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000612 int32_t InitEncode(const VideoCodec* config,
613 int32_t number_of_cores,
614 size_t max_payload_size) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000615 if (encoder_inits_ == 0) {
616 EXPECT_EQ(kInitialBitrateKbps, config->startBitrate)
617 << "Encoder not initialized at expected bitrate.";
618 }
619 ++encoder_inits_;
620 if (encoder_inits_ == 2) {
621 EXPECT_GE(last_set_bitrate_, kReconfigureThresholdKbps);
622 EXPECT_NEAR(config->startBitrate,
623 last_set_bitrate_,
624 kPermittedReconfiguredBitrateDiffKbps)
625 << "Encoder reconfigured with bitrate too far away from last set.";
626 observation_complete_->Set();
627 }
628 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
629 }
630
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000631 int32_t SetRates(uint32_t new_target_bitrate_kbps,
632 uint32_t framerate) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000633 last_set_bitrate_ = new_target_bitrate_kbps;
634 if (encoder_inits_ == 1 &&
635 new_target_bitrate_kbps > kReconfigureThresholdKbps) {
636 time_to_reconfigure_->Set();
637 }
638 return FakeEncoder::SetRates(new_target_bitrate_kbps, framerate);
639 }
640
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000641 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000642 Call::Config config = EndToEndTest::GetSenderCallConfig();
pbos@webrtc.org00873182014-11-25 14:03:34 +0000643 config.stream_bitrates.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000644 return config;
645 }
646
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000647 void ModifyConfigs(VideoSendStream::Config* send_config,
648 std::vector<VideoReceiveStream::Config>* receive_configs,
649 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000650 send_config->encoder_settings.encoder = this;
651 encoder_config->streams[0].min_bitrate_bps = 50000;
652 encoder_config->streams[0].target_bitrate_bps =
653 encoder_config->streams[0].max_bitrate_bps = 2000000;
654
655 encoder_config_ = *encoder_config;
656 }
657
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000658 void OnStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000659 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000660 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000661 send_stream_ = send_stream;
662 }
663
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000664 void PerformTest() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000665 ASSERT_EQ(kEventSignaled, time_to_reconfigure_->Wait(kDefaultTimeoutMs))
666 << "Timed out before receiving an initial high bitrate.";
667 encoder_config_.streams[0].width *= 2;
668 encoder_config_.streams[0].height *= 2;
669 EXPECT_TRUE(send_stream_->ReconfigureVideoEncoder(encoder_config_));
670 EXPECT_EQ(kEventSignaled, Wait())
671 << "Timed out while waiting for a couple of high bitrate estimates "
672 "after reconfiguring the send stream.";
673 }
674
675 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000676 rtc::scoped_ptr<webrtc::EventWrapper> time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000677 int encoder_inits_;
678 uint32_t last_set_bitrate_;
679 VideoSendStream* send_stream_;
680 VideoEncoderConfig encoder_config_;
681 } test;
682
683 RunBaseTest(&test);
684}
685
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000686} // namespace webrtc