blob: b6e2b3408da2073a4152e43ba2d25b4adde2794d [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
44namespace webrtc {
45
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000046class CallPerfTest : public test::CallTest {
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +000047 protected:
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000048 void TestMinTransmitBitrate(bool pad_to_min_bitrate);
49
wu@webrtc.orgcd701192014-04-24 22:10:24 +000050 void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
51 int threshold_ms,
52 int start_time_ms,
53 int run_time_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000054};
55
56class SyncRtcpObserver : public test::RtpRtcpObserver {
57 public:
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000058 explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000059 : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000060 crit_(CriticalSectionWrapper::CreateCriticalSection()) {}
pbos@webrtc.org1d096902013-12-13 12:48:05 +000061
62 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
63 RTCPUtility::RTCPParserV2 parser(packet, length, true);
64 EXPECT_TRUE(parser.IsValid());
65
66 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
67 packet_type != RTCPUtility::kRtcpNotValidCode;
68 packet_type = parser.Iterate()) {
69 if (packet_type == RTCPUtility::kRtcpSrCode) {
70 const RTCPUtility::RTCPPacket& packet = parser.Packet();
wu@webrtc.org66773a02014-05-07 17:09:44 +000071 RtcpMeasurement ntp_rtp_pair(
pbos@webrtc.org1d096902013-12-13 12:48:05 +000072 packet.SR.NTPMostSignificant,
73 packet.SR.NTPLeastSignificant,
74 packet.SR.RTPTimestamp);
75 StoreNtpRtpPair(ntp_rtp_pair);
76 }
77 }
78 return SEND_PACKET;
79 }
80
81 int64_t RtpTimestampToNtp(uint32_t timestamp) const {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000082 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org1d096902013-12-13 12:48:05 +000083 int64_t timestamp_in_ms = -1;
84 if (ntp_rtp_pairs_.size() == 2) {
85 // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the
86 // RTCP sender where it sends RTCP SR before any RTP packets, which leads
87 // to a bogus NTP/RTP mapping.
wu@webrtc.org66773a02014-05-07 17:09:44 +000088 RtpToNtpMs(timestamp, ntp_rtp_pairs_, &timestamp_in_ms);
pbos@webrtc.org1d096902013-12-13 12:48:05 +000089 return timestamp_in_ms;
90 }
91 return -1;
92 }
93
94 private:
wu@webrtc.org66773a02014-05-07 17:09:44 +000095 void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000096 CriticalSectionScoped lock(crit_.get());
wu@webrtc.org66773a02014-05-07 17:09:44 +000097 for (RtcpList::iterator it = ntp_rtp_pairs_.begin();
pbos@webrtc.org1d096902013-12-13 12:48:05 +000098 it != ntp_rtp_pairs_.end();
99 ++it) {
100 if (ntp_rtp_pair.ntp_secs == it->ntp_secs &&
101 ntp_rtp_pair.ntp_frac == it->ntp_frac) {
102 // This RTCP has already been added to the list.
103 return;
104 }
105 }
106 // We need two RTCP SR reports to map between RTP and NTP. More than two
107 // will not improve the mapping.
108 if (ntp_rtp_pairs_.size() == 2) {
109 ntp_rtp_pairs_.pop_back();
110 }
111 ntp_rtp_pairs_.push_front(ntp_rtp_pair);
112 }
113
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000114 const scoped_ptr<CriticalSectionWrapper> crit_;
wu@webrtc.org66773a02014-05-07 17:09:44 +0000115 RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000116};
117
118class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer {
119 static const int kInSyncThresholdMs = 50;
120 static const int kStartupTimeMs = 2000;
121 static const int kMinRunTimeMs = 30000;
122
123 public:
124 VideoRtcpAndSyncObserver(Clock* clock,
125 int voe_channel,
126 VoEVideoSync* voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000127 SyncRtcpObserver* audio_observer)
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000128 : SyncRtcpObserver(FakeNetworkPipe::Config()),
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000129 clock_(clock),
130 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
136 virtual void RenderFrame(const I420VideoFrame& video_frame,
137 int time_to_render_ms) OVERRIDE {
138 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
179 private:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000180 Clock* const clock_;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000181 int voe_channel_;
182 VoEVideoSync* voe_sync_;
183 SyncRtcpObserver* audio_observer_;
184 int64_t creation_time_ms_;
185 int64_t first_time_in_sync_;
186};
187
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000188TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000189 class AudioPacketReceiver : public PacketReceiver {
190 public:
191 AudioPacketReceiver(int channel, VoENetwork* voe_network)
192 : channel_(channel),
193 voe_network_(voe_network),
194 parser_(RtpHeaderParser::Create()) {}
195 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
196 size_t length) OVERRIDE {
197 int ret;
198 if (parser_->IsRtcp(packet, static_cast<int>(length))) {
199 ret = voe_network_->ReceivedRTCPPacket(
200 channel_, packet, static_cast<unsigned int>(length));
201 } else {
202 ret = voe_network_->ReceivedRTPPacket(
203 channel_, packet, static_cast<unsigned int>(length), PacketTime());
204 }
205 return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR;
206 }
207
208 private:
209 int channel_;
210 VoENetwork* voe_network_;
211 scoped_ptr<RtpHeaderParser> parser_;
212 };
213
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000214 VoiceEngine* voice_engine = VoiceEngine::Create();
215 VoEBase* voe_base = VoEBase::GetInterface(voice_engine);
216 VoECodec* voe_codec = VoECodec::GetInterface(voice_engine);
217 VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine);
218 VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine);
219 const std::string audio_filename =
220 test::ResourcePath("voice_engine/audio_long16", "pcm");
221 ASSERT_STRNE("", audio_filename.c_str());
222 test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(),
223 audio_filename);
224 EXPECT_EQ(0, voe_base->Init(&fake_audio_device, NULL));
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000225 int channel = voe_base->CreateChannel();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000226
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000227 FakeNetworkPipe::Config net_config;
228 net_config.queue_delay_ms = 500;
229 SyncRtcpObserver audio_observer(net_config);
henrik.lundin@webrtc.orged865b52014-03-06 10:28:07 +0000230 VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
231 channel,
232 voe_sync,
henrik.lundin@webrtc.orgd144bb62014-04-22 08:36:33 +0000233 &audio_observer);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000234
235 Call::Config receiver_config(observer.ReceiveTransport());
236 receiver_config.voice_engine = voice_engine;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000237 CreateCalls(Call::Config(observer.SendTransport()), receiver_config);
238
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000239 CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000};
240 EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac));
241
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000242 AudioPacketReceiver voe_packet_receiver(channel, voe_network);
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000243 audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver);
244
245 internal::TransportAdapter transport_adapter(audio_observer.SendTransport());
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000246 transport_adapter.Enable();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000247 EXPECT_EQ(0,
248 voe_network->RegisterExternalTransport(channel, transport_adapter));
249
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000250 observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver());
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000251
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000252 test::FakeDecoder fake_decoder;
253
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000254 CreateSendConfig(1);
255 CreateMatchingReceiveConfigs();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000256
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000257 receive_config_.renderer = &observer;
258 receive_config_.audio_channel_id = channel;
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000259
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000260 CreateStreams();
261
262 CreateFrameGeneratorCapturer();
263
264 Start();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000265
266 fake_audio_device.Start();
267 EXPECT_EQ(0, voe_base->StartPlayout(channel));
268 EXPECT_EQ(0, voe_base->StartReceive(channel));
269 EXPECT_EQ(0, voe_base->StartSend(channel));
270
271 EXPECT_EQ(kEventSignaled, observer.Wait())
272 << "Timed out while waiting for audio and video to be synchronized.";
273
274 EXPECT_EQ(0, voe_base->StopSend(channel));
275 EXPECT_EQ(0, voe_base->StopReceive(channel));
276 EXPECT_EQ(0, voe_base->StopPlayout(channel));
277 fake_audio_device.Stop();
278
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000279 Stop();
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000280 observer.StopSending();
281 audio_observer.StopSending();
282
283 voe_base->DeleteChannel(channel);
284 voe_base->Release();
285 voe_codec->Release();
286 voe_network->Release();
287 voe_sync->Release();
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000288
289 DestroyStreams();
290
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000291 VoiceEngine::Delete(voice_engine);
292}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000293
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000294void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
295 int threshold_ms,
296 int start_time_ms,
297 int run_time_ms) {
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000298 class CaptureNtpTimeObserver : public test::EndToEndTest,
299 public VideoRenderer {
300 public:
301 CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config,
302 int threshold_ms,
303 int start_time_ms,
304 int run_time_ms)
305 : EndToEndTest(kLongTimeoutMs, config),
306 clock_(Clock::GetRealTimeClock()),
307 threshold_ms_(threshold_ms),
308 start_time_ms_(start_time_ms),
309 run_time_ms_(run_time_ms),
310 creation_time_ms_(clock_->TimeInMilliseconds()),
311 capturer_(NULL),
312 rtp_start_timestamp_set_(false),
313 rtp_start_timestamp_(0) {}
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000314
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000315 private:
316 virtual void RenderFrame(const I420VideoFrame& video_frame,
317 int time_to_render_ms) OVERRIDE {
318 if (video_frame.ntp_time_ms() <= 0) {
319 // Haven't got enough RTCP SR in order to calculate the capture ntp
320 // time.
321 return;
322 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000323
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000324 int64_t now_ms = clock_->TimeInMilliseconds();
325 int64_t time_since_creation = now_ms - creation_time_ms_;
326 if (time_since_creation < start_time_ms_) {
327 // Wait for |start_time_ms_| before start measuring.
328 return;
329 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000330
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000331 if (time_since_creation > run_time_ms_) {
332 observation_complete_->Set();
333 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000334
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000335 FrameCaptureTimeList::iterator iter =
336 capture_time_list_.find(video_frame.timestamp());
337 EXPECT_TRUE(iter != capture_time_list_.end());
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000338
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000339 // The real capture time has been wrapped to uint32_t before converted
340 // to rtp timestamp in the sender side. So here we convert the estimated
341 // capture time to a uint32_t 90k timestamp also for comparing.
342 uint32_t estimated_capture_timestamp =
343 90 * static_cast<uint32_t>(video_frame.ntp_time_ms());
344 uint32_t real_capture_timestamp = iter->second;
345 int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp;
346 time_offset_ms = time_offset_ms / 90;
347 std::stringstream ss;
348 ss << time_offset_ms;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000349
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000350 webrtc::test::PrintResult(
351 "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true);
352 EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_);
353 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000354
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000355 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
356 RTPHeader header;
357 EXPECT_TRUE(parser_->Parse(packet, static_cast<int>(length), &header));
358
359 if (!rtp_start_timestamp_set_) {
360 // Calculate the rtp timestamp offset in order to calculate the real
361 // capture time.
362 uint32_t first_capture_timestamp =
363 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time());
364 rtp_start_timestamp_ = header.timestamp - first_capture_timestamp;
365 rtp_start_timestamp_set_ = true;
366 }
367
368 uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_;
369 capture_time_list_.insert(
370 capture_time_list_.end(),
371 std::make_pair(header.timestamp, capture_timestamp));
372 return SEND_PACKET;
373 }
374
375 virtual void OnFrameGeneratorCapturerCreated(
376 test::FrameGeneratorCapturer* frame_generator_capturer) OVERRIDE {
377 capturer_ = frame_generator_capturer;
378 }
379
380 virtual void ModifyConfigs(
381 VideoSendStream::Config* send_config,
382 VideoReceiveStream::Config* receive_config,
383 std::vector<VideoStream>* video_streams) OVERRIDE {
384 receive_config->renderer = this;
385 // Enable the receiver side rtt calculation.
386 receive_config->rtp.rtcp_xr.receiver_reference_time_report = true;
387 }
388
389 virtual void PerformTest() OVERRIDE {
390 EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for "
391 "estimated capture NTP time to be "
392 "within bounds.";
393 }
394
395 Clock* clock_;
396 int threshold_ms_;
397 int start_time_ms_;
398 int run_time_ms_;
399 int64_t creation_time_ms_;
400 test::FrameGeneratorCapturer* capturer_;
401 bool rtp_start_timestamp_set_;
402 uint32_t rtp_start_timestamp_;
403 typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList;
404 FrameCaptureTimeList capture_time_list_;
405 } test(net_config, threshold_ms, start_time_ms, run_time_ms);
406
407 RunBaseTest(&test);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000408}
409
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000410TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000411 FakeNetworkPipe::Config net_config;
412 net_config.queue_delay_ms = 100;
413 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
414 // accurate.
wu@webrtc.org9aa7d8d2014-05-29 05:03:52 +0000415 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000416 const int kStartTimeMs = 10000;
417 const int kRunTimeMs = 20000;
418 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
419}
420
wu@webrtc.org0224c202014-05-05 17:42:43 +0000421TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000422 FakeNetworkPipe::Config net_config;
wu@webrtc.org0224c202014-05-05 17:42:43 +0000423 net_config.queue_delay_ms = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000424 net_config.delay_standard_deviation_ms = 10;
425 // TODO(wu): lower the threshold as the calculation/estimatation becomes more
426 // accurate.
wu@webrtc.org0224c202014-05-05 17:42:43 +0000427 const int kThresholdMs = 100;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000428 const int kStartTimeMs = 10000;
429 const int kRunTimeMs = 20000;
430 TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs);
431}
432
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000433TEST_F(CallPerfTest, RegisterCpuOveruseObserver) {
434 // Verifies that either a normal or overuse callback is triggered.
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000435 class OveruseCallbackObserver : public test::SendTest,
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000436 public webrtc::OveruseCallback {
437 public:
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000438 OveruseCallbackObserver() : SendTest(kLongTimeoutMs) {}
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000439
440 virtual void OnOveruse() OVERRIDE {
441 observation_complete_->Set();
442 }
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000443
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000444 virtual void OnNormalUse() OVERRIDE {
445 observation_complete_->Set();
446 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000447
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000448 virtual Call::Config GetSenderCallConfig() OVERRIDE {
449 Call::Config config(SendTransport());
450 config.overuse_callback = this;
451 return config;
452 }
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000453
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000454 virtual void PerformTest() OVERRIDE {
455 EXPECT_EQ(kEventSignaled, Wait())
456 << "Timed out before receiving an overuse callback.";
457 }
458 } test;
459
460 RunBaseTest(&test);
asapersson@webrtc.orgbdc5ed22014-01-31 10:05:07 +0000461}
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000462
463void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) {
464 static const int kMaxEncodeBitrateKbps = 30;
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000465 static const int kMinTransmitBitrateBps = 150000;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000466 static const int kMinAcceptableTransmitBitrate = 130;
467 static const int kMaxAcceptableTransmitBitrate = 170;
468 static const int kNumBitrateObservationsInRange = 100;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000469 class BitrateObserver : public test::EndToEndTest, public PacketReceiver {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000470 public:
471 explicit BitrateObserver(bool using_min_transmit_bitrate)
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000472 : EndToEndTest(kLongTimeoutMs),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000473 send_stream_(NULL),
474 send_transport_receiver_(NULL),
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000475 pad_to_min_bitrate_(using_min_transmit_bitrate),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000476 num_bitrate_observations_in_range_(0) {}
477
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000478 private:
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000479 virtual void SetReceivers(PacketReceiver* send_transport_receiver,
480 PacketReceiver* receive_transport_receiver)
481 OVERRIDE {
482 send_transport_receiver_ = send_transport_receiver;
483 test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver);
484 }
485
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000486 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
487 size_t length) OVERRIDE {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000488 VideoSendStream::Stats stats = send_stream_->GetStats();
489 if (stats.substreams.size() > 0) {
490 assert(stats.substreams.size() == 1);
491 int bitrate_kbps = stats.substreams.begin()->second.bitrate_bps / 1000;
492 if (bitrate_kbps > 0) {
493 test::PrintResult(
494 "bitrate_stats_",
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000495 (pad_to_min_bitrate_ ? "min_transmit_bitrate"
496 : "without_min_transmit_bitrate"),
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000497 "bitrate_kbps",
498 static_cast<size_t>(bitrate_kbps),
499 "kbps",
500 false);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000501 if (pad_to_min_bitrate_) {
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000502 if (bitrate_kbps > kMinAcceptableTransmitBitrate &&
503 bitrate_kbps < kMaxAcceptableTransmitBitrate) {
504 ++num_bitrate_observations_in_range_;
505 }
506 } else {
507 // Expect bitrate stats to roughly match the max encode bitrate.
508 if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 &&
509 bitrate_kbps < kMaxEncodeBitrateKbps + 5) {
510 ++num_bitrate_observations_in_range_;
511 }
512 }
513 if (num_bitrate_observations_in_range_ ==
514 kNumBitrateObservationsInRange)
515 observation_complete_->Set();
516 }
517 }
518 return send_transport_receiver_->DeliverPacket(packet, length);
519 }
520
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000521 virtual void OnStreamsCreated(VideoSendStream* send_stream,
522 VideoReceiveStream* receive_stream) {
523 send_stream_ = send_stream;
524 }
525
526 virtual void ModifyConfigs(
527 VideoSendStream::Config* send_config,
528 VideoReceiveStream::Config* receive_config,
529 std::vector<VideoStream>* video_streams) OVERRIDE {
530 if (pad_to_min_bitrate_) {
531 send_config->rtp.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
532 } else {
533 assert(send_config->rtp.min_transmit_bitrate_bps == 0);
534 }
535 }
536
537 virtual void PerformTest() OVERRIDE {
538 EXPECT_EQ(kEventSignaled, Wait())
539 << "Timeout while waiting for send-bitrate stats.";
540 }
541
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000542 VideoSendStream* send_stream_;
543 PacketReceiver* send_transport_receiver_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000544 const bool pad_to_min_bitrate_;
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000545 int num_bitrate_observations_in_range_;
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000546 } test(pad_to_min_bitrate);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000547
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000548 fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps);
pbos@webrtc.org994d0b72014-06-27 08:47:52 +0000549 RunBaseTest(&test);
pbos@webrtc.org3349ae02014-03-13 12:52:27 +0000550}
551
552TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); }
553
554TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) {
555 TestMinTransmitBitrate(false);
556}
557
pbos@webrtc.org1d096902013-12-13 12:48:05 +0000558} // namespace webrtc