blob: af74bf17e40f8e0b97ee9073aa2727ada7e277d5 [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)
51 : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
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_(
57 new RTPPayloadRegistry(-1,
58 RTPPayloadStrategy::CreateStrategy(false))),
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000059 clock_(clock),
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000060 expected_bitrate_bps_(0),
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +000061 rtx_media_ssrcs_(rtx_media_ssrcs),
62 total_sent_(0),
63 padding_sent_(0),
64 rtx_media_sent_(0),
65 total_packets_sent_(0),
66 padding_packets_sent_(0),
67 rtx_media_packets_sent_(0) {
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000068 // Ideally we would only have to instantiate an RtcpSender, an
69 // RtpHeaderParser and a RemoteBitrateEstimator here, but due to the current
70 // state of the RTP module we need a full module and receive statistics to
71 // be able to produce an RTCP with REMB.
72 RtpRtcp::Configuration config;
73 config.receive_statistics = receive_stats_.get();
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +000074 feedback_transport_.Enable();
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +000075 config.outgoing_transport = &feedback_transport_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000076 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
77 rtp_rtcp_->SetREMBStatus(true);
78 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
pbos@webrtc.org39139dc2013-12-16 12:24:44 +000079 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
80 kAbsoluteSendTimeExtensionId);
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000081 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
henrik.lundin@webrtc.orge6dc4ff2013-12-13 08:42:42 +000082 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 30000;
83 remote_bitrate_estimator_.reset(
stefan@webrtc.org9b2b8ec2014-03-24 09:42:08 +000084 rbe_factory.Create(this, clock, kMimdControl,
85 kRemoteBitrateEstimatorMinBitrateBps));
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +000086 }
87
pbos@webrtc.orgf39df522014-03-19 08:43:57 +000088 void set_expected_bitrate_bps(unsigned int expected_bitrate_bps) {
89 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.org4d1cb142013-09-10 09:26:25 +000094 CriticalSectionScoped lock(critical_section_.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.org4d1cb142013-09-10 09:26:25 +0000108 CriticalSectionScoped lock(critical_section_.get());
109 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
160 void TriggerTestDone() {
161 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.org4d1cb142013-09-10 09:26:25 +0000170 scoped_ptr<CriticalSectionWrapper> critical_section_;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000171 scoped_ptr<EventWrapper> test_done_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000172 scoped_ptr<RtpHeaderParser> rtp_parser_;
173 scoped_ptr<RtpRtcp> rtp_rtcp_;
pbos@webrtc.orgf3b46022013-11-20 12:17:04 +0000174 internal::TransportAdapter feedback_transport_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000175 scoped_ptr<ReceiveStatistics> receive_stats_;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000176 scoped_ptr<RTPPayloadRegistry> payload_registry_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000177 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
178 Clock* clock_;
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000179 unsigned int expected_bitrate_bps_;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000180 SsrcMap rtx_media_ssrcs_;
181 size_t total_sent_;
182 size_t padding_sent_;
183 size_t rtx_media_sent_;
184 int total_packets_sent_;
185 int padding_packets_sent_;
186 int rtx_media_packets_sent_;
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000187};
188
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000189class LowRateStreamObserver : public test::DirectTransport,
190 public RemoteBitrateObserver,
191 public PacketReceiver {
192 public:
193 LowRateStreamObserver(newapi::Transport* feedback_transport,
194 Clock* clock,
195 size_t number_of_streams,
196 bool rtx_used)
197 : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
198 test_done_(EventWrapper::Create()),
199 rtp_parser_(RtpHeaderParser::Create()),
200 feedback_transport_(feedback_transport),
201 receive_stats_(ReceiveStatistics::Create(clock)),
202 clock_(clock),
203 test_state_(kFirstRampup),
204 state_start_ms_(clock_->TimeInMilliseconds()),
205 interval_start_ms_(state_start_ms_),
206 last_remb_bps_(0),
207 sent_bytes_(0),
208 total_overuse_bytes_(0),
209 number_of_streams_(number_of_streams),
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000210 rtx_used_(rtx_used),
211 send_stream_(NULL),
henrik.lundin@webrtc.org3c00b1c2014-03-13 15:39:27 +0000212 suspended_in_stats_(false) {
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000213 RtpRtcp::Configuration config;
214 config.receive_statistics = receive_stats_.get();
215 feedback_transport_.Enable();
216 config.outgoing_transport = &feedback_transport_;
217 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
218 rtp_rtcp_->SetREMBStatus(true);
219 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
220 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
221 kAbsoluteSendTimeExtensionId);
222 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
223 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 10000;
224 remote_bitrate_estimator_.reset(
stefan@webrtc.org9b2b8ec2014-03-24 09:42:08 +0000225 rbe_factory.Create(this, clock, kMimdControl,
226 kRemoteBitrateEstimatorMinBitrateBps));
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000227 forward_transport_config_.link_capacity_kbps =
228 kHighBandwidthLimitBps / 1000;
229 forward_transport_config_.queue_length = 100; // Something large.
230 test::DirectTransport::SetConfig(forward_transport_config_);
231 test::DirectTransport::SetReceiver(this);
232 }
233
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000234 virtual void SetSendStream(const VideoSendStream* send_stream) {
235 send_stream_ = send_stream;
236 }
237
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000238 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
239 unsigned int bitrate) {
240 CriticalSectionScoped lock(critical_section_.get());
241 rtp_rtcp_->SetREMBData(
242 bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]);
243 rtp_rtcp_->Process();
244 last_remb_bps_ = bitrate;
245 }
246
247 virtual bool SendRtp(const uint8_t* data, size_t length) OVERRIDE {
248 sent_bytes_ += length;
249 int64_t now_ms = clock_->TimeInMilliseconds();
250 if (now_ms > interval_start_ms_ + 1000) { // Let at least 1 second pass.
251 // Verify that the send rate was about right.
252 unsigned int average_rate_bps = static_cast<unsigned int>(sent_bytes_) *
253 8 * 1000 / (now_ms - interval_start_ms_);
254 // TODO(holmer): Why is this failing?
255 // EXPECT_LT(average_rate_bps, last_remb_bps_ * 1.1);
256 if (average_rate_bps > last_remb_bps_ * 1.1) {
257 total_overuse_bytes_ +=
258 sent_bytes_ -
259 last_remb_bps_ / 8 * (now_ms - interval_start_ms_) / 1000;
260 }
261 EvolveTestState(average_rate_bps);
262 interval_start_ms_ = now_ms;
263 sent_bytes_ = 0;
264 }
265 return test::DirectTransport::SendRtp(data, length);
266 }
267
268 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE {
269 CriticalSectionScoped lock(critical_section_.get());
270 RTPHeader header;
271 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
272 receive_stats_->IncomingPacket(header, length, false);
273 remote_bitrate_estimator_->IncomingPacket(
274 clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header);
275 if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) {
276 remote_bitrate_estimator_->Process();
277 }
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000278 suspended_in_stats_ = send_stream_->GetStats().suspended;
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000279 return true;
280 }
281
282 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
283 return true;
284 }
285
286 // Produces a string similar to "1stream_nortx", depending on the values of
287 // number_of_streams_ and rtx_used_;
288 std::string GetModifierString() {
289 std::string str("_");
290 char temp_str[5];
henrik.lundin@webrtc.org0b117152014-03-25 13:39:11 +0000291 sprintf(temp_str, "%i", static_cast<int>(number_of_streams_));
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000292 str += std::string(temp_str);
293 str += "stream";
294 str += (number_of_streams_ > 1 ? "s" : "");
295 str += "_";
296 str += (rtx_used_ ? "" : "no");
297 str += "rtx";
298 return str;
299 }
300
301 // This method defines the state machine for the ramp up-down-up test.
302 void EvolveTestState(unsigned int bitrate_bps) {
303 int64_t now = clock_->TimeInMilliseconds();
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000304 assert(send_stream_ != NULL);
305 CriticalSectionScoped lock(critical_section_.get());
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000306 switch (test_state_) {
307 case kFirstRampup: {
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000308 EXPECT_FALSE(suspended_in_stats_);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000309 if (bitrate_bps > kExpectedHighBitrateBps) {
310 // The first ramp-up has reached the target bitrate. Change the
311 // channel limit, and move to the next test state.
312 forward_transport_config_.link_capacity_kbps =
313 kLowBandwidthLimitBps / 1000;
314 test::DirectTransport::SetConfig(forward_transport_config_);
315 test_state_ = kLowRate;
316 webrtc::test::PrintResult("ramp_up_down_up",
317 GetModifierString(),
318 "first_rampup",
319 now - state_start_ms_,
320 "ms",
321 false);
322 state_start_ms_ = now;
323 interval_start_ms_ = now;
324 sent_bytes_ = 0;
325 }
326 break;
327 }
328 case kLowRate: {
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000329 if (bitrate_bps < kExpectedLowBitrateBps && suspended_in_stats_) {
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000330 // The ramp-down was successful. Change the channel limit back to a
331 // high value, and move to the next test state.
332 forward_transport_config_.link_capacity_kbps =
333 kHighBandwidthLimitBps / 1000;
334 test::DirectTransport::SetConfig(forward_transport_config_);
335 test_state_ = kSecondRampup;
336 webrtc::test::PrintResult("ramp_up_down_up",
337 GetModifierString(),
338 "rampdown",
339 now - state_start_ms_,
340 "ms",
341 false);
342 state_start_ms_ = now;
343 interval_start_ms_ = now;
344 sent_bytes_ = 0;
345 }
346 break;
347 }
348 case kSecondRampup: {
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000349 if (bitrate_bps > kExpectedHighBitrateBps && !suspended_in_stats_) {
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000350 webrtc::test::PrintResult("ramp_up_down_up",
351 GetModifierString(),
352 "second_rampup",
353 now - state_start_ms_,
354 "ms",
355 false);
356 webrtc::test::PrintResult("ramp_up_down_up",
357 GetModifierString(),
358 "total_overuse",
359 total_overuse_bytes_,
360 "bytes",
361 false);
362 test_done_->Set();
363 }
364 break;
365 }
366 }
367 }
368
369 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
370
371 private:
372 static const unsigned int kHighBandwidthLimitBps = 80000;
373 static const unsigned int kExpectedHighBitrateBps = 60000;
374 static const unsigned int kLowBandwidthLimitBps = 20000;
375 static const unsigned int kExpectedLowBitrateBps = 20000;
376 enum TestStates { kFirstRampup, kLowRate, kSecondRampup };
377
378 scoped_ptr<CriticalSectionWrapper> critical_section_;
379 scoped_ptr<EventWrapper> test_done_;
380 scoped_ptr<RtpHeaderParser> rtp_parser_;
381 scoped_ptr<RtpRtcp> rtp_rtcp_;
382 internal::TransportAdapter feedback_transport_;
383 scoped_ptr<ReceiveStatistics> receive_stats_;
384 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
385 Clock* clock_;
386 FakeNetworkPipe::Config forward_transport_config_;
387 TestStates test_state_;
388 int64_t state_start_ms_;
389 int64_t interval_start_ms_;
390 unsigned int last_remb_bps_;
391 size_t sent_bytes_;
392 size_t total_overuse_bytes_;
393 const size_t number_of_streams_;
394 const bool rtx_used_;
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000395 const VideoSendStream* send_stream_;
396 bool suspended_in_stats_ GUARDED_BY(critical_section_);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000397};
398}
399
400class RampUpTest : public ::testing::Test {
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000401 public:
402 virtual void SetUp() { reserved_ssrcs_.clear(); }
403
404 protected:
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000405 void RunRampUpTest(bool pacing, bool rtx, size_t num_streams) {
406 std::vector<uint32_t> ssrcs(GenerateSsrcs(num_streams, 100));
407 std::vector<uint32_t> rtx_ssrcs(GenerateSsrcs(num_streams, 200));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000408 StreamObserver::SsrcMap rtx_ssrc_map;
409 if (rtx) {
410 for (size_t i = 0; i < ssrcs.size(); ++i)
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000411 rtx_ssrc_map[rtx_ssrcs[i]] = ssrcs[i];
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000412 }
413 test::DirectTransport receiver_transport;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000414 StreamObserver stream_observer(rtx_ssrc_map,
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000415 &receiver_transport,
416 Clock::GetRealTimeClock());
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000417
418 Call::Config call_config(&stream_observer);
419 webrtc::Config webrtc_config;
420 call_config.webrtc_config = &webrtc_config;
421 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
422 scoped_ptr<Call> call(Call::Create(call_config));
423 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
424
425 receiver_transport.SetReceiver(call->Receiver());
426
427 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000428 send_config.encoder_settings =
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000429 test::CreateEncoderSettings(&encoder, "FAKE", 125, num_streams);
430
431 if (num_streams == 1) {
432 send_config.encoder_settings.streams[0].target_bitrate_bps = 2000000;
433 send_config.encoder_settings.streams[0].max_bitrate_bps = 2000000;
434 }
435
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000436 send_config.pacing = pacing;
437 send_config.rtp.nack.rtp_history_ms = 1000;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000438 send_config.rtp.ssrcs = ssrcs;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000439 if (rtx) {
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000440 send_config.rtp.rtx.payload_type = 96;
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000441 send_config.rtp.rtx.ssrcs = rtx_ssrcs;
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000442 }
443 send_config.rtp.extensions.push_back(
pbos@webrtc.org39139dc2013-12-16 12:24:44 +0000444 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000445
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000446 if (num_streams == 1) {
447 // For single stream rampup until 1mbps
448 stream_observer.set_expected_bitrate_bps(1000000);
449 } else {
450 // For multi stream rampup until all streams are being sent. That means
451 // enough birate to sent all the target streams plus the min bitrate of
452 // the last one.
453 int expected_bitrate_bps =
454 send_config.encoder_settings.streams.back().min_bitrate_bps;
455 for (size_t i = 0; i < send_config.encoder_settings.streams.size() - 1;
456 ++i) {
457 expected_bitrate_bps +=
458 send_config.encoder_settings.streams[i].target_bitrate_bps;
459 }
460 stream_observer.set_expected_bitrate_bps(expected_bitrate_bps);
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000461 }
462
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000463 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
464
465 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000466 test::FrameGeneratorCapturer::Create(
467 send_stream->Input(),
468 send_config.encoder_settings.streams.back().width,
469 send_config.encoder_settings.streams.back().height,
470 send_config.encoder_settings.streams.back().max_framerate,
471 Clock::GetRealTimeClock()));
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000472
473 send_stream->StartSending();
474 frame_generator_capturer->Start();
475
476 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
477
478 frame_generator_capturer->Stop();
479 send_stream->StopSending();
480
481 call->DestroyVideoSendStream(send_stream);
482 }
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000483
484 void RunRampUpDownUpTest(size_t number_of_streams, bool rtx) {
485 std::vector<uint32_t> ssrcs;
486 for (size_t i = 0; i < number_of_streams; ++i)
487 ssrcs.push_back(static_cast<uint32_t>(i + 1));
488 test::DirectTransport receiver_transport;
489 LowRateStreamObserver stream_observer(
490 &receiver_transport, Clock::GetRealTimeClock(), number_of_streams, rtx);
491
492 Call::Config call_config(&stream_observer);
493 webrtc::Config webrtc_config;
494 call_config.webrtc_config = &webrtc_config;
495 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
496 scoped_ptr<Call> call(Call::Create(call_config));
497 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
498
499 receiver_transport.SetReceiver(call->Receiver());
500
501 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000502 send_config.encoder_settings =
503 test::CreateEncoderSettings(&encoder, "FAKE", 125, number_of_streams);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000504 send_config.rtp.nack.rtp_history_ms = 1000;
505 send_config.rtp.ssrcs.insert(
506 send_config.rtp.ssrcs.begin(), ssrcs.begin(), ssrcs.end());
507 send_config.rtp.extensions.push_back(
508 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
509 send_config.suspend_below_min_bitrate = true;
510
511 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
henrik.lundin@webrtc.org15cf7172014-03-13 13:31:21 +0000512 stream_observer.SetSendStream(send_stream);
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000513
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000514 size_t width = 0;
515 size_t height = 0;
516 for (size_t i = 0; i < send_config.encoder_settings.streams.size(); ++i) {
517 size_t stream_width = send_config.encoder_settings.streams[i].width;
518 size_t stream_height = send_config.encoder_settings.streams[i].height;
519 if (stream_width > width)
520 width = stream_width;
521 if (stream_height > height)
522 height = stream_height;
523 }
524
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000525 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
526 test::FrameGeneratorCapturer::Create(send_stream->Input(),
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000527 width,
528 height,
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000529 30,
530 Clock::GetRealTimeClock()));
531
532 send_stream->StartSending();
533 frame_generator_capturer->Start();
534
535 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
536
henrik.lundin@webrtc.org3c00b1c2014-03-13 15:39:27 +0000537 stream_observer.StopSending();
538 receiver_transport.StopSending();
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000539 frame_generator_capturer->Stop();
540 send_stream->StopSending();
541
542 call->DestroyVideoSendStream(send_stream);
543 }
544
andresp@webrtc.orgca626eb2014-03-17 15:34:57 +0000545 private:
546 std::vector<uint32_t> GenerateSsrcs(size_t num_streams,
547 uint32_t ssrc_offset) {
548 std::vector<uint32_t> ssrcs;
549 for (size_t i = 0; i != num_streams; ++i)
550 ssrcs.push_back(static_cast<uint32_t>(ssrc_offset + i));
551 return ssrcs;
552 }
553
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000554 std::map<uint32_t, bool> reserved_ssrcs_;
555};
556
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000557TEST_F(RampUpTest, SingleStreamWithoutPacing) {
558 RunRampUpTest(false, false, 1);
559}
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000560
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000561TEST_F(RampUpTest, SingleStreamWithPacing) {
562 RunRampUpTest(true, false, 1);
563}
564
565TEST_F(RampUpTest, SimulcastWithoutPacing) {
566 RunRampUpTest(false, false, 3);
567}
568
569TEST_F(RampUpTest, SimulcastWithPacing) {
570 RunRampUpTest(true, false, 3);
571}
stefan@webrtc.orgda3ae7c2013-12-04 10:24:26 +0000572
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000573// TODO(pbos): Re-enable, webrtc:2992.
andresp@webrtc.orga183edc2014-03-20 03:23:55 +0000574TEST_F(RampUpTest, DISABLED_SimulcastWithPacingAndRtx) {
575 RunRampUpTest(true, true, 3);
576}
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000577
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000578TEST_F(RampUpTest, UpDownUpOneStream) { RunRampUpDownUpTest(1, false); }
579
henrik.lundin@webrtc.org41da3292014-03-13 09:21:26 +0000580TEST_F(RampUpTest, UpDownUpThreeStreams) { RunRampUpDownUpTest(3, false); }
henrik.lundin@webrtc.orgc63f18d2014-03-06 09:12:00 +0000581
henrik.lundin@webrtc.org41da3292014-03-13 09:21:26 +0000582TEST_F(RampUpTest, UpDownUpOneStreamRtx) { RunRampUpDownUpTest(1, true); }
henrik.lundin@webrtc.orgc63f18d2014-03-06 09:12:00 +0000583
henrik.lundin@webrtc.org41da3292014-03-13 09:21:26 +0000584TEST_F(RampUpTest, UpDownUpThreeStreamsRtx) { RunRampUpDownUpTest(3, true); }
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000585
pbos@webrtc.org4d1cb142013-09-10 09:26:25 +0000586} // namespace webrtc