blob: 0eaf310cb77c26df0951314f3f92983eef6c68b5 [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:
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:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000061 explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000062 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000063 crit_(CriticalSectionWrapper::CreateCriticalSection()) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000064
65 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
66 RTCPUtility::RTCPParserV2 parser(packet, length, true);
67 EXPECT_TRUE(parser.IsValid());
68
69 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
70 packet_type != RTCPUtility::kRtcpNotValidCode;
71 packet_type = parser.Iterate()) {
72 if (packet_type == RTCPUtility::kRtcpSrCode) {
73 const RTCPUtility::RTCPPacket& packet = parser.Packet();
wu@webrtc.org66773a02014-05-07 17:09:44 +000074 RtcpMeasurement ntp_rtp_pair(
pbos@webrtc.org1d096902013-12-13 12:48:05 +000075 packet.SR.NTPMostSignificant,
76 packet.SR.NTPLeastSignificant,
77 packet.SR.RTPTimestamp);
78 StoreNtpRtpPair(ntp_rtp_pair);
79 }
80 }
81 return SEND_PACKET;
82 }
83
84 int64_t RtpTimestampToNtp(uint32_t timestamp) const {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000085 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +000086 int64_t timestamp_in_ms = -1;
87 if (ntp_rtp_pairs_.size() == 2) {
88 // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
89 // RTCP sender where it sends RTCP SR before any RTP packets, which leads
90 // to a bogus NTP/RTP mapping.
wu@webrtc.org66773a02014-05-07 17:09:44 +000091 RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000092 return timestamp_in_ms;
93 }
94 return -1;
95 }
96
97 private:
wu@webrtc.org66773a02014-05-07 17:09:44 +000098 void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000099 CriticalSectionScoped lock(crit_.get());
wu@webrtc.org66773a02014-05-07 17:09:44 +0000100 for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000101 it != ntp_rtp_pairs_.end();
102 ++it) {
103 if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
104 ntp_rtp_pair.ntp_frac == it->ntp_frac) {
105 // This RTCP has already been added to the list.
106 return;
107 }
108 }
109 // We need two RTCP SR reports to map between RTP and NTP. More than two
110 // will not improve the mapping.
111 if (ntp_rtp_pairs_.size() == 2) {
112 ntp_rtp_pairs_.pop_back();
113 }
114 ntp_rtp_pairs_.push_front(ntp_rtp_pair);
115 }
116
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000117 const scoped_ptr<CriticalSectionWrapper> crit_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000118 RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000119};
120
121class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
122 static const int kInSyncThresholdMs = 50;
123 static const int kStartupTimeMs = 2000;
124 static const int kMinRunTimeMs = 30000;
125
126 public:
127 VideoRtcpAndSyncObserver(Clock* clock,
128 int voe_channel,
129 VoEVideoSync* voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000130 SyncRtcpObserver* audio_observer)
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000131 : SyncRtcpObserver(FakeNetworkPipe::Config()),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000132 clock_(clock),
133 voe_channel_(voe_channel),
134 voe_sync_(voe_sync),
135 audio_observer_(audio_observer),
136 creation_time_ms_(clock_->TimeInMilliseconds()),
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000137 first_time_in_sync_(-1) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000138
139 virtual void RenderFrame(const I420VideoFrame& video_frame,
140 int time_to_render_ms) OVERRIDE {
141 int64_t now_ms = clock_->TimeInMilliseconds();
142 uint32_t playout_timestamp = 0;
143 if (voe_sync_->GetPlayoutTimestamp(voe_channel_, playout_timestamp) != 0)
144 return;
145 int64_t latest_audio_ntp =
146 audio_observer_->RtpTimestampToNtp(playout_timestamp);
147 int64_t latest_video_ntp = RtpTimestampToNtp(video_frame.timestamp());
148 if (latest_audio_ntp < 0 || latest_video_ntp < 0)
149 return;
150 int time_until_render_ms =
151 std::max(0, static_cast<int>(video_frame.render_time_ms() - now_ms));
152 latest_video_ntp += time_until_render_ms;
153 int64_t stream_offset = latest_audio_ntp - latest_video_ntp;
154 std::stringstream ss;
155 ss << stream_offset;
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000156 webrtc::test::PrintResult("stream_offset",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000157 "",
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000158 "synchronization",
159 ss.str(),
160 "ms",
161 false);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000162 int64_t time_since_creation = now_ms - creation_time_ms_;
163 // During the first couple of seconds audio and video can falsely be
164 // estimated as being synchronized. We don't want to trigger on those.
165 if (time_since_creation < kStartupTimeMs)
166 return;
pbos@webrtc.orgb5f30292014-03-13 08:53:39 +0000167 if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) {
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000168 if (first_time_in_sync_ == -1) {
169 first_time_in_sync_ = now_ms;
170 webrtc::test::PrintResult("sync_convergence_time",
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000171 "",
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000172 "synchronization",
173 time_since_creation,
174 "ms",
175 false);
176 }
177 if (time_since_creation > kMinRunTimeMs)
178 observation_complete_->Set();
179 }
180 }
181
182 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000183 Clock* const clock_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000184 int voe_channel_;
185 VoEVideoSync* voe_sync_;
186 SyncRtcpObserver* audio_observer_;
187 int64_t creation_time_ms_;
188 int64_t first_time_in_sync_;
189};
190
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000191TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
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()) {}
198 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
199 size_t length) OVERRIDE {
200 int ret;
201 if (parser_->IsRtcp(packet, static_cast<int>(length))) {
202 ret = voe_network_->ReceivedRTCPPacket(
203 channel_, packet, static_cast<unsigned int>(length));
204 } else {
205 ret = voe_network_->ReceivedRTPPacket(
206 channel_, packet, static_cast<unsigned int>(length), PacketTime());
207 }
208 return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
209 }
210
211 private:
212 int channel_;
213 VoENetwork* voe_network_;
214 scoped_ptr<RtpHeaderParser> parser_;
215 };
216
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000217 VoiceEngine* voice_engine = VoiceEngine::Create();
218 VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
219 VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
220 VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine);
221 VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine);
222 const std::string audio_filename =
223 test::ResourcePath("voice_engine/audio_long16", "pcm");
224 ASSERT_STRNE("", audio_filename.c_str());
225 test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(),
226 audio_filename);
227 EXPECT_EQ(0, voe_base->Init(&fake_audio_device, NULL));
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000228 int channel = voe_base->CreateChannel();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000229
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000230 FakeNetworkPipe::Config net_config;
231 net_config.queue_delay_ms = 500;
232 SyncRtcpObserver audio_observer(net_config);
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000233 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
234 channel,
235 voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000236 &audio_observer);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000237
238 Call::Config receiver_config(observer.ReceiveTransport());
239 receiver_config.voice_engine = voice_engine;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000240 CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
241
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000242 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
243 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
244
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000245 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000246 audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
247
248 internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000249 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000250 EXPECT_EQ(0,
251 voe_network->RegisterExternalTransport(channel, transport_adapter));
252
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000253 observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000254
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000255 test::FakeDecoder fake_decoder;
256
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000257 CreateSendConfig(1);
258 CreateMatchingReceiveConfigs();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000259
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000260 receive_configs_[0].renderer = &observer;
261 receive_configs_[0].audio_channel_id = channel;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000262
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000263 CreateStreams();
264
265 CreateFrameGeneratorCapturer();
266
267 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000268
269 fake_audio_device.Start();
270 EXPECT_EQ(0, voe_base->StartPlayout(channel));
271 EXPECT_EQ(0, voe_base->StartReceive(channel));
272 EXPECT_EQ(0, voe_base->StartSend(channel));
273
274 EXPECT_EQ(kEventSignaled, observer.Wait())
275 << "Timed out while waiting for audio and video to be synchronized.";
276
277 EXPECT_EQ(0, voe_base->StopSend(channel));
278 EXPECT_EQ(0, voe_base->StopReceive(channel));
279 EXPECT_EQ(0, voe_base->StopPlayout(channel));
280 fake_audio_device.Stop();
281
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000282 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000283 observer.StopSending();
284 audio_observer.StopSending();
285
286 voe_base->DeleteChannel(channel);
287 voe_base->Release();
288 voe_codec->Release();
289 voe_network->Release();
290 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000291
292 DestroyStreams();
293
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000294 VoiceEngine::Delete(voice_engine);
295}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000296
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000297void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
298 int threshold_ms,
299 int start_time_ms,
300 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000301 class CaptureNtpTimeObserver : public test::EndToEndTest,
302 public VideoRenderer {
303 public:
304 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
305 int threshold_ms,
306 int start_time_ms,
307 int run_time_ms)
308 : EndToEndTest(kLongTimeoutMs, config),
309 clock_(Clock::GetRealTimeClock()),
310 threshold_ms_(threshold_ms),
311 start_time_ms_(start_time_ms),
312 run_time_ms_(run_time_ms),
313 creation_time_ms_(clock_->TimeInMilliseconds()),
314 capturer_(NULL),
315 rtp_start_timestamp_set_(false),
316 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000317
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000318 private:
319 virtual void RenderFrame(const I420VideoFrame& video_frame,
320 int time_to_render_ms) OVERRIDE {
321 if (video_frame.ntp_time_ms() <= 0) {
322 // Haven't got enough RTCP SR in order to calculate the capture ntp
323 // time.
324 return;
325 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000326
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000327 int64_t now_ms = clock_->TimeInMilliseconds();
328 int64_t time_since_creation = now_ms - creation_time_ms_;
329 if (time_since_creation < start_time_ms_) {
330 // Wait for |start_time_ms_| before start measuring.
331 return;
332 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000333
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000334 if (time_since_creation > run_time_ms_) {
335 observation_complete_->Set();
336 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000337
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000338 FrameCaptureTimeList::iterator iter =
339 capture_time_list_.find(video_frame.timestamp());
340 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000341
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000342 // The real capture time has been wrapped to uint32_t before converted
343 // to rtp timestamp in the sender side. So here we convert the estimated
344 // capture time to a uint32_t 90k timestamp also for comparing.
345 uint32_t estimated_capture_timestamp =
346 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
347 uint32_t real_capture_timestamp = iter->second;
348 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
349 time_offset_ms = time_offset_ms / 90;
350 std::stringstream ss;
351 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000352
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000353 webrtc::test::PrintResult(
354 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
355 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
356 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000357
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000358 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
359 RTPHeader header;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000360 EXPECT_TRUE(parser_->Parse(packet, length, &header));
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000361
362 if (!rtp_start_timestamp_set_) {
363 // Calculate the rtp timestamp offset in order to calculate the real
364 // capture time.
365 uint32_t first_capture_timestamp =
366 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
367 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
368 rtp_start_timestamp_set_ = true;
369 }
370
371 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
372 capture_time_list_.insert(
373 capture_time_list_.end(),
374 std::make_pair(header.timestamp, capture_timestamp));
375 return SEND_PACKET;
376 }
377
378 virtual void OnFrameGeneratorCapturerCreated(
379 test::FrameGeneratorCapturer* frame_generator_capturer) OVERRIDE {
380 capturer_ = frame_generator_capturer;
381 }
382
383 virtual void ModifyConfigs(
384 VideoSendStream::Config* send_config,
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000385 std::vector<VideoReceiveStream::Config>* receive_configs,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000386 std::vector<VideoStream>* video_streams) OVERRIDE {
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000387 (*receive_configs)[0].renderer = this;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000388 // Enable the receiver side rtt calculation.
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000389 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = true;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000390 }
391
392 virtual void PerformTest() OVERRIDE {
393 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
394 "estimated capture NTP time to be "
395 "within bounds.";
396 }
397
398 Clock* clock_;
399 int threshold_ms_;
400 int start_time_ms_;
401 int run_time_ms_;
402 int64_t creation_time_ms_;
403 test::FrameGeneratorCapturer* capturer_;
404 bool rtp_start_timestamp_set_;
405 uint32_t rtp_start_timestamp_;
406 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
407 FrameCaptureTimeList capture_time_list_;
408 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
409
410 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000411}
412
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000413TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000414 FakeNetworkPipe::Config net_config;
415 net_config.queue_delay_ms = 100;
416 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
417 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000418 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000419 const int kStartTimeMs = 10000;
420 const int kRunTimeMs = 20000;
421 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
422}
423
wu@webrtc.org0224c202014-05-05 17:42:43 +0000424TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000425 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000426 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000427 net_config.delay_standard_deviation_ms = 10;
428 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
429 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000430 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000431 const int kStartTimeMs = 10000;
432 const int kRunTimeMs = 20000;
433 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
434}
435
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000436TEST_F(CallPerfTest, RegisterCpuOveruseObserver) {
437 // Verifies that either a normal or overuse callback is triggered.
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000438 class OveruseCallbackObserver : public test::SendTest,
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000439 public webrtc::OveruseCallback {
440 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000441 OveruseCallbackObserver() : SendTest(kLongTimeoutMs) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000442
443 virtual void OnOveruse() OVERRIDE {
444 observation_complete_->Set();
445 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000446
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000447 virtual void OnNormalUse() OVERRIDE {
448 observation_complete_->Set();
449 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000450
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000451 virtual Call::Config GetSenderCallConfig() OVERRIDE {
452 Call::Config config(SendTransport());
453 config.overuse_callback = this;
454 return config;
455 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000456
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000457 virtual void PerformTest() OVERRIDE {
458 EXPECT_EQ(kEventSignaled, Wait())
459 << "Timed out before receiving an overuse callback.";
460 }
461 } test;
462
463 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000464}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000465
466void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
467 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000468 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000469 static const int kMinAcceptableTransmitBitrate = 130;
470 static const int kMaxAcceptableTransmitBitrate = 170;
471 static const int kNumBitrateObservationsInRange = 100;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000472 class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000473 public:
474 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000475 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000476 send_stream_(NULL),
477 send_transport_receiver_(NULL),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000478 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000479 num_bitrate_observations_in_range_(0) {}
480
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000481 private:
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000482 virtual void SetReceivers(PacketReceiver* send_transport_receiver,
483 PacketReceiver* receive_transport_receiver)
484 OVERRIDE {
485 send_transport_receiver_ = send_transport_receiver;
486 test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
487 }
488
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000489 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
490 size_t length) OVERRIDE {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000491 VideoSendStream::Stats stats = send_stream_->GetStats();
492 if (stats.substreams.size() > 0) {
493 assert(stats.substreams.size() == 1);
494 int bitrate_kbps = stats.substreams.begin()->second.bitrate_bps / 1000;
495 if (bitrate_kbps > 0) {
496 test::PrintResult(
497 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000498 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
499 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000500 "bitrate_kbps",
501 static_cast<size_t>(bitrate_kbps),
502 "kbps",
503 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000504 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000505 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
506 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
507 ++num_bitrate_observations_in_range_;
508 }
509 } else {
510 // Expect bitrate stats to roughly match the max encode bitrate.
511 if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 &&
512 bitrate_kbps < kMaxEncodeBitrateKbps + 5) {
513 ++num_bitrate_observations_in_range_;
514 }
515 }
516 if (num_bitrate_observations_in_range_ ==
517 kNumBitrateObservationsInRange)
518 observation_complete_->Set();
519 }
520 }
521 return send_transport_receiver_->DeliverPacket(packet, length);
522 }
523
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000524 virtual void OnStreamsCreated(
525 VideoSendStream* send_stream,
526 const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000527 send_stream_ = send_stream;
528 }
529
530 virtual void ModifyConfigs(
531 VideoSendStream::Config* send_config,
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000532 std::vector<VideoReceiveStream::Config>* receive_configs,
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000533 std::vector<VideoStream>* video_streams) OVERRIDE {
534 if (pad_to_min_bitrate_) {
535 send_config->rtp.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
536 } else {
537 assert(send_config->rtp.min_transmit_bitrate_bps == 0);
538 }
539 }
540
541 virtual void PerformTest() OVERRIDE {
542 EXPECT_EQ(kEventSignaled, Wait())
543 << "Timeout while waiting for send-bitrate stats.";
544 }
545
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000546 VideoSendStream* send_stream_;
547 PacketReceiver* send_transport_receiver_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000548 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000549 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000550 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000551
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000552 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000553 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000554}
555
556TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
557
558TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
559 TestMinTransmitBitrate(false);
560}
561
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000562} // namespace webrtc
kjellander@webrtc.org6f729e82014-09-02 15:13:55 +0000563
564#endif // !WEBRTC_ANDROID