blob: c86963d033f7e3388d2b428383dba282a4f254ad [file] [log] [blame]
pbos@webrtc.org744fbc72013-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.org845862f2014-03-06 07:19:28 +000013#include <string>
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000014#include <vector>
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000015
16#include "testing/gtest/include/gtest/gtest.h"
17
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000018#include "webrtc/call.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000019#include "webrtc/common.h"
20#include "webrtc/experiments.h"
pbos@webrtc.org744fbc72013-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.org7e9315b2013-12-04 10:24:26 +000024#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
pbos@webrtc.org744fbc72013-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.org16e03b72013-10-28 16:32:01 +000030#include "webrtc/test/direct_transport.h"
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000031#include "webrtc/test/encoder_settings.h"
pbos@webrtc.org16e03b72013-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.org7e9315b2013-12-04 10:24:26 +000035#include "webrtc/test/testsupport/perf_test.h"
pbos@webrtc.org27326b62013-11-20 12:17:04 +000036#include "webrtc/video/transport_adapter.h"
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000037
38namespace webrtc {
39
pbos@webrtc.org29023282013-09-11 10:14:56 +000040namespace {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +000041static const int kAbsoluteSendTimeExtensionId = 7;
42static const int kMaxPacketSize = 1500;
pbos@webrtc.org29023282013-09-11 10:14:56 +000043
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000044class StreamObserver : public newapi::Transport, public RemoteBitrateObserver {
45 public:
46 typedef std::map<uint32_t, int> BytesSentMap;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000047 typedef std::map<uint32_t, uint32_t> SsrcMap;
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +000048 StreamObserver(const SsrcMap& rtx_media_ssrcs,
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000049 newapi::Transport* feedback_transport,
50 Clock* clock)
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000051 : clock_(clock),
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +000052 test_done_(EventWrapper::Create()),
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000053 rtp_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org27326b62013-11-20 12:17:04 +000054 feedback_transport_(feedback_transport),
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000055 receive_stats_(ReceiveStatistics::Create(clock)),
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +000056 payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000057 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000058 crit_(CriticalSectionWrapper::CreateCriticalSection()),
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000059 expected_bitrate_bps_(0),
stefan@webrtc.org7e9315b2013-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.org744fbc72013-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.orgd9b95602014-01-27 13:03:02 +000073 feedback_transport_.Enable();
pbos@webrtc.org27326b62013-11-20 12:17:04 +000074 config.outgoing_transport = &feedback_transport_;
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000075 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
76 rtp_rtcp_->SetREMBStatus(true);
77 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000078 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
79 kAbsoluteSendTimeExtensionId);
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000080 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
henrik.lundin@webrtc.orge9abd592013-12-13 08:42:42 +000081 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 30000;
82 remote_bitrate_estimator_.reset(
stefan@webrtc.orgaf839b22014-03-24 09:42:08 +000083 rbe_factory.Create(this, clock, kMimdControl,
84 kRemoteBitrateEstimatorMinBitrateBps));
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000085 }
86
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000087 void set_expected_bitrate_bps(unsigned int expected_bitrate_bps) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000088 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000089 expected_bitrate_bps_ = expected_bitrate_bps;
90 }
91
pbos@webrtc.org744fbc72013-09-10 09:26:25 +000092 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000093 unsigned int bitrate) OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000094 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000095 assert(expected_bitrate_bps_ > 0);
96 if (bitrate >= expected_bitrate_bps_) {
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +000097 // Just trigger if there was any rtx padding packet.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000098 if (rtx_media_ssrcs_.empty() || rtx_media_sent_ > 0) {
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +000099 TriggerTestDone();
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000100 }
101 }
pbos@webrtc.org744fbc72013-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.org27326b62013-11-20 12:17:04 +0000107 virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000108 CriticalSectionScoped lock(crit_.get());
pbos@webrtc.org744fbc72013-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.org7e9315b2013-12-04 10:24:26 +0000112 payload_registry_->SetIncomingPayloadType(header);
pbos@webrtc.org744fbc72013-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.org7e9315b2013-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.orgc279a5d2014-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.org7e9315b2013-12-04 10:24:26 +0000135 length = restored_length;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000136 EXPECT_TRUE(rtp_parser_->Parse(
137 restored_packet, static_cast<int>(length), &header));
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000138 } else {
139 rtp_rtcp_->SetRemoteSSRC(header.ssrc);
140 }
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000141 return true;
142 }
143
pbos@webrtc.org27326b62013-11-20 12:17:04 +0000144 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000145 return true;
146 }
147
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +0000148 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000149
150 private:
andresp@webrtc.orga714eaf2014-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.orgde1429e2014-04-28 13:00:21 +0000160 void TriggerTestDone() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
andresp@webrtc.orga714eaf2014-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.orgde1429e2014-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.org744fbc72013-09-10 09:26:25 +0000173 scoped_ptr<RtpRtcp> rtp_rtcp_;
pbos@webrtc.org27326b62013-11-20 12:17:04 +0000174 internal::TransportAdapter feedback_transport_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000175 const scoped_ptr<ReceiveStatistics> receive_stats_;
176 const scoped_ptr<RTPPayloadRegistry> payload_registry_;
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000177 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
pbos@webrtc.orgde1429e2014-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.org744fbc72013-09-10 09:26:25 +0000188};
189
henrik.lundin@webrtc.org845862f2014-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.orgde1429e2014-04-28 13:00:21 +0000198 : clock_(clock),
199 number_of_streams_(number_of_streams),
200 rtx_used_(rtx_used),
henrik.lundin@webrtc.org845862f2014-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.orgde1429e2014-04-28 13:00:21 +0000205 crit_(CriticalSectionWrapper::CreateCriticalSection()),
206 send_stream_(NULL),
henrik.lundin@webrtc.org845862f2014-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.org54464e62014-03-13 15:39:27 +0000213 suspended_in_stats_(false) {
henrik.lundin@webrtc.org845862f2014-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.orgaf839b22014-03-24 09:42:08 +0000226 rbe_factory.Create(this, clock, kMimdControl,
227 kRemoteBitrateEstimatorMinBitrateBps));
henrik.lundin@webrtc.org845862f2014-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.orgb10363f2014-03-13 13:31:21 +0000235 virtual void SetSendStream(const VideoSendStream* send_stream) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000236 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000237 send_stream_ = send_stream;
238 }
239
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000240 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
241 unsigned int bitrate) {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000242 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org845862f2014-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.orgde1429e2014-04-28 13:00:21 +0000250 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org845862f2014-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
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000271 virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
272 size_t length) OVERRIDE {
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000273 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000274 RTPHeader header;
275 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
276 receive_stats_->IncomingPacket(header, length, false);
277 remote_bitrate_estimator_->IncomingPacket(
278 clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header);
279 if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) {
280 remote_bitrate_estimator_->Process();
281 }
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000282 suspended_in_stats_ = send_stream_->GetStats().suspended;
pbos@webrtc.orgcaba2d22014-05-14 13:57:12 +0000283 return DELIVERY_OK;
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000284 }
285
286 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
287 return true;
288 }
289
290 // Produces a string similar to "1stream_nortx", depending on the values of
291 // number_of_streams_ and rtx_used_;
292 std::string GetModifierString() {
293 std::string str("_");
294 char temp_str[5];
henrik.lundin@webrtc.org02e749f2014-03-25 13:39:11 +0000295 sprintf(temp_str, "%i", static_cast<int>(number_of_streams_));
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000296 str += std::string(temp_str);
297 str += "stream";
298 str += (number_of_streams_ > 1 ? "s" : "");
299 str += "_";
300 str += (rtx_used_ ? "" : "no");
301 str += "rtx";
302 return str;
303 }
304
305 // This method defines the state machine for the ramp up-down-up test.
306 void EvolveTestState(unsigned int bitrate_bps) {
307 int64_t now = clock_->TimeInMilliseconds();
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000308 CriticalSectionScoped lock(crit_.get());
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000309 assert(send_stream_ != NULL);
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000310 switch (test_state_) {
311 case kFirstRampup: {
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000312 EXPECT_FALSE(suspended_in_stats_);
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000313 if (bitrate_bps > kExpectedHighBitrateBps) {
314 // The first ramp-up has reached the target bitrate. Change the
315 // channel limit, and move to the next test state.
316 forward_transport_config_.link_capacity_kbps =
317 kLowBandwidthLimitBps / 1000;
318 test::DirectTransport::SetConfig(forward_transport_config_);
319 test_state_ = kLowRate;
320 webrtc::test::PrintResult("ramp_up_down_up",
321 GetModifierString(),
322 "first_rampup",
323 now - state_start_ms_,
324 "ms",
325 false);
326 state_start_ms_ = now;
327 interval_start_ms_ = now;
328 sent_bytes_ = 0;
329 }
330 break;
331 }
332 case kLowRate: {
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000333 if (bitrate_bps < kExpectedLowBitrateBps && suspended_in_stats_) {
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000334 // The ramp-down was successful. Change the channel limit back to a
335 // high value, and move to the next test state.
336 forward_transport_config_.link_capacity_kbps =
337 kHighBandwidthLimitBps / 1000;
338 test::DirectTransport::SetConfig(forward_transport_config_);
339 test_state_ = kSecondRampup;
340 webrtc::test::PrintResult("ramp_up_down_up",
341 GetModifierString(),
342 "rampdown",
343 now - state_start_ms_,
344 "ms",
345 false);
346 state_start_ms_ = now;
347 interval_start_ms_ = now;
348 sent_bytes_ = 0;
349 }
350 break;
351 }
352 case kSecondRampup: {
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000353 if (bitrate_bps > kExpectedHighBitrateBps && !suspended_in_stats_) {
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000354 webrtc::test::PrintResult("ramp_up_down_up",
355 GetModifierString(),
356 "second_rampup",
357 now - state_start_ms_,
358 "ms",
359 false);
360 webrtc::test::PrintResult("ramp_up_down_up",
361 GetModifierString(),
362 "total_overuse",
363 total_overuse_bytes_,
364 "bytes",
365 false);
366 test_done_->Set();
367 }
368 break;
369 }
370 }
371 }
372
373 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
374
375 private:
376 static const unsigned int kHighBandwidthLimitBps = 80000;
377 static const unsigned int kExpectedHighBitrateBps = 60000;
378 static const unsigned int kLowBandwidthLimitBps = 20000;
379 static const unsigned int kExpectedLowBitrateBps = 20000;
380 enum TestStates { kFirstRampup, kLowRate, kSecondRampup };
381
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000382 Clock* const clock_;
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000383 const size_t number_of_streams_;
384 const bool rtx_used_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000385 const scoped_ptr<EventWrapper> test_done_;
386 const scoped_ptr<RtpHeaderParser> rtp_parser_;
387 scoped_ptr<RtpRtcp> rtp_rtcp_;
388 internal::TransportAdapter feedback_transport_;
389 const scoped_ptr<ReceiveStatistics> receive_stats_;
390 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
391
392 scoped_ptr<CriticalSectionWrapper> crit_;
393 const VideoSendStream* send_stream_ GUARDED_BY(crit_);
394 FakeNetworkPipe::Config forward_transport_config_ GUARDED_BY(crit_);
395 TestStates test_state_ GUARDED_BY(crit_);
396 int64_t state_start_ms_ GUARDED_BY(crit_);
397 int64_t interval_start_ms_ GUARDED_BY(crit_);
398 unsigned int last_remb_bps_ GUARDED_BY(crit_);
399 size_t sent_bytes_ GUARDED_BY(crit_);
400 size_t total_overuse_bytes_ GUARDED_BY(crit_);
401 bool suspended_in_stats_ GUARDED_BY(crit_);
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000402};
403}
404
405class RampUpTest : public ::testing::Test {
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000406 public:
407 virtual void SetUp() { reserved_ssrcs_.clear(); }
408
409 protected:
andresp@webrtc.orgc1480792014-03-20 03:23:55 +0000410 void RunRampUpTest(bool pacing, bool rtx, size_t num_streams) {
411 std::vector<uint32_t> ssrcs(GenerateSsrcs(num_streams, 100));
412 std::vector<uint32_t> rtx_ssrcs(GenerateSsrcs(num_streams, 200));
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000413 StreamObserver::SsrcMap rtx_ssrc_map;
414 if (rtx) {
415 for (size_t i = 0; i < ssrcs.size(); ++i)
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +0000416 rtx_ssrc_map[rtx_ssrcs[i]] = ssrcs[i];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000417 }
418 test::DirectTransport receiver_transport;
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +0000419 StreamObserver stream_observer(rtx_ssrc_map,
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000420 &receiver_transport,
421 Clock::GetRealTimeClock());
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000422
423 Call::Config call_config(&stream_observer);
424 webrtc::Config webrtc_config;
425 call_config.webrtc_config = &webrtc_config;
426 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
427 scoped_ptr<Call> call(Call::Create(call_config));
428 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
429
430 receiver_transport.SetReceiver(call->Receiver());
431
432 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000433 send_config.encoder_settings =
andresp@webrtc.orgc1480792014-03-20 03:23:55 +0000434 test::CreateEncoderSettings(&encoder, "FAKE", 125, num_streams);
435
436 if (num_streams == 1) {
437 send_config.encoder_settings.streams[0].target_bitrate_bps = 2000000;
438 send_config.encoder_settings.streams[0].max_bitrate_bps = 2000000;
439 }
440
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000441 send_config.pacing = pacing;
442 send_config.rtp.nack.rtp_history_ms = 1000;
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +0000443 send_config.rtp.ssrcs = ssrcs;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000444 if (rtx) {
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000445 send_config.rtp.rtx.payload_type = 96;
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +0000446 send_config.rtp.rtx.ssrcs = rtx_ssrcs;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000447 }
448 send_config.rtp.extensions.push_back(
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000449 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000450
andresp@webrtc.orgc1480792014-03-20 03:23:55 +0000451 if (num_streams == 1) {
452 // For single stream rampup until 1mbps
453 stream_observer.set_expected_bitrate_bps(1000000);
454 } else {
455 // For multi stream rampup until all streams are being sent. That means
456 // enough birate to sent all the target streams plus the min bitrate of
457 // the last one.
458 int expected_bitrate_bps =
459 send_config.encoder_settings.streams.back().min_bitrate_bps;
460 for (size_t i = 0; i < send_config.encoder_settings.streams.size() - 1;
461 ++i) {
462 expected_bitrate_bps +=
463 send_config.encoder_settings.streams[i].target_bitrate_bps;
464 }
465 stream_observer.set_expected_bitrate_bps(expected_bitrate_bps);
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000466 }
467
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000468 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
469
470 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000471 test::FrameGeneratorCapturer::Create(
472 send_stream->Input(),
473 send_config.encoder_settings.streams.back().width,
474 send_config.encoder_settings.streams.back().height,
475 send_config.encoder_settings.streams.back().max_framerate,
476 Clock::GetRealTimeClock()));
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000477
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000478 send_stream->Start();
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000479 frame_generator_capturer->Start();
480
481 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
482
483 frame_generator_capturer->Stop();
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000484 send_stream->Stop();
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000485
486 call->DestroyVideoSendStream(send_stream);
487 }
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000488
489 void RunRampUpDownUpTest(size_t number_of_streams, bool rtx) {
490 std::vector<uint32_t> ssrcs;
491 for (size_t i = 0; i < number_of_streams; ++i)
492 ssrcs.push_back(static_cast<uint32_t>(i + 1));
493 test::DirectTransport receiver_transport;
494 LowRateStreamObserver stream_observer(
495 &receiver_transport, Clock::GetRealTimeClock(), number_of_streams, rtx);
496
497 Call::Config call_config(&stream_observer);
498 webrtc::Config webrtc_config;
499 call_config.webrtc_config = &webrtc_config;
500 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
501 scoped_ptr<Call> call(Call::Create(call_config));
502 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
503
504 receiver_transport.SetReceiver(call->Receiver());
505
506 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000507 send_config.encoder_settings =
508 test::CreateEncoderSettings(&encoder, "FAKE", 125, number_of_streams);
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000509 send_config.rtp.nack.rtp_history_ms = 1000;
510 send_config.rtp.ssrcs.insert(
511 send_config.rtp.ssrcs.begin(), ssrcs.begin(), ssrcs.end());
512 send_config.rtp.extensions.push_back(
513 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
514 send_config.suspend_below_min_bitrate = true;
515
516 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
henrik.lundin@webrtc.orgb10363f2014-03-13 13:31:21 +0000517 stream_observer.SetSendStream(send_stream);
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000518
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000519 size_t width = 0;
520 size_t height = 0;
521 for (size_t i = 0; i < send_config.encoder_settings.streams.size(); ++i) {
522 size_t stream_width = send_config.encoder_settings.streams[i].width;
523 size_t stream_height = send_config.encoder_settings.streams[i].height;
524 if (stream_width > width)
525 width = stream_width;
526 if (stream_height > height)
527 height = stream_height;
528 }
529
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000530 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
531 test::FrameGeneratorCapturer::Create(send_stream->Input(),
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000532 width,
533 height,
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000534 30,
535 Clock::GetRealTimeClock()));
536
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000537 send_stream->Start();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000538 frame_generator_capturer->Start();
539
540 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
541
henrik.lundin@webrtc.org54464e62014-03-13 15:39:27 +0000542 stream_observer.StopSending();
543 receiver_transport.StopSending();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000544 frame_generator_capturer->Stop();
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000545 send_stream->Stop();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000546
547 call->DestroyVideoSendStream(send_stream);
548 }
549
andresp@webrtc.orga714eaf2014-03-17 15:34:57 +0000550 private:
551 std::vector<uint32_t> GenerateSsrcs(size_t num_streams,
552 uint32_t ssrc_offset) {
553 std::vector<uint32_t> ssrcs;
554 for (size_t i = 0; i != num_streams; ++i)
555 ssrcs.push_back(static_cast<uint32_t>(ssrc_offset + i));
556 return ssrcs;
557 }
558
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000559 std::map<uint32_t, bool> reserved_ssrcs_;
560};
561
andresp@webrtc.orgc1480792014-03-20 03:23:55 +0000562TEST_F(RampUpTest, SingleStreamWithoutPacing) {
563 RunRampUpTest(false, false, 1);
564}
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000565
andresp@webrtc.orgc1480792014-03-20 03:23:55 +0000566TEST_F(RampUpTest, SingleStreamWithPacing) {
567 RunRampUpTest(true, false, 1);
568}
569
570TEST_F(RampUpTest, SimulcastWithoutPacing) {
571 RunRampUpTest(false, false, 3);
572}
573
574TEST_F(RampUpTest, SimulcastWithPacing) {
575 RunRampUpTest(true, false, 3);
576}
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000577
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000578// TODO(pbos): Re-enable, webrtc:2992.
andresp@webrtc.orgc1480792014-03-20 03:23:55 +0000579TEST_F(RampUpTest, DISABLED_SimulcastWithPacingAndRtx) {
580 RunRampUpTest(true, true, 3);
581}
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000582
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000583TEST_F(RampUpTest, UpDownUpOneStream) { RunRampUpDownUpTest(1, false); }
584
henrik.lundin@webrtc.org6ea4f632014-03-13 09:21:26 +0000585TEST_F(RampUpTest, UpDownUpThreeStreams) { RunRampUpDownUpTest(3, false); }
henrik.lundin@webrtc.org998cb8f2014-03-06 09:12:00 +0000586
henrik.lundin@webrtc.org6ea4f632014-03-13 09:21:26 +0000587TEST_F(RampUpTest, UpDownUpOneStreamRtx) { RunRampUpDownUpTest(1, true); }
henrik.lundin@webrtc.org998cb8f2014-03-06 09:12:00 +0000588
henrik.lundin@webrtc.org6ea4f632014-03-13 09:21:26 +0000589TEST_F(RampUpTest, UpDownUpThreeStreamsRtx) { RunRampUpDownUpTest(3, true); }
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000590
pbos@webrtc.org744fbc72013-09-10 09:26:25 +0000591} // namespace webrtc