blob: 44ecae325ef884d1ca65b94a319c6a8ab2df8aab [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"
kjellander3e6db232015-11-26 04:44:54 -080021#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010022#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000023#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010024#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
25#include "webrtc/system_wrappers/include/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
solenberg566ef242015-11-06 15:34:49 -0800235 AudioState::Config audio_state_config;
236 audio_state_config.voice_engine = voice_engine;
solenberg4fbae2b2015-08-28 04:07:10 -0700237 Call::Config receiver_config;
solenberg566ef242015-11-06 15:34:49 -0800238 receiver_config.audio_state = AudioState::Create(audio_state_config);
solenberg4fbae2b2015-08-28 04:07:10 -0700239 CreateCalls(Call::Config(), receiver_config);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000240
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000241 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
242 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
243
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000244 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000245
stefanf116bd02015-10-27 08:29:42 -0700246 FakeNetworkPipe::Config net_config;
247 net_config.queue_delay_ms = 500;
248 net_config.loss_percent = 5;
249 test::PacketTransport audio_send_transport(
250 nullptr, &audio_observer, test::PacketTransport::kSender, net_config);
251 audio_send_transport.SetReceiver(&voe_packet_receiver);
252 test::PacketTransport audio_receive_transport(
253 nullptr, &audio_observer, test::PacketTransport::kReceiver, net_config);
254 audio_receive_transport.SetReceiver(&voe_packet_receiver);
255
256 internal::TransportAdapter transport_adapter(&audio_send_transport);
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000257 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000258 EXPECT_EQ(0,
259 voe_network->RegisterExternalTransport(channel, transport_adapter));
260
stefanf116bd02015-10-27 08:29:42 -0700261 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), channel,
262 voe_sync, &audio_observer);
263
264 test::PacketTransport sync_send_transport(sender_call_.get(), &observer,
265 test::PacketTransport::kSender,
266 FakeNetworkPipe::Config());
267 sync_send_transport.SetReceiver(receiver_call_->Receiver());
268 test::PacketTransport sync_receive_transport(receiver_call_.get(), &observer,
269 test::PacketTransport::kReceiver,
270 FakeNetworkPipe::Config());
271 sync_receive_transport.SetReceiver(sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000272
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000273 test::FakeDecoder fake_decoder;
274
stefanf116bd02015-10-27 08:29:42 -0700275 CreateSendConfig(1, &sync_send_transport);
276 CreateMatchingReceiveConfigs(&sync_receive_transport);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000277
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000278 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
279 if (fec) {
280 send_config_.rtp.fec.red_payload_type = kRedPayloadType;
281 send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
282 receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
283 receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
284 }
285 receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000286 receive_configs_[0].renderer = &observer;
pbos8fc7fa72015-07-15 08:02:58 -0700287 receive_configs_[0].sync_group = kSyncGroup;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000288
pbos8fc7fa72015-07-15 08:02:58 -0700289 AudioReceiveStream::Config audio_config;
290 audio_config.voe_channel_id = channel;
291 audio_config.sync_group = kSyncGroup;
292
293 AudioReceiveStream* audio_receive_stream = nullptr;
294
295 if (create_audio_first) {
296 audio_receive_stream =
297 receiver_call_->CreateAudioReceiveStream(audio_config);
298 CreateStreams();
299 } else {
300 CreateStreams();
301 audio_receive_stream =
302 receiver_call_->CreateAudioReceiveStream(audio_config);
303 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000304
305 CreateFrameGeneratorCapturer();
306
307 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000308
309 fake_audio_device.Start();
310 EXPECT_EQ(0, voe_base->StartPlayout(channel));
311 EXPECT_EQ(0, voe_base->StartReceive(channel));
312 EXPECT_EQ(0, voe_base->StartSend(channel));
313
314 EXPECT_EQ(kEventSignaled, observer.Wait())
315 << "Timed out while waiting for audio and video to be synchronized.";
316
317 EXPECT_EQ(0, voe_base->StopSend(channel));
318 EXPECT_EQ(0, voe_base->StopReceive(channel));
319 EXPECT_EQ(0, voe_base->StopPlayout(channel));
320 fake_audio_device.Stop();
321
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000322 Stop();
stefanf116bd02015-10-27 08:29:42 -0700323 sync_send_transport.StopSending();
324 sync_receive_transport.StopSending();
325 audio_send_transport.StopSending();
326 audio_receive_transport.StopSending();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000327
328 voe_base->DeleteChannel(channel);
329 voe_base->Release();
330 voe_codec->Release();
331 voe_network->Release();
332 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000333
334 DestroyStreams();
335
pbos8fc7fa72015-07-15 08:02:58 -0700336 receiver_call_->DestroyAudioReceiveStream(audio_receive_stream);
337
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200338 DestroyCalls();
339
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000340 VoiceEngine::Delete(voice_engine);
341}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000342
pbos8fc7fa72015-07-15 08:02:58 -0700343TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithAudioCreatedFirst) {
344 TestAudioVideoSync(false, true);
345}
346
347TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithVideoCreatedFirst) {
348 TestAudioVideoSync(false, false);
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000349}
350
351TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
pbos8fc7fa72015-07-15 08:02:58 -0700352 TestAudioVideoSync(true, false);
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000353}
354
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000355void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
356 int threshold_ms,
357 int start_time_ms,
358 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000359 class CaptureNtpTimeObserver : public test::EndToEndTest,
360 public VideoRenderer {
361 public:
stefanf116bd02015-10-27 08:29:42 -0700362 CaptureNtpTimeObserver(int threshold_ms, int start_time_ms, int run_time_ms)
363 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000364 clock_(Clock::GetRealTimeClock()),
365 threshold_ms_(threshold_ms),
366 start_time_ms_(start_time_ms),
367 run_time_ms_(run_time_ms),
368 creation_time_ms_(clock_->TimeInMilliseconds()),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000369 capturer_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000370 rtp_start_timestamp_set_(false),
371 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000372
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000373 private:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700374 void RenderFrame(const VideoFrame& video_frame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000375 int time_to_render_ms) override {
stefanf116bd02015-10-27 08:29:42 -0700376 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000377 if (video_frame.ntp_time_ms() <= 0) {
378 // Haven't got enough RTCP SR in order to calculate the capture ntp
379 // time.
380 return;
381 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000382
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000383 int64_t now_ms = clock_->TimeInMilliseconds();
384 int64_t time_since_creation = now_ms - creation_time_ms_;
385 if (time_since_creation < start_time_ms_) {
386 // Wait for |start_time_ms_| before start measuring.
387 return;
388 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000389
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000390 if (time_since_creation > run_time_ms_) {
391 observation_complete_->Set();
392 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000393
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000394 FrameCaptureTimeList::iterator iter =
395 capture_time_list_.find(video_frame.timestamp());
396 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000397
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000398 // The real capture time has been wrapped to uint32_t before converted
399 // to rtp timestamp in the sender side. So here we convert the estimated
400 // capture time to a uint32_t 90k timestamp also for comparing.
401 uint32_t estimated_capture_timestamp =
402 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
403 uint32_t real_capture_timestamp = iter->second;
404 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
405 time_offset_ms = time_offset_ms / 90;
406 std::stringstream ss;
407 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000408
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000409 webrtc::test::PrintResult(
410 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
411 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
412 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000413
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000414 bool IsTextureSupported() const override { return false; }
pbos@webrtc.org0d852d52015-02-09 15:14:36 +0000415
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000416 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
stefanf116bd02015-10-27 08:29:42 -0700417 rtc::CritScope lock(&crit_);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000418 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000419 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000420
421 if (!rtp_start_timestamp_set_) {
422 // Calculate the rtp timestamp offset in order to calculate the real
423 // capture time.
424 uint32_t first_capture_timestamp =
425 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
426 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
427 rtp_start_timestamp_set_ = true;
428 }
429
430 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
431 capture_time_list_.insert(
432 capture_time_list_.end(),
433 std::make_pair(header.timestamp, capture_timestamp));
434 return SEND_PACKET;
435 }
436
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000437 void OnFrameGeneratorCapturerCreated(
438 test::FrameGeneratorCapturer* frame_generator_capturer) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000439 capturer_ = frame_generator_capturer;
440 }
441
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000442 void ModifyConfigs(VideoSendStream::Config* send_config,
443 std::vector<VideoReceiveStream::Config>* receive_configs,
444 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000445 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000446 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000447 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000448 }
449
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000450 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000451 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
452 "estimated capture NTP time to be "
453 "within bounds.";
454 }
455
stefanf116bd02015-10-27 08:29:42 -0700456 rtc::CriticalSection crit_;
457 Clock* const clock_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000458 int threshold_ms_;
459 int start_time_ms_;
460 int run_time_ms_;
461 int64_t creation_time_ms_;
462 test::FrameGeneratorCapturer* capturer_;
463 bool rtp_start_timestamp_set_;
464 uint32_t rtp_start_timestamp_;
465 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
stefanf116bd02015-10-27 08:29:42 -0700466 FrameCaptureTimeList capture_time_list_ GUARDED_BY(&crit_);
467 } test(threshold_ms, start_time_ms, run_time_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000468
stefanf116bd02015-10-27 08:29:42 -0700469 RunBaseTest(&test, net_config);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000470}
471
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000472TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000473 FakeNetworkPipe::Config net_config;
474 net_config.queue_delay_ms = 100;
475 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
476 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000477 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000478 const int kStartTimeMs = 10000;
479 const int kRunTimeMs = 20000;
480 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
481}
482
wu@webrtc.org0224c202014-05-05 17:42:43 +0000483TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000484 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000485 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000486 net_config.delay_standard_deviation_ms = 10;
487 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
488 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000489 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000490 const int kStartTimeMs = 10000;
491 const int kRunTimeMs = 20000;
492 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
493}
494
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000495void CallPerfTest::TestCpuOveruse(LoadObserver::Load tested_load,
496 int encode_delay_ms) {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000497 class LoadObserver : public test::SendTest, public webrtc::LoadObserver {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000498 public:
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000499 LoadObserver(LoadObserver::Load tested_load, int encode_delay_ms)
500 : SendTest(kLongTimeoutMs),
501 tested_load_(tested_load),
502 encoder_(Clock::GetRealTimeClock(), encode_delay_ms) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000503
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000504 void OnLoadUpdate(Load load) override {
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000505 if (load == tested_load_)
506 observation_complete_->Set();
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000507 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000508
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000509 void ModifyConfigs(VideoSendStream::Config* send_config,
510 std::vector<VideoReceiveStream::Config>* receive_configs,
511 VideoEncoderConfig* encoder_config) override {
solenberge5269742015-09-08 05:13:22 -0700512 send_config->overuse_callback = this;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000513 send_config->encoder_settings.encoder = &encoder_;
514 }
515
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000516 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000517 EXPECT_EQ(kEventSignaled, Wait())
518 << "Timed out before receiving an overuse callback.";
519 }
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000520
521 LoadObserver::Load tested_load_;
522 test::DelayedEncoder encoder_;
523 } test(tested_load, encode_delay_ms);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000524
stefanf116bd02015-10-27 08:29:42 -0700525 RunBaseTest(&test, FakeNetworkPipe::Config());
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000526}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000527
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000528TEST_F(CallPerfTest, ReceivesCpuUnderuse) {
529 const int kEncodeDelayMs = 2;
530 TestCpuOveruse(LoadObserver::kUnderuse, kEncodeDelayMs);
531}
532
533TEST_F(CallPerfTest, ReceivesCpuOveruse) {
534 const int kEncodeDelayMs = 35;
535 TestCpuOveruse(LoadObserver::kOveruse, kEncodeDelayMs);
536}
537
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000538void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
539 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000540 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000541 static const int kMinAcceptableTransmitBitrate = 130;
542 static const int kMaxAcceptableTransmitBitrate = 170;
543 static const int kNumBitrateObservationsInRange = 100;
sprang867fb522015-08-03 04:38:41 -0700544 static const int kAcceptableBitrateErrorMargin = 15; // +- 7
stefanf116bd02015-10-27 08:29:42 -0700545 class BitrateObserver : public test::EndToEndTest {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000546 public:
547 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000548 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000549 send_stream_(nullptr),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000550 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000551 num_bitrate_observations_in_range_(0) {}
552
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000553 private:
stefanf116bd02015-10-27 08:29:42 -0700554 // TODO(holmer): Run this with a timer instead of once per packet.
555 Action OnSendRtp(const uint8_t* packet, size_t length) override {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000556 VideoSendStream::Stats stats = send_stream_->GetStats();
557 if (stats.substreams.size() > 0) {
henrikg91d6ede2015-09-17 00:24:34 -0700558 RTC_DCHECK_EQ(1u, stats.substreams.size());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000559 int bitrate_kbps =
560 stats.substreams.begin()->second.total_bitrate_bps / 1000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000561 if (bitrate_kbps > 0) {
562 test::PrintResult(
563 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000564 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
565 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000566 "bitrate_kbps",
567 static_cast<size_t>(bitrate_kbps),
568 "kbps",
569 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000570 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000571 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
572 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
573 ++num_bitrate_observations_in_range_;
574 }
575 } else {
576 // Expect bitrate stats to roughly match the max encode bitrate.
sprang867fb522015-08-03 04:38:41 -0700577 if (bitrate_kbps > (kMaxEncodeBitrateKbps -
578 kAcceptableBitrateErrorMargin / 2) &&
579 bitrate_kbps < (kMaxEncodeBitrateKbps +
580 kAcceptableBitrateErrorMargin / 2)) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000581 ++num_bitrate_observations_in_range_;
582 }
583 }
584 if (num_bitrate_observations_in_range_ ==
585 kNumBitrateObservationsInRange)
586 observation_complete_->Set();
587 }
588 }
stefanf116bd02015-10-27 08:29:42 -0700589 return SEND_PACKET;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000590 }
591
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000592 void OnStreamsCreated(
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000593 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000594 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000595 send_stream_ = send_stream;
596 }
597
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000598 void ModifyConfigs(VideoSendStream::Config* send_config,
599 std::vector<VideoReceiveStream::Config>* receive_configs,
600 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000601 if (pad_to_min_bitrate_) {
pbos@webrtc.orgad3b5a52014-10-24 09:23:21 +0000602 encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000603 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700604 RTC_DCHECK_EQ(0, encoder_config->min_transmit_bitrate_bps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000605 }
606 }
607
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000608 void PerformTest() override {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000609 EXPECT_EQ(kEventSignaled, Wait())
610 << "Timeout while waiting for send-bitrate stats.";
611 }
612
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000613 VideoSendStream* send_stream_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000614 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000615 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000616 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000617
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000618 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
stefanf116bd02015-10-27 08:29:42 -0700619 RunBaseTest(&test, FakeNetworkPipe::Config());
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000620}
621
622TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
623
624TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
625 TestMinTransmitBitrate(false);
626}
627
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000628TEST_F(CallPerfTest, KeepsHighBitrateWhenReconfiguringSender) {
629 static const uint32_t kInitialBitrateKbps = 400;
630 static const uint32_t kReconfigureThresholdKbps = 600;
631 static const uint32_t kPermittedReconfiguredBitrateDiffKbps = 100;
632
633 class BitrateObserver : public test::EndToEndTest, public test::FakeEncoder {
634 public:
635 BitrateObserver()
636 : EndToEndTest(kDefaultTimeoutMs),
637 FakeEncoder(Clock::GetRealTimeClock()),
638 time_to_reconfigure_(webrtc::EventWrapper::Create()),
sprang867fb522015-08-03 04:38:41 -0700639 encoder_inits_(0),
640 last_set_bitrate_(0),
641 send_stream_(nullptr) {}
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000642
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000643 int32_t InitEncode(const VideoCodec* config,
644 int32_t number_of_cores,
645 size_t max_payload_size) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000646 if (encoder_inits_ == 0) {
647 EXPECT_EQ(kInitialBitrateKbps, config->startBitrate)
648 << "Encoder not initialized at expected bitrate.";
649 }
650 ++encoder_inits_;
651 if (encoder_inits_ == 2) {
652 EXPECT_GE(last_set_bitrate_, kReconfigureThresholdKbps);
653 EXPECT_NEAR(config->startBitrate,
654 last_set_bitrate_,
655 kPermittedReconfiguredBitrateDiffKbps)
656 << "Encoder reconfigured with bitrate too far away from last set.";
657 observation_complete_->Set();
658 }
659 return FakeEncoder::InitEncode(config, number_of_cores, max_payload_size);
660 }
661
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000662 int32_t SetRates(uint32_t new_target_bitrate_kbps,
663 uint32_t framerate) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000664 last_set_bitrate_ = new_target_bitrate_kbps;
665 if (encoder_inits_ == 1 &&
666 new_target_bitrate_kbps > kReconfigureThresholdKbps) {
667 time_to_reconfigure_->Set();
668 }
669 return FakeEncoder::SetRates(new_target_bitrate_kbps, framerate);
670 }
671
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000672 Call::Config GetSenderCallConfig() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000673 Call::Config config = EndToEndTest::GetSenderCallConfig();
Stefan Holmere5904162015-03-26 11:11:06 +0100674 config.bitrate_config.start_bitrate_bps = kInitialBitrateKbps * 1000;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000675 return config;
676 }
677
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000678 void ModifyConfigs(VideoSendStream::Config* send_config,
679 std::vector<VideoReceiveStream::Config>* receive_configs,
680 VideoEncoderConfig* encoder_config) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000681 send_config->encoder_settings.encoder = this;
682 encoder_config->streams[0].min_bitrate_bps = 50000;
683 encoder_config->streams[0].target_bitrate_bps =
684 encoder_config->streams[0].max_bitrate_bps = 2000000;
685
686 encoder_config_ = *encoder_config;
687 }
688
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000689 void OnStreamsCreated(
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000690 VideoSendStream* send_stream,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000691 const std::vector<VideoReceiveStream*>& receive_streams) override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000692 send_stream_ = send_stream;
693 }
694
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000695 void PerformTest() override {
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000696 ASSERT_EQ(kEventSignaled, time_to_reconfigure_->Wait(kDefaultTimeoutMs))
697 << "Timed out before receiving an initial high bitrate.";
698 encoder_config_.streams[0].width *= 2;
699 encoder_config_.streams[0].height *= 2;
700 EXPECT_TRUE(send_stream_->ReconfigureVideoEncoder(encoder_config_));
701 EXPECT_EQ(kEventSignaled, Wait())
702 << "Timed out while waiting for a couple of high bitrate estimates "
703 "after reconfiguring the send stream.";
704 }
705
706 private:
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000707 rtc::scoped_ptr<webrtc::EventWrapper> time_to_reconfigure_;
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000708 int encoder_inits_;
709 uint32_t last_set_bitrate_;
710 VideoSendStream* send_stream_;
711 VideoEncoderConfig encoder_config_;
712 } test;
713
stefanf116bd02015-10-27 08:29:42 -0700714 RunBaseTest(&test, FakeNetworkPipe::Config());
pbos@webrtc.org32452b22014-10-22 12:15:24 +0000715}
716
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000717} // namespace webrtc