blob: 32a299820ddea99cb04a91212ca51677705b880d [file] [log] [blame]
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +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 <map>
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +000013#include <string>
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000014#include <vector>
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000015
16#include "testing/gtest/include/gtest/gtest.h"
17
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000018#include "webrtc/call.h"
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000019#include "webrtc/common.h"
20#include "webrtc/experiments.h"
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000021#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
22#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
23#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000024#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000025#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
26#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
27#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
28#include "webrtc/system_wrappers/interface/event_wrapper.h"
29#include "webrtc/system_wrappers/interface/scoped_ptr.h"
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000030#include "webrtc/test/direct_transport.h"
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000031#include "webrtc/test/encoder_settings.h"
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000032#include "webrtc/test/fake_decoder.h"
33#include "webrtc/test/fake_encoder.h"
34#include "webrtc/test/frame_generator_capturer.h"
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000035#include "webrtc/test/testsupport/perf_test.h"
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +000036#include "webrtc/video/transport_adapter.h"
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000037
38namespace webrtc {
39
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000040namespace {
pbos@webrtc.orgc7667752014-01-24 09:30:53 +000041static const int kAbsoluteSendTimeExtensionId = 7;
42static const int kMaxPacketSize = 1500;
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000043
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000044class StreamObserver : public newapi::Transport, public RemoteBitrateObserver {
45 public:
46 typedef std::map<uint32_t, int> BytesSentMap;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000047 typedef std::map<uint32_t, uint32_t> SsrcMap;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +000048 StreamObserver(const SsrcMap& rtx_media_ssrcs,
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000049 newapi::Transport* feedback_transport,
50 Clock* clock)
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +000051 : clock_(clock),
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +000052 test_done_(EventWrapper::Create()),
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000053 rtp_parser_(RtpHeaderParser::Create()),
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +000054 feedback_transport_(feedback_transport),
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000055 receive_stats_(ReceiveStatistics::Create(clock)),
pbos@webrtc.orgc7667752014-01-24 09:30:53 +000056 payload_registry_(
andresp@webrtc.org4b0cd7f2014-04-08 11:06:12 +000057 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +000058 crit_(CriticalSectionWrapper::CreateCriticalSection()),
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000059 expected_bitrate_bps_(0),
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000060 rtx_media_ssrcs_(rtx_media_ssrcs),
61 total_sent_(0),
62 padding_sent_(0),
63 rtx_media_sent_(0),
64 total_packets_sent_(0),
65 padding_packets_sent_(0),
66 rtx_media_packets_sent_(0) {
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000067 // Ideally we would only have to instantiate an RtcpSender, an
68 // RtpHeaderParser and a RemoteBitrateEstimator here, but due to the current
69 // state of the RTP module we need a full module and receive statistics to
70 // be able to produce an RTCP with REMB.
71 RtpRtcp::Configuration config;
72 config.receive_statistics = receive_stats_.get();
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +000073 feedback_transport_.Enable();
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +000074 config.outgoing_transport = &feedback_transport_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000075 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
76 rtp_rtcp_->SetREMBStatus(true);
77 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
pbos@webrtc.org39139dc2013-12-16 12:24:44 +000078 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
79 kAbsoluteSendTimeExtensionId);
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000080 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
henrik.lundin@webrtc.orge6dc4ff2013-12-13 08:42:42 +000081 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 30000;
82 remote_bitrate_estimator_.reset(
stefan@webrtc.org9b2b8ec2014-03-24 09:42:08 +000083 rbe_factory.Create(this, clock, kMimdControl,
84 kRemoteBitrateEstimatorMinBitrateBps));
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000085 }
86
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000087 void set_expected_bitrate_bps(unsigned int expected_bitrate_bps) {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +000088 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000089 expected_bitrate_bps_ = expected_bitrate_bps;
90 }
91
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000092 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000093 unsigned int bitrate) OVERRIDE {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +000094 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000095 assert(expected_bitrate_bps_ > 0);
96 if (bitrate >= expected_bitrate_bps_) {
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +000097 // Just trigger if there was any rtx padding packet.
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000098 if (rtx_media_ssrcs_.empty() || rtx_media_sent_ > 0) {
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +000099 TriggerTestDone();
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000100 }
101 }
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000102 rtp_rtcp_->SetREMBData(
103 bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]);
104 rtp_rtcp_->Process();
105 }
106
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +0000107 virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000108 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000109 RTPHeader header;
110 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
111 receive_stats_->IncomingPacket(header, length, false);
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000112 payload_registry_->SetIncomingPayloadType(header);
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000113 remote_bitrate_estimator_->IncomingPacket(
114 clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header);
115 if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) {
116 remote_bitrate_estimator_->Process();
117 }
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000118 total_sent_ += length;
119 padding_sent_ += header.paddingLength;
120 ++total_packets_sent_;
121 if (header.paddingLength > 0)
122 ++padding_packets_sent_;
123 if (rtx_media_ssrcs_.find(header.ssrc) != rtx_media_ssrcs_.end()) {
124 rtx_media_sent_ += length - header.headerLength - header.paddingLength;
125 if (header.paddingLength == 0)
126 ++rtx_media_packets_sent_;
127 uint8_t restored_packet[kMaxPacketSize];
128 uint8_t* restored_packet_ptr = restored_packet;
129 int restored_length = static_cast<int>(length);
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000130 payload_registry_->RestoreOriginalPacket(&restored_packet_ptr,
131 packet,
132 &restored_length,
133 rtx_media_ssrcs_[header.ssrc],
134 header);
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000135 length = restored_length;
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000136 EXPECT_TRUE(rtp_parser_->Parse(
137 restored_packet, static_cast<int>(length), &header));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000138 } else {
139 rtp_rtcp_->SetRemoteSSRC(header.ssrc);
140 }
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000141 return true;
142 }
143
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +0000144 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000145 return true;
146 }
147
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000148 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000149
150 private:
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000151 void ReportResult(const std::string& measurement,
152 size_t value,
153 const std::string& units) {
154 webrtc::test::PrintResult(
155 measurement, "",
156 ::testing::UnitTest::GetInstance()->current_test_info()->name(),
157 value, units, false);
158 }
159
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000160 void TriggerTestDone() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000161 ReportResult("total-sent", total_sent_, "bytes");
162 ReportResult("padding-sent", padding_sent_, "bytes");
163 ReportResult("rtx-media-sent", rtx_media_sent_, "bytes");
164 ReportResult("total-packets-sent", total_packets_sent_, "packets");
165 ReportResult("padding-packets-sent", padding_packets_sent_, "packets");
166 ReportResult("rtx-packets-sent", rtx_media_packets_sent_, "packets");
167 test_done_->Set();
168 }
169
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000170 Clock* const clock_;
171 const scoped_ptr<EventWrapper> test_done_;
172 const scoped_ptr<RtpHeaderParser> rtp_parser_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000173 scoped_ptr<RtpRtcp> rtp_rtcp_;
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +0000174 internal::TransportAdapter feedback_transport_;
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000175 const scoped_ptr<ReceiveStatistics> receive_stats_;
176 const scoped_ptr<RTPPayloadRegistry> payload_registry_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000177 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000178
179 const scoped_ptr<CriticalSectionWrapper> crit_;
180 unsigned int expected_bitrate_bps_ GUARDED_BY(crit_);
181 SsrcMap rtx_media_ssrcs_ GUARDED_BY(crit_);
182 size_t total_sent_ GUARDED_BY(crit_);
183 size_t padding_sent_ GUARDED_BY(crit_);
184 size_t rtx_media_sent_ GUARDED_BY(crit_);
185 int total_packets_sent_ GUARDED_BY(crit_);
186 int padding_packets_sent_ GUARDED_BY(crit_);
187 int rtx_media_packets_sent_ GUARDED_BY(crit_);
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000188};
189
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000190class LowRateStreamObserver : public test::DirectTransport,
191 public RemoteBitrateObserver,
192 public PacketReceiver {
193 public:
194 LowRateStreamObserver(newapi::Transport* feedback_transport,
195 Clock* clock,
196 size_t number_of_streams,
197 bool rtx_used)
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000198 : clock_(clock),
199 number_of_streams_(number_of_streams),
200 rtx_used_(rtx_used),
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000201 test_done_(EventWrapper::Create()),
202 rtp_parser_(RtpHeaderParser::Create()),
203 feedback_transport_(feedback_transport),
204 receive_stats_(ReceiveStatistics::Create(clock)),
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000205 crit_(CriticalSectionWrapper::CreateCriticalSection()),
206 send_stream_(NULL),
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000207 test_state_(kFirstRampup),
208 state_start_ms_(clock_->TimeInMilliseconds()),
209 interval_start_ms_(state_start_ms_),
210 last_remb_bps_(0),
211 sent_bytes_(0),
212 total_overuse_bytes_(0),
henrik.lundin@webrtc.org3c00b1c2014-03-13 15:39:27 +0000213 suspended_in_stats_(false) {
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000214 RtpRtcp::Configuration config;
215 config.receive_statistics = receive_stats_.get();
216 feedback_transport_.Enable();
217 config.outgoing_transport = &feedback_transport_;
218 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
219 rtp_rtcp_->SetREMBStatus(true);
220 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
221 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
222 kAbsoluteSendTimeExtensionId);
223 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
224 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 10000;
225 remote_bitrate_estimator_.reset(
stefan@webrtc.org9b2b8ec2014-03-24 09:42:08 +0000226 rbe_factory.Create(this, clock, kMimdControl,
227 kRemoteBitrateEstimatorMinBitrateBps));
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000228 forward_transport_config_.link_capacity_kbps =
229 kHighBandwidthLimitBps / 1000;
230 forward_transport_config_.queue_length = 100; // Something large.
231 test::DirectTransport::SetConfig(forward_transport_config_);
232 test::DirectTransport::SetReceiver(this);
233 }
234
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000235 virtual void SetSendStream(const VideoSendStream* send_stream) {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000236 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000237 send_stream_ = send_stream;
238 }
239
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000240 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
241 unsigned int bitrate) {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000242 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000243 rtp_rtcp_->SetREMBData(
244 bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]);
245 rtp_rtcp_->Process();
246 last_remb_bps_ = bitrate;
247 }
248
249 virtual bool SendRtp(const uint8_t* data, size_t length) OVERRIDE {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000250 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000251 sent_bytes_ += length;
252 int64_t now_ms = clock_->TimeInMilliseconds();
253 if (now_ms > interval_start_ms_ + 1000) { // Let at least 1 second pass.
254 // Verify that the send rate was about right.
255 unsigned int average_rate_bps = static_cast<unsigned int>(sent_bytes_) *
256 8 * 1000 / (now_ms - interval_start_ms_);
257 // TODO(holmer): Why is this failing?
258 // EXPECT_LT(average_rate_bps, last_remb_bps_ * 1.1);
259 if (average_rate_bps > last_remb_bps_ * 1.1) {
260 total_overuse_bytes_ +=
261 sent_bytes_ -
262 last_remb_bps_ / 8 * (now_ms - interval_start_ms_) / 1000;
263 }
264 EvolveTestState(average_rate_bps);
265 interval_start_ms_ = now_ms;
266 sent_bytes_ = 0;
267 }
268 return test::DirectTransport::SendRtp(data, length);
269 }
270
271 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000272 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000273 RTPHeader header;
274 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
275 receive_stats_->IncomingPacket(header, length, false);
276 remote_bitrate_estimator_->IncomingPacket(
277 clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header);
278 if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) {
279 remote_bitrate_estimator_->Process();
280 }
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000281 suspended_in_stats_ = send_stream_->GetStats().suspended;
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000282 return true;
283 }
284
285 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
286 return true;
287 }
288
289 // Produces a string similar to "1stream_nortx", depending on the values of
290 // number_of_streams_ and rtx_used_;
291 std::string GetModifierString() {
292 std::string str("_");
293 char temp_str[5];
henrik.lundin@webrtc.org0b117152014-03-25 13:39:11 +0000294 sprintf(temp_str, "%i", static_cast<int>(number_of_streams_));
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000295 str += std::string(temp_str);
296 str += "stream";
297 str += (number_of_streams_ > 1 ? "s" : "");
298 str += "_";
299 str += (rtx_used_ ? "" : "no");
300 str += "rtx";
301 return str;
302 }
303
304 // This method defines the state machine for the ramp up-down-up test.
305 void EvolveTestState(unsigned int bitrate_bps) {
306 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000307 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000308 assert(send_stream_ != NULL);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000309 switch (test_state_) {
310 case kFirstRampup: {
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000311 EXPECT_FALSE(suspended_in_stats_);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000312 if (bitrate_bps > kExpectedHighBitrateBps) {
313 // The first ramp-up has reached the target bitrate. Change the
314 // channel limit, and move to the next test state.
315 forward_transport_config_.link_capacity_kbps =
316 kLowBandwidthLimitBps / 1000;
317 test::DirectTransport::SetConfig(forward_transport_config_);
318 test_state_ = kLowRate;
319 webrtc::test::PrintResult("ramp_up_down_up",
320 GetModifierString(),
321 "first_rampup",
322 now - state_start_ms_,
323 "ms",
324 false);
325 state_start_ms_ = now;
326 interval_start_ms_ = now;
327 sent_bytes_ = 0;
328 }
329 break;
330 }
331 case kLowRate: {
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000332 if (bitrate_bps < kExpectedLowBitrateBps && suspended_in_stats_) {
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000333 // The ramp-down was successful. Change the channel limit back to a
334 // high value, and move to the next test state.
335 forward_transport_config_.link_capacity_kbps =
336 kHighBandwidthLimitBps / 1000;
337 test::DirectTransport::SetConfig(forward_transport_config_);
338 test_state_ = kSecondRampup;
339 webrtc::test::PrintResult("ramp_up_down_up",
340 GetModifierString(),
341 "rampdown",
342 now - state_start_ms_,
343 "ms",
344 false);
345 state_start_ms_ = now;
346 interval_start_ms_ = now;
347 sent_bytes_ = 0;
348 }
349 break;
350 }
351 case kSecondRampup: {
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000352 if (bitrate_bps > kExpectedHighBitrateBps && !suspended_in_stats_) {
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000353 webrtc::test::PrintResult("ramp_up_down_up",
354 GetModifierString(),
355 "second_rampup",
356 now - state_start_ms_,
357 "ms",
358 false);
359 webrtc::test::PrintResult("ramp_up_down_up",
360 GetModifierString(),
361 "total_overuse",
362 total_overuse_bytes_,
363 "bytes",
364 false);
365 test_done_->Set();
366 }
367 break;
368 }
369 }
370 }
371
372 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
373
374 private:
375 static const unsigned int kHighBandwidthLimitBps = 80000;
376 static const unsigned int kExpectedHighBitrateBps = 60000;
377 static const unsigned int kLowBandwidthLimitBps = 20000;
378 static const unsigned int kExpectedLowBitrateBps = 20000;
379 enum TestStates { kFirstRampup, kLowRate, kSecondRampup };
380
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000381 Clock* const clock_;
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000382 const size_t number_of_streams_;
383 const bool rtx_used_;
pbos@webrtc.orgc54ff692014-04-28 13:00:21 +0000384 const scoped_ptr<EventWrapper> test_done_;
385 const scoped_ptr<RtpHeaderParser> rtp_parser_;
386 scoped_ptr<RtpRtcp> rtp_rtcp_;
387 internal::TransportAdapter feedback_transport_;
388 const scoped_ptr<ReceiveStatistics> receive_stats_;
389 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
390
391 scoped_ptr<CriticalSectionWrapper> crit_;
392 const VideoSendStream* send_stream_ GUARDED_BY(crit_);
393 FakeNetworkPipe::Config forward_transport_config_ GUARDED_BY(crit_);
394 TestStates test_state_ GUARDED_BY(crit_);
395 int64_t state_start_ms_ GUARDED_BY(crit_);
396 int64_t interval_start_ms_ GUARDED_BY(crit_);
397 unsigned int last_remb_bps_ GUARDED_BY(crit_);
398 size_t sent_bytes_ GUARDED_BY(crit_);
399 size_t total_overuse_bytes_ GUARDED_BY(crit_);
400 bool suspended_in_stats_ GUARDED_BY(crit_);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000401};
402}
403
404class RampUpTest : public ::testing::Test {
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000405 public:
406 virtual void SetUp() { reserved_ssrcs_.clear(); }
407
408 protected:
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000409 void RunRampUpTest(bool pacing, bool rtx, size_t num_streams) {
410 std::vector<uint32_t> ssrcs(GenerateSsrcs(num_streams, 100));
411 std::vector<uint32_t> rtx_ssrcs(GenerateSsrcs(num_streams, 200));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000412 StreamObserver::SsrcMap rtx_ssrc_map;
413 if (rtx) {
414 for (size_t i = 0; i < ssrcs.size(); ++i)
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000415 rtx_ssrc_map[rtx_ssrcs[i]] = ssrcs[i];
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000416 }
417 test::DirectTransport receiver_transport;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000418 StreamObserver stream_observer(rtx_ssrc_map,
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000419 &receiver_transport,
420 Clock::GetRealTimeClock());
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000421
422 Call::Config call_config(&stream_observer);
423 webrtc::Config webrtc_config;
424 call_config.webrtc_config = &webrtc_config;
425 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
426 scoped_ptr<Call> call(Call::Create(call_config));
427 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
428
429 receiver_transport.SetReceiver(call->Receiver());
430
431 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000432 send_config.encoder_settings =
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000433 test::CreateEncoderSettings(&encoder, "FAKE", 125, num_streams);
434
435 if (num_streams == 1) {
436 send_config.encoder_settings.streams[0].target_bitrate_bps = 2000000;
437 send_config.encoder_settings.streams[0].max_bitrate_bps = 2000000;
438 }
439
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000440 send_config.pacing = pacing;
441 send_config.rtp.nack.rtp_history_ms = 1000;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000442 send_config.rtp.ssrcs = ssrcs;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000443 if (rtx) {
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000444 send_config.rtp.rtx.payload_type = 96;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000445 send_config.rtp.rtx.ssrcs = rtx_ssrcs;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000446 }
447 send_config.rtp.extensions.push_back(
pbos@webrtc.org39139dc2013-12-16 12:24:44 +0000448 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000449
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000450 if (num_streams == 1) {
451 // For single stream rampup until 1mbps
452 stream_observer.set_expected_bitrate_bps(1000000);
453 } else {
454 // For multi stream rampup until all streams are being sent. That means
455 // enough birate to sent all the target streams plus the min bitrate of
456 // the last one.
457 int expected_bitrate_bps =
458 send_config.encoder_settings.streams.back().min_bitrate_bps;
459 for (size_t i = 0; i < send_config.encoder_settings.streams.size() - 1;
460 ++i) {
461 expected_bitrate_bps +=
462 send_config.encoder_settings.streams[i].target_bitrate_bps;
463 }
464 stream_observer.set_expected_bitrate_bps(expected_bitrate_bps);
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000465 }
466
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000467 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
468
469 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000470 test::FrameGeneratorCapturer::Create(
471 send_stream->Input(),
472 send_config.encoder_settings.streams.back().width,
473 send_config.encoder_settings.streams.back().height,
474 send_config.encoder_settings.streams.back().max_framerate,
475 Clock::GetRealTimeClock()));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000476
pbos@webrtc.org9d0f79f2014-04-24 11:13:21 +0000477 send_stream->Start();
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000478 frame_generator_capturer->Start();
479
480 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
481
482 frame_generator_capturer->Stop();
pbos@webrtc.org9d0f79f2014-04-24 11:13:21 +0000483 send_stream->Stop();
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000484
485 call->DestroyVideoSendStream(send_stream);
486 }
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000487
488 void RunRampUpDownUpTest(size_t number_of_streams, bool rtx) {
489 std::vector<uint32_t> ssrcs;
490 for (size_t i = 0; i < number_of_streams; ++i)
491 ssrcs.push_back(static_cast<uint32_t>(i + 1));
492 test::DirectTransport receiver_transport;
493 LowRateStreamObserver stream_observer(
494 &receiver_transport, Clock::GetRealTimeClock(), number_of_streams, rtx);
495
496 Call::Config call_config(&stream_observer);
497 webrtc::Config webrtc_config;
498 call_config.webrtc_config = &webrtc_config;
499 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
500 scoped_ptr<Call> call(Call::Create(call_config));
501 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
502
503 receiver_transport.SetReceiver(call->Receiver());
504
505 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000506 send_config.encoder_settings =
507 test::CreateEncoderSettings(&encoder, "FAKE", 125, number_of_streams);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000508 send_config.rtp.nack.rtp_history_ms = 1000;
509 send_config.rtp.ssrcs.insert(
510 send_config.rtp.ssrcs.begin(), ssrcs.begin(), ssrcs.end());
511 send_config.rtp.extensions.push_back(
512 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
513 send_config.suspend_below_min_bitrate = true;
514
515 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000516 stream_observer.SetSendStream(send_stream);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000517
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000518 size_t width = 0;
519 size_t height = 0;
520 for (size_t i = 0; i < send_config.encoder_settings.streams.size(); ++i) {
521 size_t stream_width = send_config.encoder_settings.streams[i].width;
522 size_t stream_height = send_config.encoder_settings.streams[i].height;
523 if (stream_width > width)
524 width = stream_width;
525 if (stream_height > height)
526 height = stream_height;
527 }
528
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000529 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
530 test::FrameGeneratorCapturer::Create(send_stream->Input(),
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000531 width,
532 height,
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000533 30,
534 Clock::GetRealTimeClock()));
535
pbos@webrtc.org9d0f79f2014-04-24 11:13:21 +0000536 send_stream->Start();
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000537 frame_generator_capturer->Start();
538
539 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
540
henrik.lundin@webrtc.org3c00b1c2014-03-13 15:39:27 +0000541 stream_observer.StopSending();
542 receiver_transport.StopSending();
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000543 frame_generator_capturer->Stop();
pbos@webrtc.org9d0f79f2014-04-24 11:13:21 +0000544 send_stream->Stop();
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000545
546 call->DestroyVideoSendStream(send_stream);
547 }
548
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000549 private:
550 std::vector<uint32_t> GenerateSsrcs(size_t num_streams,
551 uint32_t ssrc_offset) {
552 std::vector<uint32_t> ssrcs;
553 for (size_t i = 0; i != num_streams; ++i)
554 ssrcs.push_back(static_cast<uint32_t>(ssrc_offset + i));
555 return ssrcs;
556 }
557
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000558 std::map<uint32_t, bool> reserved_ssrcs_;
559};
560
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000561TEST_F(RampUpTest, SingleStreamWithoutPacing) {
562 RunRampUpTest(false, false, 1);
563}
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000564
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000565TEST_F(RampUpTest, SingleStreamWithPacing) {
566 RunRampUpTest(true, false, 1);
567}
568
569TEST_F(RampUpTest, SimulcastWithoutPacing) {
570 RunRampUpTest(false, false, 3);
571}
572
573TEST_F(RampUpTest, SimulcastWithPacing) {
574 RunRampUpTest(true, false, 3);
575}
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000576
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000577// TODO(pbos): Re-enable, webrtc:2992.
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000578TEST_F(RampUpTest, DISABLED_SimulcastWithPacingAndRtx) {
579 RunRampUpTest(true, true, 3);
580}
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000581
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000582TEST_F(RampUpTest, UpDownUpOneStream) { RunRampUpDownUpTest(1, false); }
583
henrik.lundin@webrtc.org41da3292014-03-13 09:21:26 +0000584TEST_F(RampUpTest, UpDownUpThreeStreams) { RunRampUpDownUpTest(3, false); }
henrik.lundin@webrtc.orgc63f18d2014-03-06 09:12:00 +0000585
henrik.lundin@webrtc.org41da3292014-03-13 09:21:26 +0000586TEST_F(RampUpTest, UpDownUpOneStreamRtx) { RunRampUpDownUpTest(1, true); }
henrik.lundin@webrtc.orgc63f18d2014-03-06 09:12:00 +0000587
henrik.lundin@webrtc.org41da3292014-03-13 09:21:26 +0000588TEST_F(RampUpTest, UpDownUpThreeStreamsRtx) { RunRampUpDownUpTest(3, true); }
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000589
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000590} // namespace webrtc