blob: 3d1bf7f548eea7ba72242829d486520460b8e62d [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
18#include "webrtc/call.h"
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +000019#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000020#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
21#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
22#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
wu@webrtc.org66773a02014-05-07 17:09:44 +000023#include "webrtc/system_wrappers/interface/rtp_to_ntp.h"
pbos@webrtc.org1d096902013-12-13 12:48:05 +000024#include "webrtc/system_wrappers/interface/scoped_ptr.h"
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000025#include "webrtc/system_wrappers/interface/thread_annotations.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
kjellander@webrtc.org6f729e82014-09-02 15:13:55 +000044// Disabled on Android since all these tests currently fail (webrtc:3770).
45#ifndef WEBRTC_ANDROID
46
pbos@webrtc.org1d096902013-12-13 12:48:05 +000047namespace webrtc {
48
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000049class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000050 protected:
stefan@webrtc.org01581da2014-09-04 06:48:14 +000051 void TestAudioVideoSync(bool fec);
52
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000053 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
54
wu@webrtc.orgcd701192014-04-24 22:10:24 +000055 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
56 int threshold_ms,
57 int start_time_ms,
58 int run_time_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000059};
60
61class SyncRtcpObserver : public test::RtpRtcpObserver {
62 public:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000063 explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000064 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000065 crit_(CriticalSectionWrapper::CreateCriticalSection()) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000066
67 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
68 RTCPUtility::RTCPParserV2 parser(packet, length, true);
69 EXPECT_TRUE(parser.IsValid());
70
71 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
72 packet_type != RTCPUtility::kRtcpNotValidCode;
73 packet_type = parser.Iterate()) {
74 if (packet_type == RTCPUtility::kRtcpSrCode) {
75 const RTCPUtility::RTCPPacket& packet = parser.Packet();
wu@webrtc.org66773a02014-05-07 17:09:44 +000076 RtcpMeasurement ntp_rtp_pair(
pbos@webrtc.org1d096902013-12-13 12:48:05 +000077 packet.SR.NTPMostSignificant,
78 packet.SR.NTPLeastSignificant,
79 packet.SR.RTPTimestamp);
80 StoreNtpRtpPair(ntp_rtp_pair);
81 }
82 }
83 return SEND_PACKET;
84 }
85
86 int64_t RtpTimestampToNtp(uint32_t timestamp) const {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000087 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +000088 int64_t timestamp_in_ms = -1;
89 if (ntp_rtp_pairs_.size() == 2) {
90 // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
91 // RTCP sender where it sends RTCP SR before any RTP packets, which leads
92 // to a bogus NTP/RTP mapping.
wu@webrtc.org66773a02014-05-07 17:09:44 +000093 RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000094 return timestamp_in_ms;
95 }
96 return -1;
97 }
98
99 private:
wu@webrtc.org66773a02014-05-07 17:09:44 +0000100 void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000101 CriticalSectionScoped lock(crit_.get());
wu@webrtc.org66773a02014-05-07 17:09:44 +0000102 for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000103 it != ntp_rtp_pairs_.end();
104 ++it) {
105 if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
106 ntp_rtp_pair.ntp_frac == it->ntp_frac) {
107 // This RTCP has already been added to the list.
108 return;
109 }
110 }
111 // We need two RTCP SR reports to map between RTP and NTP. More than two
112 // will not improve the mapping.
113 if (ntp_rtp_pairs_.size() == 2) {
114 ntp_rtp_pairs_.pop_back();
115 }
116 ntp_rtp_pairs_.push_front(ntp_rtp_pair);
117 }
118
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000119 const scoped_ptr<CriticalSectionWrapper> crit_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000120 RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000121};
122
123class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
124 static const int kInSyncThresholdMs = 50;
125 static const int kStartupTimeMs = 2000;
126 static const int kMinRunTimeMs = 30000;
127
128 public:
129 VideoRtcpAndSyncObserver(Clock* clock,
130 int voe_channel,
131 VoEVideoSync* voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000132 SyncRtcpObserver* audio_observer)
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000133 : SyncRtcpObserver(FakeNetworkPipe::Config()),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000134 clock_(clock),
135 voe_channel_(voe_channel),
136 voe_sync_(voe_sync),
137 audio_observer_(audio_observer),
138 creation_time_ms_(clock_->TimeInMilliseconds()),
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000139 first_time_in_sync_(-1) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000140
141 virtual void RenderFrame(const I420VideoFrame& video_frame,
142 int time_to_render_ms) OVERRIDE {
143 int64_t now_ms = clock_->TimeInMilliseconds();
144 uint32_t playout_timestamp = 0;
145 if (voe_sync_->GetPlayoutTimestamp(voe_channel_, playout_timestamp) != 0)
146 return;
147 int64_t latest_audio_ntp =
148 audio_observer_->RtpTimestampToNtp(playout_timestamp);
149 int64_t latest_video_ntp = RtpTimestampToNtp(video_frame.timestamp());
150 if (latest_audio_ntp < 0 || latest_video_ntp < 0)
151 return;
152 int time_until_render_ms =
153 std::max(0, static_cast<int>(video_frame.render_time_ms() - now_ms));
154 latest_video_ntp += time_until_render_ms;
155 int64_t stream_offset = latest_audio_ntp - latest_video_ntp;
156 std::stringstream ss;
157 ss << stream_offset;
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000158 webrtc::test::PrintResult("stream_offset",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000159 "",
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000160 "synchronization",
161 ss.str(),
162 "ms",
163 false);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000164 int64_t time_since_creation = now_ms - creation_time_ms_;
165 // During the first couple of seconds audio and video can falsely be
166 // estimated as being synchronized. We don't want to trigger on those.
167 if (time_since_creation < kStartupTimeMs)
168 return;
pbos@webrtc.orgb5f30292014-03-13 08:53:39 +0000169 if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000170 if (first_time_in_sync_ == -1) {
171 first_time_in_sync_ = now_ms;
172 webrtc::test::PrintResult("sync_convergence_time",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000173 "",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000174 "synchronization",
175 time_since_creation,
176 "ms",
177 false);
178 }
179 if (time_since_creation > kMinRunTimeMs)
180 observation_complete_->Set();
181 }
182 }
183
184 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000185 Clock* const clock_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000186 int voe_channel_;
187 VoEVideoSync* voe_sync_;
188 SyncRtcpObserver* audio_observer_;
189 int64_t creation_time_ms_;
190 int64_t first_time_in_sync_;
191};
192
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000193void CallPerfTest::TestAudioVideoSync(bool fec) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000194 class AudioPacketReceiver : public PacketReceiver {
195 public:
196 AudioPacketReceiver(int channel, VoENetwork* voe_network)
197 : channel_(channel),
198 voe_network_(voe_network),
199 parser_(RtpHeaderParser::Create()) {}
200 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
201 size_t length) OVERRIDE {
202 int ret;
203 if (parser_->IsRtcp(packet, static_cast<int>(length))) {
204 ret = voe_network_->ReceivedRTCPPacket(
205 channel_, packet, static_cast<unsigned int>(length));
206 } else {
207 ret = voe_network_->ReceivedRTPPacket(
208 channel_, packet, static_cast<unsigned int>(length), PacketTime());
209 }
210 return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
211 }
212
213 private:
214 int channel_;
215 VoENetwork* voe_network_;
216 scoped_ptr<RtpHeaderParser> parser_;
217 };
218
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000219 VoiceEngine* voice_engine = VoiceEngine::Create();
220 VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
221 VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
222 VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine);
223 VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine);
224 const std::string audio_filename =
225 test::ResourcePath("voice_engine/audio_long16", "pcm");
226 ASSERT_STRNE("", audio_filename.c_str());
227 test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(),
228 audio_filename);
229 EXPECT_EQ(0, voe_base->Init(&fake_audio_device, NULL));
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000230 int channel = voe_base->CreateChannel();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000231
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000232 FakeNetworkPipe::Config net_config;
233 net_config.queue_delay_ms = 500;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000234 net_config.loss_percent = 5;
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000235 SyncRtcpObserver audio_observer(net_config);
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000236 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
237 channel,
238 voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000239 &audio_observer);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000240
241 Call::Config receiver_config(observer.ReceiveTransport());
242 receiver_config.voice_engine = voice_engine;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000243 CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
244
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000245 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
246 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
247
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000248 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000249 audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
250
251 internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000252 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000253 EXPECT_EQ(0,
254 voe_network->RegisterExternalTransport(channel, transport_adapter));
255
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000256 observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000257
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000258 test::FakeDecoder fake_decoder;
259
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000260 CreateSendConfig(1);
261 CreateMatchingReceiveConfigs();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000262
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000263 send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
264 if (fec) {
265 send_config_.rtp.fec.red_payload_type = kRedPayloadType;
266 send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
267 receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
268 receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
269 }
270 receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000271 receive_configs_[0].renderer = &observer;
272 receive_configs_[0].audio_channel_id = channel;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000273
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000274 CreateStreams();
275
276 CreateFrameGeneratorCapturer();
277
278 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000279
280 fake_audio_device.Start();
281 EXPECT_EQ(0, voe_base->StartPlayout(channel));
282 EXPECT_EQ(0, voe_base->StartReceive(channel));
283 EXPECT_EQ(0, voe_base->StartSend(channel));
284
285 EXPECT_EQ(kEventSignaled, observer.Wait())
286 << "Timed out while waiting for audio and video to be synchronized.";
287
288 EXPECT_EQ(0, voe_base->StopSend(channel));
289 EXPECT_EQ(0, voe_base->StopReceive(channel));
290 EXPECT_EQ(0, voe_base->StopPlayout(channel));
291 fake_audio_device.Stop();
292
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000293 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000294 observer.StopSending();
295 audio_observer.StopSending();
296
297 voe_base->DeleteChannel(channel);
298 voe_base->Release();
299 voe_codec->Release();
300 voe_network->Release();
301 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000302
303 DestroyStreams();
304
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000305 VoiceEngine::Delete(voice_engine);
306}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000307
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000308TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
309 TestAudioVideoSync(false);
310}
311
312TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
313 TestAudioVideoSync(true);
314}
315
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000316void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
317 int threshold_ms,
318 int start_time_ms,
319 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000320 class CaptureNtpTimeObserver : public test::EndToEndTest,
321 public VideoRenderer {
322 public:
323 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
324 int threshold_ms,
325 int start_time_ms,
326 int run_time_ms)
327 : EndToEndTest(kLongTimeoutMs, config),
328 clock_(Clock::GetRealTimeClock()),
329 threshold_ms_(threshold_ms),
330 start_time_ms_(start_time_ms),
331 run_time_ms_(run_time_ms),
332 creation_time_ms_(clock_->TimeInMilliseconds()),
333 capturer_(NULL),
334 rtp_start_timestamp_set_(false),
335 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000336
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000337 private:
338 virtual void RenderFrame(const I420VideoFrame& video_frame,
339 int time_to_render_ms) OVERRIDE {
340 if (video_frame.ntp_time_ms() <= 0) {
341 // Haven't got enough RTCP SR in order to calculate the capture ntp
342 // time.
343 return;
344 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000345
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000346 int64_t now_ms = clock_->TimeInMilliseconds();
347 int64_t time_since_creation = now_ms - creation_time_ms_;
348 if (time_since_creation < start_time_ms_) {
349 // Wait for |start_time_ms_| before start measuring.
350 return;
351 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000352
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000353 if (time_since_creation > run_time_ms_) {
354 observation_complete_->Set();
355 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000356
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000357 FrameCaptureTimeList::iterator iter =
358 capture_time_list_.find(video_frame.timestamp());
359 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000360
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000361 // The real capture time has been wrapped to uint32_t before converted
362 // to rtp timestamp in the sender side. So here we convert the estimated
363 // capture time to a uint32_t 90k timestamp also for comparing.
364 uint32_t estimated_capture_timestamp =
365 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
366 uint32_t real_capture_timestamp = iter->second;
367 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
368 time_offset_ms = time_offset_ms / 90;
369 std::stringstream ss;
370 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000371
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000372 webrtc::test::PrintResult(
373 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
374 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
375 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000376
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000377 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
378 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000379 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000380
381 if (!rtp_start_timestamp_set_) {
382 // Calculate the rtp timestamp offset in order to calculate the real
383 // capture time.
384 uint32_t first_capture_timestamp =
385 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
386 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
387 rtp_start_timestamp_set_ = true;
388 }
389
390 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
391 capture_time_list_.insert(
392 capture_time_list_.end(),
393 std::make_pair(header.timestamp, capture_timestamp));
394 return SEND_PACKET;
395 }
396
397 virtual void OnFrameGeneratorCapturerCreated(
398 test::FrameGeneratorCapturer* frame_generator_capturer) OVERRIDE {
399 capturer_ = frame_generator_capturer;
400 }
401
402 virtual void ModifyConfigs(
403 VideoSendStream::Config* send_config,
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000404 std::vector<VideoReceiveStream::Config>* receive_configs,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000405 std::vector<VideoStream>* video_streams) OVERRIDE {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000406 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000407 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000408 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000409 }
410
411 virtual void PerformTest() OVERRIDE {
412 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
413 "estimated capture NTP time to be "
414 "within bounds.";
415 }
416
417 Clock* clock_;
418 int threshold_ms_;
419 int start_time_ms_;
420 int run_time_ms_;
421 int64_t creation_time_ms_;
422 test::FrameGeneratorCapturer* capturer_;
423 bool rtp_start_timestamp_set_;
424 uint32_t rtp_start_timestamp_;
425 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
426 FrameCaptureTimeList capture_time_list_;
427 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
428
429 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000430}
431
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000432TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000433 FakeNetworkPipe::Config net_config;
434 net_config.queue_delay_ms = 100;
435 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
436 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000437 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000438 const int kStartTimeMs = 10000;
439 const int kRunTimeMs = 20000;
440 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
441}
442
wu@webrtc.org0224c202014-05-05 17:42:43 +0000443TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000444 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000445 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000446 net_config.delay_standard_deviation_ms = 10;
447 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
448 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000449 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000450 const int kStartTimeMs = 10000;
451 const int kRunTimeMs = 20000;
452 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
453}
454
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000455TEST_F(CallPerfTest, RegisterCpuOveruseObserver) {
456 // Verifies that either a normal or overuse callback is triggered.
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000457 class OveruseCallbackObserver : public test::SendTest,
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000458 public webrtc::OveruseCallback {
459 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000460 OveruseCallbackObserver() : SendTest(kLongTimeoutMs) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000461
462 virtual void OnOveruse() OVERRIDE {
463 observation_complete_->Set();
464 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000465
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000466 virtual void OnNormalUse() OVERRIDE {
467 observation_complete_->Set();
468 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000469
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000470 virtual Call::Config GetSenderCallConfig() OVERRIDE {
471 Call::Config config(SendTransport());
472 config.overuse_callback = this;
473 return config;
474 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000475
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000476 virtual void PerformTest() OVERRIDE {
477 EXPECT_EQ(kEventSignaled, Wait())
478 << "Timed out before receiving an overuse callback.";
479 }
480 } test;
481
482 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000483}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000484
485void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
486 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000487 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000488 static const int kMinAcceptableTransmitBitrate = 130;
489 static const int kMaxAcceptableTransmitBitrate = 170;
490 static const int kNumBitrateObservationsInRange = 100;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000491 class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000492 public:
493 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000494 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000495 send_stream_(NULL),
496 send_transport_receiver_(NULL),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000497 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000498 num_bitrate_observations_in_range_(0) {}
499
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000500 private:
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000501 virtual void SetReceivers(PacketReceiver* send_transport_receiver,
502 PacketReceiver* receive_transport_receiver)
503 OVERRIDE {
504 send_transport_receiver_ = send_transport_receiver;
505 test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
506 }
507
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000508 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
509 size_t length) OVERRIDE {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000510 VideoSendStream::Stats stats = send_stream_->GetStats();
511 if (stats.substreams.size() > 0) {
512 assert(stats.substreams.size() == 1);
513 int bitrate_kbps = stats.substreams.begin()->second.bitrate_bps / 1000;
514 if (bitrate_kbps > 0) {
515 test::PrintResult(
516 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000517 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
518 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000519 "bitrate_kbps",
520 static_cast<size_t>(bitrate_kbps),
521 "kbps",
522 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000523 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000524 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
525 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
526 ++num_bitrate_observations_in_range_;
527 }
528 } else {
529 // Expect bitrate stats to roughly match the max encode bitrate.
530 if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 &&
531 bitrate_kbps < kMaxEncodeBitrateKbps + 5) {
532 ++num_bitrate_observations_in_range_;
533 }
534 }
535 if (num_bitrate_observations_in_range_ ==
536 kNumBitrateObservationsInRange)
537 observation_complete_->Set();
538 }
539 }
540 return send_transport_receiver_->DeliverPacket(packet, length);
541 }
542
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000543 virtual void OnStreamsCreated(
544 VideoSendStream* send_stream,
545 const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000546 send_stream_ = send_stream;
547 }
548
549 virtual void ModifyConfigs(
550 VideoSendStream::Config* send_config,
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000551 std::vector<VideoReceiveStream::Config>* receive_configs,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000552 std::vector<VideoStream>* video_streams) OVERRIDE {
553 if (pad_to_min_bitrate_) {
554 send_config->rtp.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
555 } else {
556 assert(send_config->rtp.min_transmit_bitrate_bps == 0);
557 }
558 }
559
560 virtual void PerformTest() OVERRIDE {
561 EXPECT_EQ(kEventSignaled, Wait())
562 << "Timeout while waiting for send-bitrate stats.";
563 }
564
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000565 VideoSendStream* send_stream_;
566 PacketReceiver* send_transport_receiver_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000567 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000568 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000569 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000570
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000571 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000572 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000573}
574
575TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
576
577TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
578 TestMinTransmitBitrate(false);
579}
580
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000581} // namespace webrtc
kjellander@webrtc.org6f729e82014-09-02 15:13:55 +0000582
583#endif // !WEBRTC_ANDROID