blob: d9edac802eb57bbc3e020ca6f863381d8cf8d62d [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"
Peter Boström5c389d32015-09-25 13:58:30 +020020#include "webrtc/call/transport_adapter.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"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000037#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:
stefanf116bd02015-10-27 08:29:42 -070061 SyncRtcpObserver() : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000062
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 Action OnSendRtcp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000064 RTCPUtility::RTCPParserV2 parser(packet, length, true);
65 EXPECT_TRUE(parser.IsValid());
66
67 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
Erik Språng242e22b2015-05-11 10:17:43 +020068 packet_type != RTCPUtility::RTCPPacketTypes::kInvalid;
pbos@webrtc.org1d096902013-12-13 12:48:05 +000069 packet_type = parser.Iterate()) {
Erik Språng242e22b2015-05-11 10:17:43 +020070 if (packet_type == RTCPUtility::RTCPPacketTypes::kSr) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +000071 const RTCPUtility::RTCPPacket& packet = parser.Packet();
wu@webrtc.org66773a02014-05-07 17:09:44 +000072 RtcpMeasurement ntp_rtp_pair(
pbos@webrtc.org1d096902013-12-13 12:48:05 +000073 packet.SR.NTPMostSignificant,
74 packet.SR.NTPLeastSignificant,
75 packet.SR.RTPTimestamp);
76 StoreNtpRtpPair(ntp_rtp_pair);
77 }
78 }
79 return SEND_PACKET;
80 }
81
82 int64_t RtpTimestampToNtp(uint32_t timestamp) const {
Peter Boströmf2f82832015-05-01 13:00:41 +020083 rtc::CritScope lock(&crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000084 int64_t timestamp_in_ms = -1;
85 if (ntp_rtp_pairs_.size() == 2) {
86 // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
87 // RTCP sender where it sends RTCP SR before any RTP packets, which leads
88 // to a bogus NTP/RTP mapping.
wu@webrtc.org66773a02014-05-07 17:09:44 +000089 RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000090 return timestamp_in_ms;
91 }
92 return -1;
93 }
94
95 private:
wu@webrtc.org66773a02014-05-07 17:09:44 +000096 void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
Peter Boströmf2f82832015-05-01 13:00:41 +020097 rtc::CritScope lock(&crit_);
wu@webrtc.org66773a02014-05-07 17:09:44 +000098 for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
pbos@webrtc.org1d096902013-12-13 12:48:05 +000099 it != ntp_rtp_pairs_.end();
100 ++it) {
101 if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
102 ntp_rtp_pair.ntp_frac == it->ntp_frac) {
103 // This RTCP has already been added to the list.
104 return;
105 }
106 }
107 // We need two RTCP SR reports to map between RTP and NTP. More than two
108 // will not improve the mapping.
109 if (ntp_rtp_pairs_.size() == 2) {
110 ntp_rtp_pairs_.pop_back();
111 }
112 ntp_rtp_pairs_.push_front(ntp_rtp_pair);
113 }
114
Peter Boströmf2f82832015-05-01 13:00:41 +0200115 mutable rtc::CriticalSection crit_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000116 RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000117};
118
119class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
120 static const int kInSyncThresholdMs = 50;
121 static const int kStartupTimeMs = 2000;
122 static const int kMinRunTimeMs = 30000;
123
124 public:
125 VideoRtcpAndSyncObserver(Clock* clock,
126 int voe_channel,
127 VoEVideoSync* voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000128 SyncRtcpObserver* audio_observer)
stefanf116bd02015-10-27 08:29:42 -0700129 : clock_(clock),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000130 voe_channel_(voe_channel),
131 voe_sync_(voe_sync),
132 audio_observer_(audio_observer),
133 creation_time_ms_(clock_->TimeInMilliseconds()),
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000134 first_time_in_sync_(-1) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000135
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700136 void RenderFrame(const VideoFrame& video_frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 int time_to_render_ms) override {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000138 int64_t now_ms = clock_->TimeInMilliseconds();
139 uint32_t playout_timestamp = 0;
140 if (voe_sync_->GetPlayoutTimestamp(voe_channel_, playout_timestamp) != 0)
141 return;
142 int64_t latest_audio_ntp =
143 audio_observer_->RtpTimestampToNtp(playout_timestamp);
144 int64_t latest_video_ntp = RtpTimestampToNtp(video_frame.timestamp());
145 if (latest_audio_ntp < 0 || latest_video_ntp < 0)
146 return;
147 int time_until_render_ms =
148 std::max(0, static_cast<int>(video_frame.render_time_ms() - now_ms));
149 latest_video_ntp += time_until_render_ms;
150 int64_t stream_offset = latest_audio_ntp - latest_video_ntp;
151 std::stringstream ss;
152 ss << stream_offset;
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000153 webrtc::test::PrintResult("stream_offset",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000154 "",
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000155 "synchronization",
156 ss.str(),
157 "ms",
158 false);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000159 int64_t time_since_creation = now_ms - creation_time_ms_;
160 // During the first couple of seconds audio and video can falsely be
161 // estimated as being synchronized. We don't want to trigger on those.
162 if (time_since_creation < kStartupTimeMs)
163 return;
pbos@webrtc.orgb5f30292014-03-13 08:53:39 +0000164 if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000165 if (first_time_in_sync_ == -1) {
166 first_time_in_sync_ = now_ms;
167 webrtc::test::PrintResult("sync_convergence_time",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000168 "",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000169 "synchronization",
170 time_since_creation,
171 "ms",
172 false);
173 }
174 if (time_since_creation > kMinRunTimeMs)
175 observation_complete_->Set();
176 }
177 }
178
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000180
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000181 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000182 Clock* const clock_;
stefanf116bd02015-10-27 08:29:42 -0700183 const int voe_channel_;
184 VoEVideoSync* const voe_sync_;
185 SyncRtcpObserver* const audio_observer_;
186 const int64_t creation_time_ms_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000187 int64_t first_time_in_sync_;
188};
189
pbos8fc7fa72015-07-15 08:02:58 -0700190void CallPerfTest::TestAudioVideoSync(bool fec, bool create_audio_first) {
191 const char* kSyncGroup = "av_sync";
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000192 class AudioPacketReceiver : public PacketReceiver {
193 public:
194 AudioPacketReceiver(int channel, VoENetwork* voe_network)
195 : channel_(channel),
196 voe_network_(voe_network),
197 parser_(RtpHeaderParser::Create()) {}
stefan68786d22015-09-08 05:36:15 -0700198 DeliveryStatus DeliverPacket(MediaType media_type,
199 const uint8_t* packet,
200 size_t length,
201 const PacketTime& packet_time) 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
stefanf116bd02015-10-27 08:29:42 -0700233 SyncRtcpObserver audio_observer;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000234
solenberg4fbae2b2015-08-28 04:07:10 -0700235 Call::Config receiver_config;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000236 receiver_config.voice_engine = voice_engine;
solenberg4fbae2b2015-08-28 04:07:10 -0700237 CreateCalls(Call::Config(), receiver_config);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000238
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000239 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
240 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
241
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000242 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000243
stefanf116bd02015-10-27 08:29:42 -0700244 FakeNetworkPipe::Config net_config;
245 net_config.queue_delay_ms = 500;
246 net_config.loss_percent = 5;
247 test::PacketTransport audio_send_transport(
248 nullptr, &audio_observer, test::PacketTransport::kSender, net_config);
249 audio_send_transport.SetReceiver(&voe_packet_receiver);
250 test::PacketTransport audio_receive_transport(
251 nullptr, &audio_observer, test::PacketTransport::kReceiver, net_config);
252 audio_receive_transport.SetReceiver(&voe_packet_receiver);
253
254 internal::TransportAdapter transport_adapter(&audio_send_transport);
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000255 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000256 EXPECT_EQ(0,
257 voe_network->RegisterExternalTransport(channel, transport_adapter));
258
stefanf116bd02015-10-27 08:29:42 -0700259 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), channel,
260 voe_sync, &audio_observer);
261
262 test::PacketTransport sync_send_transport(sender_call_.get(), &observer,
263 test::PacketTransport::kSender,
264 FakeNetworkPipe::Config());
265 sync_send_transport.SetReceiver(receiver_call_->Receiver());
266 test::PacketTransport sync_receive_transport(receiver_call_.get(), &observer,
267 test::PacketTransport::kReceiver,
268 FakeNetworkPipe::Config());
269 sync_receive_transport.SetReceiver(sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000270
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000271 test::FakeDecoder fake_decoder;
272
stefanf116bd02015-10-27 08:29:42 -0700273 CreateSendConfig(1, &sync_send_transport);
274 CreateMatchingReceiveConfigs(&sync_receive_transport);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000275
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000276 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
277 if (fec) {
278 send_config_.rtp.fec.red_payload_type = kRedPayloadType;
279 send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
280 receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
281 receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
282 }
283 receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000284 receive_configs_[0].renderer = &observer;
pbos8fc7fa72015-07-15 08:02:58 -0700285 receive_configs_[0].sync_group = kSyncGroup;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000286
pbos8fc7fa72015-07-15 08:02:58 -0700287 AudioReceiveStream::Config audio_config;
288 audio_config.voe_channel_id = channel;
289 audio_config.sync_group = kSyncGroup;
290
291 AudioReceiveStream* audio_receive_stream = nullptr;
292
293 if (create_audio_first) {
294 audio_receive_stream =
295 receiver_call_->CreateAudioReceiveStream(audio_config);
296 CreateStreams();
297 } else {
298 CreateStreams();
299 audio_receive_stream =
300 receiver_call_->CreateAudioReceiveStream(audio_config);
301 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000302
303 CreateFrameGeneratorCapturer();
304
305 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000306
307 fake_audio_device.Start();
308 EXPECT_EQ(0, voe_base->StartPlayout(channel));
309 EXPECT_EQ(0, voe_base->StartReceive(channel));
310 EXPECT_EQ(0, voe_base->StartSend(channel));
311
312 EXPECT_EQ(kEventSignaled, observer.Wait())
313 << "Timed out while waiting for audio and video to be synchronized.";
314
315 EXPECT_EQ(0, voe_base->StopSend(channel));
316 EXPECT_EQ(0, voe_base->StopReceive(channel));
317 EXPECT_EQ(0, voe_base->StopPlayout(channel));
318 fake_audio_device.Stop();
319
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000320 Stop();
stefanf116bd02015-10-27 08:29:42 -0700321 sync_send_transport.StopSending();
322 sync_receive_transport.StopSending();
323 audio_send_transport.StopSending();
324 audio_receive_transport.StopSending();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000325
326 voe_base->DeleteChannel(channel);
327 voe_base->Release();
328 voe_codec->Release();
329 voe_network->Release();
330 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000331
332 DestroyStreams();
333
pbos8fc7fa72015-07-15 08:02:58 -0700334 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
335
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200336 DestroyCalls();
337
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000338 VoiceEngine::Delete(voice_engine);
339}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000340
pbos8fc7fa72015-07-15 08:02:58 -0700341TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioCreatedFirst) {
342 TestAudioVideoSync(false, true);
343}
344
345TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoCreatedFirst) {
346 TestAudioVideoSync(false, false);
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000347}
348
349TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
pbos8fc7fa72015-07-15 08:02:58 -0700350 TestAudioVideoSync(true, false);
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000351}
352
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000353void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
354 int threshold_ms,
355 int start_time_ms,
356 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000357 class CaptureNtpTimeObserver : public test::EndToEndTest,
358 public VideoRenderer {
359 public:
stefanf116bd02015-10-27 08:29:42 -0700360 CaptureNtpTimeObserver(int threshold_ms, int start_time_ms, int run_time_ms)
361 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000362 clock_(Clock::GetRealTimeClock()),
363 threshold_ms_(threshold_ms),
364 start_time_ms_(start_time_ms),
365 run_time_ms_(run_time_ms),
366 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000367 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000368 rtp_start_timestamp_set_(false),
369 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000370
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000371 private:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700372 void RenderFrame(const VideoFrame& video_frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000373 int time_to_render_ms) override {
stefanf116bd02015-10-27 08:29:42 -0700374 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000375 if (video_frame.ntp_time_ms() <= 0) {
376 // Haven't got enough RTCP SR in order to calculate the capture ntp
377 // time.
378 return;
379 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000380
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000381 int64_t now_ms = clock_->TimeInMilliseconds();
382 int64_t time_since_creation = now_ms - creation_time_ms_;
383 if (time_since_creation < start_time_ms_) {
384 // Wait for |start_time_ms_| before start measuring.
385 return;
386 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000387
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000388 if (time_since_creation > run_time_ms_) {
389 observation_complete_->Set();
390 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000391
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000392 FrameCaptureTimeList::iterator iter =
393 capture_time_list_.find(video_frame.timestamp());
394 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000395
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000396 // The real capture time has been wrapped to uint32_t before converted
397 // to rtp timestamp in the sender side. So here we convert the estimated
398 // capture time to a uint32_t 90k timestamp also for comparing.
399 uint32_t estimated_capture_timestamp =
400 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
401 uint32_t real_capture_timestamp = iter->second;
402 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
403 time_offset_ms = time_offset_ms / 90;
404 std::stringstream ss;
405 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000406
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000407 webrtc::test::PrintResult(
408 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
409 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
410 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000411
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000412 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000413
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000414 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
stefanf116bd02015-10-27 08:29:42 -0700415 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000416 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000417 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000418
419 if (!rtp_start_timestamp_set_) {
420 // Calculate the rtp timestamp offset in order to calculate the real
421 // capture time.
422 uint32_t first_capture_timestamp =
423 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
424 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
425 rtp_start_timestamp_set_ = true;
426 }
427
428 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
429 capture_time_list_.insert(
430 capture_time_list_.end(),
431 std::make_pair(header.timestamp, capture_timestamp));
432 return SEND_PACKET;
433 }
434
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000435 void OnFrameGeneratorCapturerCreated(
436 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000437 capturer_ = frame_generator_capturer;
438 }
439
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000440 void ModifyConfigs(VideoSendStream::Config* send_config,
441 std::vector<VideoReceiveStream::Config>* receive_configs,
442 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000443 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000444 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000445 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000446 }
447
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000448 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000449 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
450 "estimated capture NTP time to be "
451 "within bounds.";
452 }
453
stefanf116bd02015-10-27 08:29:42 -0700454 rtc::CriticalSection crit_;
455 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000456 int threshold_ms_;
457 int start_time_ms_;
458 int run_time_ms_;
459 int64_t creation_time_ms_;
460 test::FrameGeneratorCapturer* capturer_;
461 bool rtp_start_timestamp_set_;
462 uint32_t rtp_start_timestamp_;
463 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
stefanf116bd02015-10-27 08:29:42 -0700464 FrameCaptureTimeList capture_time_list_ GUARDED_BY(&crit_);
465 } test(threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000466
stefanf116bd02015-10-27 08:29:42 -0700467 RunBaseTest(&test, net_config);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000468}
469
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000470TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000471 FakeNetworkPipe::Config net_config;
472 net_config.queue_delay_ms = 100;
473 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
474 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000475 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000476 const int kStartTimeMs = 10000;
477 const int kRunTimeMs = 20000;
478 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
479}
480
wu@webrtc.org0224c202014-05-05 17:42:43 +0000481TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000482 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000483 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000484 net_config.delay_standard_deviation_ms = 10;
485 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
486 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000487 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000488 const int kStartTimeMs = 10000;
489 const int kRunTimeMs = 20000;
490 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
491}
492
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000493void CallPerfTest::TestCpuOveruse(LoadObserver::Load tested_load,
494 int encode_delay_ms) {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000495 class LoadObserver : public test::SendTest, public webrtc::LoadObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000496 public:
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000497 LoadObserver(LoadObserver::Load tested_load, int encode_delay_ms)
498 : SendTest(kLongTimeoutMs),
499 tested_load_(tested_load),
500 encoder_(Clock::GetRealTimeClock(), encode_delay_ms) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000501
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000502 void OnLoadUpdate(Load load) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000503 if (load == tested_load_)
504 observation_complete_->Set();
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000505 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000506
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000507 void ModifyConfigs(VideoSendStream::Config* send_config,
508 std::vector<VideoReceiveStream::Config>* receive_configs,
509 VideoEncoderConfig* encoder_config) override {
solenberge5269742015-09-08 05:13:22 -0700510 send_config->overuse_callback = this;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000511 send_config->encoder_settings.encoder = &encoder_;
512 }
513
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000514 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000515 EXPECT_EQ(kEventSignaled, Wait())
516 << "Timed out before receiving an overuse callback.";
517 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000518
519 LoadObserver::Load tested_load_;
520 test::DelayedEncoder encoder_;
521 } test(tested_load, encode_delay_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000522
stefanf116bd02015-10-27 08:29:42 -0700523 RunBaseTest(&test, FakeNetworkPipe::Config());
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000524}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000525
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000526TEST_F(CallPerfTest, ReceivesCpuUnderuse) {
527 const int kEncodeDelayMs = 2;
528 TestCpuOveruse(LoadObserver::kUnderuse, kEncodeDelayMs);
529}
530
531TEST_F(CallPerfTest, ReceivesCpuOveruse) {
532 const int kEncodeDelayMs = 35;
533 TestCpuOveruse(LoadObserver::kOveruse, kEncodeDelayMs);
534}
535
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000536void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
537 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000538 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000539 static const int kMinAcceptableTransmitBitrate = 130;
540 static const int kMaxAcceptableTransmitBitrate = 170;
541 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700542 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700543 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000544 public:
545 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000546 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000547 send_stream_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000548 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000549 num_bitrate_observations_in_range_(0) {}
550
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000551 private:
stefanf116bd02015-10-27 08:29:42 -0700552 // TODO(holmer): Run this with a timer instead of once per packet.
553 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000554 VideoSendStream::Stats stats = send_stream_->GetStats();
555 if (stats.substreams.size() > 0) {
henrikg91d6ede2015-09-17 00:24:34 -0700556 RTC_DCHECK_EQ(1u, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000557 int bitrate_kbps =
558 stats.substreams.begin()->second.total_bitrate_bps / 1000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000559 if (bitrate_kbps > 0) {
560 test::PrintResult(
561 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000562 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
563 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000564 "bitrate_kbps",
565 static_cast<size_t>(bitrate_kbps),
566 "kbps",
567 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000568 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000569 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
570 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
571 ++num_bitrate_observations_in_range_;
572 }
573 } else {
574 // Expect bitrate stats to roughly match the max encode bitrate.
sprang867fb522015-08-03 04:38:41 -0700575 if (bitrate_kbps > (kMaxEncodeBitrateKbps -
576 kAcceptableBitrateErrorMargin / 2) &&
577 bitrate_kbps < (kMaxEncodeBitrateKbps +
578 kAcceptableBitrateErrorMargin / 2)) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000579 ++num_bitrate_observations_in_range_;
580 }
581 }
582 if (num_bitrate_observations_in_range_ ==
583 kNumBitrateObservationsInRange)
584 observation_complete_->Set();
585 }
586 }
stefanf116bd02015-10-27 08:29:42 -0700587 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000588 }
589
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000590 void OnStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000591 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000592 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000593 send_stream_ = send_stream;
594 }
595
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000596 void ModifyConfigs(VideoSendStream::Config* send_config,
597 std::vector<VideoReceiveStream::Config>* receive_configs,
598 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000599 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000600 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000601 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700602 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000603 }
604 }
605
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000606 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000607 EXPECT_EQ(kEventSignaled, Wait())
608 << "Timeout while waiting for send-bitrate stats.";
609 }
610
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000611 VideoSendStream* send_stream_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000612 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000613 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000614 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000615
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000616 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
stefanf116bd02015-10-27 08:29:42 -0700617 RunBaseTest(&test, FakeNetworkPipe::Config());
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000618}
619
620TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
621
622TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
623 TestMinTransmitBitrate(false);
624}
625
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000626TEST_F(CallPerfTest, KeepsHighBitrateWhenReconfiguringSender) {
627 static const uint32_t kInitialBitrateKbps = 400;
628 static const uint32_t kReconfigureThresholdKbps = 600;
629 static const uint32_t kPermittedReconfiguredBitrateDiffKbps = 100;
630
631 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
632 public:
633 BitrateObserver()
634 : EndToEndTest(kDefaultTimeoutMs),
635 FakeEncoder(Clock::GetRealTimeClock()),
636 time_to_reconfigure_(webrtc::EventWrapper::Create()),
sprang867fb522015-08-03 04:38:41 -0700637 encoder_inits_(0),
638 last_set_bitrate_(0),
639 send_stream_(nullptr) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000640
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000641 int32_t InitEncode(const VideoCodec* config,
642 int32_t number_of_cores,
643 size_t max_payload_size) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000644 if (encoder_inits_ == 0) {
645 EXPECT_EQ(kInitialBitrateKbps, config->startBitrate)
646 << "Encoder not initialized at expected bitrate.";
647 }
648 ++encoder_inits_;
649 if (encoder_inits_ == 2) {
650 EXPECT_GE(last_set_bitrate_, kReconfigureThresholdKbps);
651 EXPECT_NEAR(config->startBitrate,
652 last_set_bitrate_,
653 kPermittedReconfiguredBitrateDiffKbps)
654 << "Encoder reconfigured with bitrate too far away from last set.";
655 observation_complete_->Set();
656 }
657 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
658 }
659
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000660 int32_t SetRates(uint32_t new_target_bitrate_kbps,
661 uint32_t framerate) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000662 last_set_bitrate_ = new_target_bitrate_kbps;
663 if (encoder_inits_ == 1 &&
664 new_target_bitrate_kbps > kReconfigureThresholdKbps) {
665 time_to_reconfigure_->Set();
666 }
667 return FakeEncoder::SetRates(new_target_bitrate_kbps, framerate);
668 }
669
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000670 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000671 Call::Config config = EndToEndTest::GetSenderCallConfig();
Stefan Holmere5904162015-03-26 11:11:06 +0100672 config.bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000673 return config;
674 }
675
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000676 void ModifyConfigs(VideoSendStream::Config* send_config,
677 std::vector<VideoReceiveStream::Config>* receive_configs,
678 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000679 send_config->encoder_settings.encoder = this;
680 encoder_config->streams[0].min_bitrate_bps = 50000;
681 encoder_config->streams[0].target_bitrate_bps =
682 encoder_config->streams[0].max_bitrate_bps = 2000000;
683
684 encoder_config_ = *encoder_config;
685 }
686
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000687 void OnStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000688 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000689 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000690 send_stream_ = send_stream;
691 }
692
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000693 void PerformTest() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000694 ASSERT_EQ(kEventSignaled, time_to_reconfigure_->Wait(kDefaultTimeoutMs))
695 << "Timed out before receiving an initial high bitrate.";
696 encoder_config_.streams[0].width *= 2;
697 encoder_config_.streams[0].height *= 2;
698 EXPECT_TRUE(send_stream_->ReconfigureVideoEncoder(encoder_config_));
699 EXPECT_EQ(kEventSignaled, Wait())
700 << "Timed out while waiting for a couple of high bitrate estimates "
701 "after reconfiguring the send stream.";
702 }
703
704 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000705 rtc::scoped_ptr<webrtc::EventWrapper> time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000706 int encoder_inits_;
707 uint32_t last_set_bitrate_;
708 VideoSendStream* send_stream_;
709 VideoEncoderConfig encoder_config_;
710 } test;
711
stefanf116bd02015-10-27 08:29:42 -0700712 RunBaseTest(&test, FakeNetworkPipe::Config());
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000713}
714
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000715} // namespace webrtc