blob: ca6fb10bef4cc69483d527a9fb9e2eedfe1cdcd8 [file] [log] [blame]
pbos@webrtc.orgfa996f22013-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.orgc7660982014-03-06 07:19:28 +000013#include <string>
stefan@webrtc.org47f0c412013-12-04 10:24:26 +000014#include <vector>
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000015
16#include "testing/gtest/include/gtest/gtest.h"
17
pbos@webrtc.org24e20892013-10-28 16:32:01 +000018#include "webrtc/call.h"
stefan@webrtc.org47f0c412013-12-04 10:24:26 +000019#include "webrtc/common.h"
20#include "webrtc/experiments.h"
pbos@webrtc.orgfa996f22013-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.org47f0c412013-12-04 10:24:26 +000024#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
pbos@webrtc.orgfa996f22013-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.org24e20892013-10-28 16:32:01 +000030#include "webrtc/test/direct_transport.h"
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +000031#include "webrtc/test/encoder_settings.h"
pbos@webrtc.org24e20892013-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.org47f0c412013-12-04 10:24:26 +000035#include "webrtc/test/testsupport/perf_test.h"
pbos@webrtc.org3009c812013-11-20 12:17:04 +000036#include "webrtc/video/transport_adapter.h"
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000037
38namespace webrtc {
39
pbos@webrtc.org990c5e32013-09-11 10:14:56 +000040namespace {
pbos@webrtc.orgc71929d2014-01-24 09:30:53 +000041static const int kAbsoluteSendTimeExtensionId = 7;
42static const int kMaxPacketSize = 1500;
pbos@webrtc.org990c5e32013-09-11 10:14:56 +000043
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000044class StreamObserver : public newapi::Transport, public RemoteBitrateObserver {
45 public:
46 typedef std::map<uint32_t, int> BytesSentMap;
stefan@webrtc.org47f0c412013-12-04 10:24:26 +000047 typedef std::map<uint32_t, uint32_t> SsrcMap;
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +000048 StreamObserver(const SsrcMap& rtx_media_ssrcs,
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000049 newapi::Transport* feedback_transport,
50 Clock* clock)
51 : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +000052 test_done_(EventWrapper::Create()),
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000053 rtp_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org3009c812013-11-20 12:17:04 +000054 feedback_transport_(feedback_transport),
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000055 receive_stats_(ReceiveStatistics::Create(clock)),
pbos@webrtc.orgc71929d2014-01-24 09:30:53 +000056 payload_registry_(
andresp@webrtc.org99681312014-04-08 11:06:12 +000057 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(false))),
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000058 clock_(clock),
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +000059 expected_bitrate_bps_(0),
stefan@webrtc.org47f0c412013-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.orgfa996f22013-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.org0a298152014-01-27 13:03:02 +000073 feedback_transport_.Enable();
pbos@webrtc.org3009c812013-11-20 12:17:04 +000074 config.outgoing_transport = &feedback_transport_;
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000075 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
76 rtp_rtcp_->SetREMBStatus(true);
77 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
pbos@webrtc.org46f72882013-12-16 12:24:44 +000078 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
79 kAbsoluteSendTimeExtensionId);
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000080 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
henrik.lundin@webrtc.orgc49a3fa2013-12-13 08:42:42 +000081 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 30000;
82 remote_bitrate_estimator_.reset(
stefan@webrtc.orgb9d0acb2014-03-24 09:42:08 +000083 rbe_factory.Create(this, clock, kMimdControl,
84 kRemoteBitrateEstimatorMinBitrateBps));
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000085 }
86
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +000087 void set_expected_bitrate_bps(unsigned int expected_bitrate_bps) {
88 expected_bitrate_bps_ = expected_bitrate_bps;
89 }
90
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000091 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +000092 unsigned int bitrate) OVERRIDE {
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +000093 CriticalSectionScoped lock(critical_section_.get());
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +000094 assert(expected_bitrate_bps_ > 0);
95 if (bitrate >= expected_bitrate_bps_) {
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +000096 // Just trigger if there was any rtx padding packet.
stefan@webrtc.org47f0c412013-12-04 10:24:26 +000097 if (rtx_media_ssrcs_.empty() || rtx_media_sent_ > 0) {
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +000098 TriggerTestDone();
stefan@webrtc.org47f0c412013-12-04 10:24:26 +000099 }
100 }
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000101 rtp_rtcp_->SetREMBData(
102 bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]);
103 rtp_rtcp_->Process();
104 }
105
pbos@webrtc.org3009c812013-11-20 12:17:04 +0000106 virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000107 CriticalSectionScoped lock(critical_section_.get());
108 RTPHeader header;
109 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
110 receive_stats_->IncomingPacket(header, length, false);
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000111 payload_registry_->SetIncomingPayloadType(header);
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000112 remote_bitrate_estimator_->IncomingPacket(
113 clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header);
114 if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) {
115 remote_bitrate_estimator_->Process();
116 }
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000117 total_sent_ += length;
118 padding_sent_ += header.paddingLength;
119 ++total_packets_sent_;
120 if (header.paddingLength > 0)
121 ++padding_packets_sent_;
122 if (rtx_media_ssrcs_.find(header.ssrc) != rtx_media_ssrcs_.end()) {
123 rtx_media_sent_ += length - header.headerLength - header.paddingLength;
124 if (header.paddingLength == 0)
125 ++rtx_media_packets_sent_;
126 uint8_t restored_packet[kMaxPacketSize];
127 uint8_t* restored_packet_ptr = restored_packet;
128 int restored_length = static_cast<int>(length);
pbos@webrtc.orgc71929d2014-01-24 09:30:53 +0000129 payload_registry_->RestoreOriginalPacket(&restored_packet_ptr,
130 packet,
131 &restored_length,
132 rtx_media_ssrcs_[header.ssrc],
133 header);
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000134 length = restored_length;
pbos@webrtc.orgc71929d2014-01-24 09:30:53 +0000135 EXPECT_TRUE(rtp_parser_->Parse(
136 restored_packet, static_cast<int>(length), &header));
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000137 } else {
138 rtp_rtcp_->SetRemoteSSRC(header.ssrc);
139 }
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000140 return true;
141 }
142
pbos@webrtc.org3009c812013-11-20 12:17:04 +0000143 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000144 return true;
145 }
146
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000147 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000148
149 private:
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000150 void ReportResult(const std::string& measurement,
151 size_t value,
152 const std::string& units) {
153 webrtc::test::PrintResult(
154 measurement, "",
155 ::testing::UnitTest::GetInstance()->current_test_info()->name(),
156 value, units, false);
157 }
158
159 void TriggerTestDone() {
160 ReportResult("total-sent", total_sent_, "bytes");
161 ReportResult("padding-sent", padding_sent_, "bytes");
162 ReportResult("rtx-media-sent", rtx_media_sent_, "bytes");
163 ReportResult("total-packets-sent", total_packets_sent_, "packets");
164 ReportResult("padding-packets-sent", padding_packets_sent_, "packets");
165 ReportResult("rtx-packets-sent", rtx_media_packets_sent_, "packets");
166 test_done_->Set();
167 }
168
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000169 scoped_ptr<CriticalSectionWrapper> critical_section_;
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000170 scoped_ptr<EventWrapper> test_done_;
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000171 scoped_ptr<RtpHeaderParser> rtp_parser_;
172 scoped_ptr<RtpRtcp> rtp_rtcp_;
pbos@webrtc.org3009c812013-11-20 12:17:04 +0000173 internal::TransportAdapter feedback_transport_;
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000174 scoped_ptr<ReceiveStatistics> receive_stats_;
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000175 scoped_ptr<RTPPayloadRegistry> payload_registry_;
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000176 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
177 Clock* clock_;
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000178 unsigned int expected_bitrate_bps_;
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000179 SsrcMap rtx_media_ssrcs_;
180 size_t total_sent_;
181 size_t padding_sent_;
182 size_t rtx_media_sent_;
183 int total_packets_sent_;
184 int padding_packets_sent_;
185 int rtx_media_packets_sent_;
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000186};
187
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000188class LowRateStreamObserver : public test::DirectTransport,
189 public RemoteBitrateObserver,
190 public PacketReceiver {
191 public:
192 LowRateStreamObserver(newapi::Transport* feedback_transport,
193 Clock* clock,
194 size_t number_of_streams,
195 bool rtx_used)
196 : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
197 test_done_(EventWrapper::Create()),
198 rtp_parser_(RtpHeaderParser::Create()),
199 feedback_transport_(feedback_transport),
200 receive_stats_(ReceiveStatistics::Create(clock)),
201 clock_(clock),
202 test_state_(kFirstRampup),
203 state_start_ms_(clock_->TimeInMilliseconds()),
204 interval_start_ms_(state_start_ms_),
205 last_remb_bps_(0),
206 sent_bytes_(0),
207 total_overuse_bytes_(0),
208 number_of_streams_(number_of_streams),
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000209 rtx_used_(rtx_used),
210 send_stream_(NULL),
henrik.lundin@webrtc.org96616cb2014-03-13 15:39:27 +0000211 suspended_in_stats_(false) {
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000212 RtpRtcp::Configuration config;
213 config.receive_statistics = receive_stats_.get();
214 feedback_transport_.Enable();
215 config.outgoing_transport = &feedback_transport_;
216 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
217 rtp_rtcp_->SetREMBStatus(true);
218 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
219 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
220 kAbsoluteSendTimeExtensionId);
221 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
222 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 10000;
223 remote_bitrate_estimator_.reset(
stefan@webrtc.orgb9d0acb2014-03-24 09:42:08 +0000224 rbe_factory.Create(this, clock, kMimdControl,
225 kRemoteBitrateEstimatorMinBitrateBps));
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000226 forward_transport_config_.link_capacity_kbps =
227 kHighBandwidthLimitBps / 1000;
228 forward_transport_config_.queue_length = 100; // Something large.
229 test::DirectTransport::SetConfig(forward_transport_config_);
230 test::DirectTransport::SetReceiver(this);
231 }
232
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000233 virtual void SetSendStream(const VideoSendStream* send_stream) {
234 send_stream_ = send_stream;
235 }
236
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000237 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
238 unsigned int bitrate) {
239 CriticalSectionScoped lock(critical_section_.get());
240 rtp_rtcp_->SetREMBData(
241 bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]);
242 rtp_rtcp_->Process();
243 last_remb_bps_ = bitrate;
244 }
245
246 virtual bool SendRtp(const uint8_t* data, size_t length) OVERRIDE {
247 sent_bytes_ += length;
248 int64_t now_ms = clock_->TimeInMilliseconds();
249 if (now_ms > interval_start_ms_ + 1000) { // Let at least 1 second pass.
250 // Verify that the send rate was about right.
251 unsigned int average_rate_bps = static_cast<unsigned int>(sent_bytes_) *
252 8 * 1000 / (now_ms - interval_start_ms_);
253 // TODO(holmer): Why is this failing?
254 // EXPECT_LT(average_rate_bps, last_remb_bps_ * 1.1);
255 if (average_rate_bps > last_remb_bps_ * 1.1) {
256 total_overuse_bytes_ +=
257 sent_bytes_ -
258 last_remb_bps_ / 8 * (now_ms - interval_start_ms_) / 1000;
259 }
260 EvolveTestState(average_rate_bps);
261 interval_start_ms_ = now_ms;
262 sent_bytes_ = 0;
263 }
264 return test::DirectTransport::SendRtp(data, length);
265 }
266
267 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE {
268 CriticalSectionScoped lock(critical_section_.get());
269 RTPHeader header;
270 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
271 receive_stats_->IncomingPacket(header, length, false);
272 remote_bitrate_estimator_->IncomingPacket(
273 clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header);
274 if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) {
275 remote_bitrate_estimator_->Process();
276 }
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000277 suspended_in_stats_ = send_stream_->GetStats().suspended;
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000278 return true;
279 }
280
281 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
282 return true;
283 }
284
285 // Produces a string similar to "1stream_nortx", depending on the values of
286 // number_of_streams_ and rtx_used_;
287 std::string GetModifierString() {
288 std::string str("_");
289 char temp_str[5];
henrik.lundin@webrtc.org9deb87b2014-03-25 13:39:11 +0000290 sprintf(temp_str, "%i", static_cast<int>(number_of_streams_));
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000291 str += std::string(temp_str);
292 str += "stream";
293 str += (number_of_streams_ > 1 ? "s" : "");
294 str += "_";
295 str += (rtx_used_ ? "" : "no");
296 str += "rtx";
297 return str;
298 }
299
300 // This method defines the state machine for the ramp up-down-up test.
301 void EvolveTestState(unsigned int bitrate_bps) {
302 int64_t now = clock_->TimeInMilliseconds();
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000303 assert(send_stream_ != NULL);
304 CriticalSectionScoped lock(critical_section_.get());
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000305 switch (test_state_) {
306 case kFirstRampup: {
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000307 EXPECT_FALSE(suspended_in_stats_);
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000308 if (bitrate_bps > kExpectedHighBitrateBps) {
309 // The first ramp-up has reached the target bitrate. Change the
310 // channel limit, and move to the next test state.
311 forward_transport_config_.link_capacity_kbps =
312 kLowBandwidthLimitBps / 1000;
313 test::DirectTransport::SetConfig(forward_transport_config_);
314 test_state_ = kLowRate;
315 webrtc::test::PrintResult("ramp_up_down_up",
316 GetModifierString(),
317 "first_rampup",
318 now - state_start_ms_,
319 "ms",
320 false);
321 state_start_ms_ = now;
322 interval_start_ms_ = now;
323 sent_bytes_ = 0;
324 }
325 break;
326 }
327 case kLowRate: {
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000328 if (bitrate_bps < kExpectedLowBitrateBps && suspended_in_stats_) {
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000329 // The ramp-down was successful. Change the channel limit back to a
330 // high value, and move to the next test state.
331 forward_transport_config_.link_capacity_kbps =
332 kHighBandwidthLimitBps / 1000;
333 test::DirectTransport::SetConfig(forward_transport_config_);
334 test_state_ = kSecondRampup;
335 webrtc::test::PrintResult("ramp_up_down_up",
336 GetModifierString(),
337 "rampdown",
338 now - state_start_ms_,
339 "ms",
340 false);
341 state_start_ms_ = now;
342 interval_start_ms_ = now;
343 sent_bytes_ = 0;
344 }
345 break;
346 }
347 case kSecondRampup: {
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000348 if (bitrate_bps > kExpectedHighBitrateBps && !suspended_in_stats_) {
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000349 webrtc::test::PrintResult("ramp_up_down_up",
350 GetModifierString(),
351 "second_rampup",
352 now - state_start_ms_,
353 "ms",
354 false);
355 webrtc::test::PrintResult("ramp_up_down_up",
356 GetModifierString(),
357 "total_overuse",
358 total_overuse_bytes_,
359 "bytes",
360 false);
361 test_done_->Set();
362 }
363 break;
364 }
365 }
366 }
367
368 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
369
370 private:
371 static const unsigned int kHighBandwidthLimitBps = 80000;
372 static const unsigned int kExpectedHighBitrateBps = 60000;
373 static const unsigned int kLowBandwidthLimitBps = 20000;
374 static const unsigned int kExpectedLowBitrateBps = 20000;
375 enum TestStates { kFirstRampup, kLowRate, kSecondRampup };
376
377 scoped_ptr<CriticalSectionWrapper> critical_section_;
378 scoped_ptr<EventWrapper> test_done_;
379 scoped_ptr<RtpHeaderParser> rtp_parser_;
380 scoped_ptr<RtpRtcp> rtp_rtcp_;
381 internal::TransportAdapter feedback_transport_;
382 scoped_ptr<ReceiveStatistics> receive_stats_;
383 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
384 Clock* clock_;
385 FakeNetworkPipe::Config forward_transport_config_;
386 TestStates test_state_;
387 int64_t state_start_ms_;
388 int64_t interval_start_ms_;
389 unsigned int last_remb_bps_;
390 size_t sent_bytes_;
391 size_t total_overuse_bytes_;
392 const size_t number_of_streams_;
393 const bool rtx_used_;
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000394 const VideoSendStream* send_stream_;
395 bool suspended_in_stats_ GUARDED_BY(critical_section_);
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000396};
397}
398
399class RampUpTest : public ::testing::Test {
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000400 public:
401 virtual void SetUp() { reserved_ssrcs_.clear(); }
402
403 protected:
andresp@webrtc.org700d14b2014-03-20 03:23:55 +0000404 void RunRampUpTest(bool pacing, bool rtx, size_t num_streams) {
405 std::vector<uint32_t> ssrcs(GenerateSsrcs(num_streams, 100));
406 std::vector<uint32_t> rtx_ssrcs(GenerateSsrcs(num_streams, 200));
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000407 StreamObserver::SsrcMap rtx_ssrc_map;
408 if (rtx) {
409 for (size_t i = 0; i < ssrcs.size(); ++i)
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000410 rtx_ssrc_map[rtx_ssrcs[i]] = ssrcs[i];
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000411 }
412 test::DirectTransport receiver_transport;
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000413 StreamObserver stream_observer(rtx_ssrc_map,
pbos@webrtc.orgc71929d2014-01-24 09:30:53 +0000414 &receiver_transport,
415 Clock::GetRealTimeClock());
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000416
417 Call::Config call_config(&stream_observer);
418 webrtc::Config webrtc_config;
419 call_config.webrtc_config = &webrtc_config;
420 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
421 scoped_ptr<Call> call(Call::Create(call_config));
422 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
423
424 receiver_transport.SetReceiver(call->Receiver());
425
426 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000427 send_config.encoder_settings =
andresp@webrtc.org700d14b2014-03-20 03:23:55 +0000428 test::CreateEncoderSettings(&encoder, "FAKE", 125, num_streams);
429
430 if (num_streams == 1) {
431 send_config.encoder_settings.streams[0].target_bitrate_bps = 2000000;
432 send_config.encoder_settings.streams[0].max_bitrate_bps = 2000000;
433 }
434
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000435 send_config.pacing = pacing;
436 send_config.rtp.nack.rtp_history_ms = 1000;
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000437 send_config.rtp.ssrcs = ssrcs;
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000438 if (rtx) {
pbos@webrtc.orgc71929d2014-01-24 09:30:53 +0000439 send_config.rtp.rtx.payload_type = 96;
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000440 send_config.rtp.rtx.ssrcs = rtx_ssrcs;
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000441 }
442 send_config.rtp.extensions.push_back(
pbos@webrtc.org46f72882013-12-16 12:24:44 +0000443 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000444
andresp@webrtc.org700d14b2014-03-20 03:23:55 +0000445 if (num_streams == 1) {
446 // For single stream rampup until 1mbps
447 stream_observer.set_expected_bitrate_bps(1000000);
448 } else {
449 // For multi stream rampup until all streams are being sent. That means
450 // enough birate to sent all the target streams plus the min bitrate of
451 // the last one.
452 int expected_bitrate_bps =
453 send_config.encoder_settings.streams.back().min_bitrate_bps;
454 for (size_t i = 0; i < send_config.encoder_settings.streams.size() - 1;
455 ++i) {
456 expected_bitrate_bps +=
457 send_config.encoder_settings.streams[i].target_bitrate_bps;
458 }
459 stream_observer.set_expected_bitrate_bps(expected_bitrate_bps);
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000460 }
461
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000462 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
463
464 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000465 test::FrameGeneratorCapturer::Create(
466 send_stream->Input(),
467 send_config.encoder_settings.streams.back().width,
468 send_config.encoder_settings.streams.back().height,
469 send_config.encoder_settings.streams.back().max_framerate,
470 Clock::GetRealTimeClock()));
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000471
472 send_stream->StartSending();
473 frame_generator_capturer->Start();
474
475 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
476
477 frame_generator_capturer->Stop();
478 send_stream->StopSending();
479
480 call->DestroyVideoSendStream(send_stream);
481 }
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000482
483 void RunRampUpDownUpTest(size_t number_of_streams, bool rtx) {
484 std::vector<uint32_t> ssrcs;
485 for (size_t i = 0; i < number_of_streams; ++i)
486 ssrcs.push_back(static_cast<uint32_t>(i + 1));
487 test::DirectTransport receiver_transport;
488 LowRateStreamObserver stream_observer(
489 &receiver_transport, Clock::GetRealTimeClock(), number_of_streams, rtx);
490
491 Call::Config call_config(&stream_observer);
492 webrtc::Config webrtc_config;
493 call_config.webrtc_config = &webrtc_config;
494 webrtc_config.Set<PaddingStrategy>(new PaddingStrategy(rtx));
495 scoped_ptr<Call> call(Call::Create(call_config));
496 VideoSendStream::Config send_config = call->GetDefaultSendConfig();
497
498 receiver_transport.SetReceiver(call->Receiver());
499
500 test::FakeEncoder encoder(Clock::GetRealTimeClock());
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000501 send_config.encoder_settings =
502 test::CreateEncoderSettings(&encoder, "FAKE", 125, number_of_streams);
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000503 send_config.rtp.nack.rtp_history_ms = 1000;
504 send_config.rtp.ssrcs.insert(
505 send_config.rtp.ssrcs.begin(), ssrcs.begin(), ssrcs.end());
506 send_config.rtp.extensions.push_back(
507 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
508 send_config.suspend_below_min_bitrate = true;
509
510 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
henrik.lundin@webrtc.org9376c692014-03-13 13:31:21 +0000511 stream_observer.SetSendStream(send_stream);
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000512
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000513 size_t width = 0;
514 size_t height = 0;
515 for (size_t i = 0; i < send_config.encoder_settings.streams.size(); ++i) {
516 size_t stream_width = send_config.encoder_settings.streams[i].width;
517 size_t stream_height = send_config.encoder_settings.streams[i].height;
518 if (stream_width > width)
519 width = stream_width;
520 if (stream_height > height)
521 height = stream_height;
522 }
523
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000524 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
525 test::FrameGeneratorCapturer::Create(send_stream->Input(),
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000526 width,
527 height,
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000528 30,
529 Clock::GetRealTimeClock()));
530
531 send_stream->StartSending();
532 frame_generator_capturer->Start();
533
534 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
535
henrik.lundin@webrtc.org96616cb2014-03-13 15:39:27 +0000536 stream_observer.StopSending();
537 receiver_transport.StopSending();
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000538 frame_generator_capturer->Stop();
539 send_stream->StopSending();
540
541 call->DestroyVideoSendStream(send_stream);
542 }
543
andresp@webrtc.orgc6f66962014-03-17 15:34:57 +0000544 private:
545 std::vector<uint32_t> GenerateSsrcs(size_t num_streams,
546 uint32_t ssrc_offset) {
547 std::vector<uint32_t> ssrcs;
548 for (size_t i = 0; i != num_streams; ++i)
549 ssrcs.push_back(static_cast<uint32_t>(ssrc_offset + i));
550 return ssrcs;
551 }
552
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000553 std::map<uint32_t, bool> reserved_ssrcs_;
554};
555
andresp@webrtc.org700d14b2014-03-20 03:23:55 +0000556TEST_F(RampUpTest, SingleStreamWithoutPacing) {
557 RunRampUpTest(false, false, 1);
558}
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000559
andresp@webrtc.org700d14b2014-03-20 03:23:55 +0000560TEST_F(RampUpTest, SingleStreamWithPacing) {
561 RunRampUpTest(true, false, 1);
562}
563
564TEST_F(RampUpTest, SimulcastWithoutPacing) {
565 RunRampUpTest(false, false, 3);
566}
567
568TEST_F(RampUpTest, SimulcastWithPacing) {
569 RunRampUpTest(true, false, 3);
570}
stefan@webrtc.org47f0c412013-12-04 10:24:26 +0000571
pbos@webrtc.orge2a7a772014-03-19 08:43:57 +0000572// TODO(pbos): Re-enable, webrtc:2992.
andresp@webrtc.org700d14b2014-03-20 03:23:55 +0000573TEST_F(RampUpTest, DISABLED_SimulcastWithPacingAndRtx) {
574 RunRampUpTest(true, true, 3);
575}
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000576
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000577TEST_F(RampUpTest, UpDownUpOneStream) { RunRampUpDownUpTest(1, false); }
578
henrik.lundin@webrtc.org691c5b22014-03-13 09:21:26 +0000579TEST_F(RampUpTest, UpDownUpThreeStreams) { RunRampUpDownUpTest(3, false); }
henrik.lundin@webrtc.org0435a832014-03-06 09:12:00 +0000580
henrik.lundin@webrtc.org691c5b22014-03-13 09:21:26 +0000581TEST_F(RampUpTest, UpDownUpOneStreamRtx) { RunRampUpDownUpTest(1, true); }
henrik.lundin@webrtc.org0435a832014-03-06 09:12:00 +0000582
henrik.lundin@webrtc.org691c5b22014-03-13 09:21:26 +0000583TEST_F(RampUpTest, UpDownUpThreeStreamsRtx) { RunRampUpDownUpTest(3, true); }
henrik.lundin@webrtc.orgc7660982014-03-06 07:19:28 +0000584
pbos@webrtc.orgfa996f22013-09-10 09:26:25 +0000585} // namespace webrtc