blob: a6e619eefdf3c392ac4f15e81e8804b5aab793f1 [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
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.org1d096902013-12-13 12:48:05 +000025#include "webrtc/system_wrappers/interface/scoped_ptr.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
66 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
67 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
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000118 const 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
140 virtual void RenderFrame(const I420VideoFrame& video_frame,
141 int time_to_render_ms) OVERRIDE {
142 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
183 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
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000192void CallPerfTest::TestAudioVideoSync(bool fec) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000193 class AudioPacketReceiver : public PacketReceiver {
194 public:
195 AudioPacketReceiver(int channel, VoENetwork* voe_network)
196 : channel_(channel),
197 voe_network_(voe_network),
198 parser_(RtpHeaderParser::Create()) {}
199 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
200 size_t length) OVERRIDE {
201 int ret;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000202 if (parser_->IsRtcp(packet, length)) {
203 ret = voe_network_->ReceivedRTCPPacket(channel_, packet, length);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000204 } else {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000205 ret = voe_network_->ReceivedRTPPacket(channel_, packet, length,
206 PacketTime());
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000207 }
208 return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
209 }
210
211 private:
212 int channel_;
213 VoENetwork* voe_network_;
214 scoped_ptr<RtpHeaderParser> parser_;
215 };
216
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000217 VoiceEngine* voice_engine = VoiceEngine::Create();
218 VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
219 VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
220 VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine);
221 VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine);
222 const std::string audio_filename =
223 test::ResourcePath("voice_engine/audio_long16", "pcm");
224 ASSERT_STRNE("", audio_filename.c_str());
225 test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(),
226 audio_filename);
227 EXPECT_EQ(0, voe_base->Init(&fake_audio_device, NULL));
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000228 int channel = voe_base->CreateChannel();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000229
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000230 FakeNetworkPipe::Config net_config;
231 net_config.queue_delay_ms = 500;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000232 net_config.loss_percent = 5;
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000233 SyncRtcpObserver audio_observer(net_config);
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000234 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
235 channel,
236 voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000237 &audio_observer);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000238
239 Call::Config receiver_config(observer.ReceiveTransport());
240 receiver_config.voice_engine = voice_engine;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000241 CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
242
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000243 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
244 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
245
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000246 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000247 audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
248
249 internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000250 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000251 EXPECT_EQ(0,
252 voe_network->RegisterExternalTransport(channel, transport_adapter));
253
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000254 observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000255
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000256 test::FakeDecoder fake_decoder;
257
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000258 CreateSendConfig(1);
259 CreateMatchingReceiveConfigs();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000260
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000261 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
262 if (fec) {
263 send_config_.rtp.fec.red_payload_type = kRedPayloadType;
264 send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
265 receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
266 receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
267 }
268 receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000269 receive_configs_[0].renderer = &observer;
270 receive_configs_[0].audio_channel_id = channel;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000271
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000272 CreateStreams();
273
274 CreateFrameGeneratorCapturer();
275
276 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000277
278 fake_audio_device.Start();
279 EXPECT_EQ(0, voe_base->StartPlayout(channel));
280 EXPECT_EQ(0, voe_base->StartReceive(channel));
281 EXPECT_EQ(0, voe_base->StartSend(channel));
282
283 EXPECT_EQ(kEventSignaled, observer.Wait())
284 << "Timed out while waiting for audio and video to be synchronized.";
285
286 EXPECT_EQ(0, voe_base->StopSend(channel));
287 EXPECT_EQ(0, voe_base->StopReceive(channel));
288 EXPECT_EQ(0, voe_base->StopPlayout(channel));
289 fake_audio_device.Stop();
290
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000291 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000292 observer.StopSending();
293 audio_observer.StopSending();
294
295 voe_base->DeleteChannel(channel);
296 voe_base->Release();
297 voe_codec->Release();
298 voe_network->Release();
299 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000300
301 DestroyStreams();
302
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000303 VoiceEngine::Delete(voice_engine);
304}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000305
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000306TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
307 TestAudioVideoSync(false);
308}
309
310TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
311 TestAudioVideoSync(true);
312}
313
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000314void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
315 int threshold_ms,
316 int start_time_ms,
317 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000318 class CaptureNtpTimeObserver : public test::EndToEndTest,
319 public VideoRenderer {
320 public:
321 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
322 int threshold_ms,
323 int start_time_ms,
324 int run_time_ms)
325 : EndToEndTest(kLongTimeoutMs, config),
326 clock_(Clock::GetRealTimeClock()),
327 threshold_ms_(threshold_ms),
328 start_time_ms_(start_time_ms),
329 run_time_ms_(run_time_ms),
330 creation_time_ms_(clock_->TimeInMilliseconds()),
331 capturer_(NULL),
332 rtp_start_timestamp_set_(false),
333 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000334
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000335 private:
336 virtual void RenderFrame(const I420VideoFrame& video_frame,
337 int time_to_render_ms) OVERRIDE {
338 if (video_frame.ntp_time_ms() <= 0) {
339 // Haven't got enough RTCP SR in order to calculate the capture ntp
340 // time.
341 return;
342 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000343
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000344 int64_t now_ms = clock_->TimeInMilliseconds();
345 int64_t time_since_creation = now_ms - creation_time_ms_;
346 if (time_since_creation < start_time_ms_) {
347 // Wait for |start_time_ms_| before start measuring.
348 return;
349 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000350
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000351 if (time_since_creation > run_time_ms_) {
352 observation_complete_->Set();
353 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000354
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000355 FrameCaptureTimeList::iterator iter =
356 capture_time_list_.find(video_frame.timestamp());
357 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000358
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000359 // The real capture time has been wrapped to uint32_t before converted
360 // to rtp timestamp in the sender side. So here we convert the estimated
361 // capture time to a uint32_t 90k timestamp also for comparing.
362 uint32_t estimated_capture_timestamp =
363 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
364 uint32_t real_capture_timestamp = iter->second;
365 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
366 time_offset_ms = time_offset_ms / 90;
367 std::stringstream ss;
368 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000369
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000370 webrtc::test::PrintResult(
371 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
372 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
373 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000374
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000375 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
376 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000377 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000378
379 if (!rtp_start_timestamp_set_) {
380 // Calculate the rtp timestamp offset in order to calculate the real
381 // capture time.
382 uint32_t first_capture_timestamp =
383 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
384 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
385 rtp_start_timestamp_set_ = true;
386 }
387
388 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
389 capture_time_list_.insert(
390 capture_time_list_.end(),
391 std::make_pair(header.timestamp, capture_timestamp));
392 return SEND_PACKET;
393 }
394
395 virtual void OnFrameGeneratorCapturerCreated(
396 test::FrameGeneratorCapturer* frame_generator_capturer) OVERRIDE {
397 capturer_ = frame_generator_capturer;
398 }
399
400 virtual void ModifyConfigs(
401 VideoSendStream::Config* send_config,
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000402 std::vector<VideoReceiveStream::Config>* receive_configs,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000403 VideoEncoderConfig* encoder_config) OVERRIDE {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000404 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000405 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000406 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000407 }
408
409 virtual void PerformTest() OVERRIDE {
410 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
411 "estimated capture NTP time to be "
412 "within bounds.";
413 }
414
415 Clock* clock_;
416 int threshold_ms_;
417 int start_time_ms_;
418 int run_time_ms_;
419 int64_t creation_time_ms_;
420 test::FrameGeneratorCapturer* capturer_;
421 bool rtp_start_timestamp_set_;
422 uint32_t rtp_start_timestamp_;
423 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
424 FrameCaptureTimeList capture_time_list_;
425 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
426
427 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000428}
429
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000430TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000431 FakeNetworkPipe::Config net_config;
432 net_config.queue_delay_ms = 100;
433 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
434 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000435 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000436 const int kStartTimeMs = 10000;
437 const int kRunTimeMs = 20000;
438 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
439}
440
wu@webrtc.org0224c202014-05-05 17:42:43 +0000441TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000442 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000443 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000444 net_config.delay_standard_deviation_ms = 10;
445 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
446 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000447 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000448 const int kStartTimeMs = 10000;
449 const int kRunTimeMs = 20000;
450 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
451}
452
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000453void CallPerfTest::TestCpuOveruse(LoadObserver::Load tested_load,
454 int encode_delay_ms) {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000455 class LoadObserver : public test::SendTest, public webrtc::LoadObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000456 public:
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000457 LoadObserver(LoadObserver::Load tested_load, int encode_delay_ms)
458 : SendTest(kLongTimeoutMs),
459 tested_load_(tested_load),
460 encoder_(Clock::GetRealTimeClock(), encode_delay_ms) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000461
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000462 virtual void OnLoadUpdate(Load load) OVERRIDE {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000463 if (load == tested_load_)
464 observation_complete_->Set();
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000465 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000466
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000467 virtual Call::Config GetSenderCallConfig() OVERRIDE {
468 Call::Config config(SendTransport());
469 config.overuse_callback = this;
470 return config;
471 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000472
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000473 virtual void ModifyConfigs(
474 VideoSendStream::Config* send_config,
475 std::vector<VideoReceiveStream::Config>* receive_configs,
476 VideoEncoderConfig* encoder_config) OVERRIDE {
477 send_config->encoder_settings.encoder = &encoder_;
478 }
479
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000480 virtual void PerformTest() OVERRIDE {
481 EXPECT_EQ(kEventSignaled, Wait())
482 << "Timed out before receiving an overuse callback.";
483 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000484
485 LoadObserver::Load tested_load_;
486 test::DelayedEncoder encoder_;
487 } test(tested_load, encode_delay_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000488
489 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000490}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000491
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000492TEST_F(CallPerfTest, ReceivesCpuUnderuse) {
493 const int kEncodeDelayMs = 2;
494 TestCpuOveruse(LoadObserver::kUnderuse, kEncodeDelayMs);
495}
496
497TEST_F(CallPerfTest, ReceivesCpuOveruse) {
498 const int kEncodeDelayMs = 35;
499 TestCpuOveruse(LoadObserver::kOveruse, kEncodeDelayMs);
500}
501
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000502void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
503 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000504 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000505 static const int kMinAcceptableTransmitBitrate = 130;
506 static const int kMaxAcceptableTransmitBitrate = 170;
507 static const int kNumBitrateObservationsInRange = 100;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000508 class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000509 public:
510 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000511 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000512 send_stream_(NULL),
513 send_transport_receiver_(NULL),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000514 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000515 num_bitrate_observations_in_range_(0) {}
516
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000517 private:
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000518 virtual void SetReceivers(PacketReceiver* send_transport_receiver,
519 PacketReceiver* receive_transport_receiver)
520 OVERRIDE {
521 send_transport_receiver_ = send_transport_receiver;
522 test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
523 }
524
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000525 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
526 size_t length) OVERRIDE {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000527 VideoSendStream::Stats stats = send_stream_->GetStats();
528 if (stats.substreams.size() > 0) {
529 assert(stats.substreams.size() == 1);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000530 int bitrate_kbps =
531 stats.substreams.begin()->second.total_bitrate_bps / 1000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000532 if (bitrate_kbps > 0) {
533 test::PrintResult(
534 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000535 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
536 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000537 "bitrate_kbps",
538 static_cast<size_t>(bitrate_kbps),
539 "kbps",
540 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000541 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000542 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
543 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
544 ++num_bitrate_observations_in_range_;
545 }
546 } else {
547 // Expect bitrate stats to roughly match the max encode bitrate.
548 if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 &&
549 bitrate_kbps < kMaxEncodeBitrateKbps + 5) {
550 ++num_bitrate_observations_in_range_;
551 }
552 }
553 if (num_bitrate_observations_in_range_ ==
554 kNumBitrateObservationsInRange)
555 observation_complete_->Set();
556 }
557 }
558 return send_transport_receiver_->DeliverPacket(packet, length);
559 }
560
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000561 virtual void OnStreamsCreated(
562 VideoSendStream* send_stream,
563 const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000564 send_stream_ = send_stream;
565 }
566
567 virtual void ModifyConfigs(
568 VideoSendStream::Config* send_config,
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000569 std::vector<VideoReceiveStream::Config>* receive_configs,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000570 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
578 virtual void PerformTest() OVERRIDE {
579 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
612 virtual int32_t InitEncode(const VideoCodec* config,
613 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000614 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
631 virtual int32_t SetRates(uint32_t new_target_bitrate_kbps,
632 uint32_t framerate) OVERRIDE {
633 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
641 Call::Config GetSenderCallConfig() OVERRIDE {
642 Call::Config config = EndToEndTest::GetSenderCallConfig();
643 config.stream_start_bitrate_bps = kInitialBitrateKbps * 1000;
644 return config;
645 }
646
647 virtual void ModifyConfigs(
648 VideoSendStream::Config* send_config,
649 std::vector<VideoReceiveStream::Config>* receive_configs,
650 VideoEncoderConfig* encoder_config) OVERRIDE {
651 send_config->encoder_settings.encoder = this;
652 encoder_config->streams[0].min_bitrate_bps = 50000;
653 encoder_config->streams[0].target_bitrate_bps =
654 encoder_config->streams[0].max_bitrate_bps = 2000000;
655
656 encoder_config_ = *encoder_config;
657 }
658
659 virtual void OnStreamsCreated(
660 VideoSendStream* send_stream,
661 const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
662 send_stream_ = send_stream;
663 }
664
665 virtual void PerformTest() OVERRIDE {
666 ASSERT_EQ(kEventSignaled, time_to_reconfigure_->Wait(kDefaultTimeoutMs))
667 << "Timed out before receiving an initial high bitrate.";
668 encoder_config_.streams[0].width *= 2;
669 encoder_config_.streams[0].height *= 2;
670 EXPECT_TRUE(send_stream_->ReconfigureVideoEncoder(encoder_config_));
671 EXPECT_EQ(kEventSignaled, Wait())
672 << "Timed out while waiting for a couple of high bitrate estimates "
673 "after reconfiguring the send stream.";
674 }
675
676 private:
677 scoped_ptr<webrtc::EventWrapper> time_to_reconfigure_;
678 int encoder_inits_;
679 uint32_t last_set_bitrate_;
680 VideoSendStream* send_stream_;
681 VideoEncoderConfig encoder_config_;
682 } test;
683
684 RunBaseTest(&test);
685}
686
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000687} // namespace webrtc