2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
16 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webrtc/call.h"
19 #include "webrtc/common.h"
20 #include "webrtc/experiments.h"
21 #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"
24 #include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
25 #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"
30 #include "webrtc/test/direct_transport.h"
31 #include "webrtc/test/encoder_settings.h"
32 #include "webrtc/test/fake_decoder.h"
33 #include "webrtc/test/fake_encoder.h"
34 #include "webrtc/test/frame_generator_capturer.h"
35 #include "webrtc/test/testsupport/perf_test.h"
36 #include "webrtc/video/transport_adapter.h"
41 static const int kAbsoluteSendTimeExtensionId = 7;
42 static const int kMaxPacketSize = 1500;
44 class StreamObserver : public newapi::Transport, public RemoteBitrateObserver {
46 typedef std::map<uint32_t, int> BytesSentMap;
47 typedef std::map<uint32_t, uint32_t> SsrcMap;
48 StreamObserver(const SsrcMap& rtx_media_ssrcs,
49 newapi::Transport* feedback_transport,
51 : critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
52 test_done_(EventWrapper::Create()),
53 rtp_parser_(RtpHeaderParser::Create()),
54 feedback_transport_(feedback_transport),
55 receive_stats_(ReceiveStatistics::Create(clock)),
57 new RTPPayloadRegistry(-1,
58 RTPPayloadStrategy::CreateStrategy(false))),
60 expected_bitrate_bps_(0),
61 rtx_media_ssrcs_(rtx_media_ssrcs),
65 total_packets_sent_(0),
66 padding_packets_sent_(0),
67 rtx_media_packets_sent_(0) {
68 // 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();
74 feedback_transport_.Enable();
75 config.outgoing_transport = &feedback_transport_;
76 rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config));
77 rtp_rtcp_->SetREMBStatus(true);
78 rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound);
79 rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
80 kAbsoluteSendTimeExtensionId);
81 AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory;
82 const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 30000;
83 remote_bitrate_estimator_.reset(
84 rbe_factory.Create(this, clock, kMimdControl,
85 kRemoteBitrateEstimatorMinBitrateBps));
88 void set_expected_bitrate_bps(unsigned int expected_bitrate_bps) {
89 expected_bitrate_bps_ = expected_bitrate_bps;
92 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
93 unsigned int bitrate) OVERRIDE {
94 CriticalSectionScoped lock(critical_section_.get());
95 assert(expected_bitrate_bps_ > 0);
96 if (bitrate >= expected_bitrate_bps_) {
97 // Just trigger if there was any rtx padding packet.
98 if (rtx_media_ssrcs_.empty() || rtx_media_sent_ > 0) {
102 rtp_rtcp_->SetREMBData(
103 bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]);
104 rtp_rtcp_->Process();
107 virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE {
108 CriticalSectionScoped lock(critical_section_.get());
110 EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header));
111 receive_stats_->IncomingPacket(header, length, false);
112 payload_registry_->SetIncomingPayloadType(header);
113 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();
118 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);
130 payload_registry_->RestoreOriginalPacket(&restored_packet_ptr,
133 rtx_media_ssrcs_[header.ssrc],
135 length = restored_length;
136 EXPECT_TRUE(rtp_parser_->Parse(
137 restored_packet, static_cast<int>(length), &header));
139 rtp_rtcp_->SetRemoteSSRC(header.ssrc);
144 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
148 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
151 void ReportResult(const std::string& measurement,
153 const std::string& units) {
154 webrtc::test::PrintResult(
156 ::testing::UnitTest::GetInstance()->current_test_info()->name(),
157 value, units, false);
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");
170 scoped_ptr<CriticalSectionWrapper> critical_section_;
171 scoped_ptr<EventWrapper> test_done_;
172 scoped_ptr<RtpHeaderParser> rtp_parser_;
173 scoped_ptr<RtpRtcp> rtp_rtcp_;
174 internal::TransportAdapter feedback_transport_;
175 scoped_ptr<ReceiveStatistics> receive_stats_;
176 scoped_ptr<RTPPayloadRegistry> payload_registry_;
177 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
179 unsigned int expected_bitrate_bps_;
180 SsrcMap rtx_media_ssrcs_;
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_;
189 class LowRateStreamObserver : public test::DirectTransport,
190 public RemoteBitrateObserver,
191 public PacketReceiver {
193 LowRateStreamObserver(newapi::Transport* feedback_transport,
195 size_t number_of_streams,
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)),
203 test_state_(kFirstRampup),
204 state_start_ms_(clock_->TimeInMilliseconds()),
205 interval_start_ms_(state_start_ms_),
208 total_overuse_bytes_(0),
209 number_of_streams_(number_of_streams),
212 suspended_in_stats_(false) {
213 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(
225 rbe_factory.Create(this, clock, kMimdControl,
226 kRemoteBitrateEstimatorMinBitrateBps));
227 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);
234 virtual void SetSendStream(const VideoSendStream* send_stream) {
235 send_stream_ = send_stream;
238 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;
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_ +=
259 last_remb_bps_ / 8 * (now_ms - interval_start_ms_) / 1000;
261 EvolveTestState(average_rate_bps);
262 interval_start_ms_ = now_ms;
265 return test::DirectTransport::SendRtp(data, length);
268 virtual bool DeliverPacket(const uint8_t* packet, size_t length) OVERRIDE {
269 CriticalSectionScoped lock(critical_section_.get());
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();
278 suspended_in_stats_ = send_stream_->GetStats().suspended;
282 virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
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("_");
291 sprintf(temp_str, "%i", static_cast<int>(number_of_streams_));
292 str += std::string(temp_str);
294 str += (number_of_streams_ > 1 ? "s" : "");
296 str += (rtx_used_ ? "" : "no");
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();
304 assert(send_stream_ != NULL);
305 CriticalSectionScoped lock(critical_section_.get());
306 switch (test_state_) {
308 EXPECT_FALSE(suspended_in_stats_);
309 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",
319 now - state_start_ms_,
322 state_start_ms_ = now;
323 interval_start_ms_ = now;
329 if (bitrate_bps < kExpectedLowBitrateBps && suspended_in_stats_) {
330 // 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",
339 now - state_start_ms_,
342 state_start_ms_ = now;
343 interval_start_ms_ = now;
348 case kSecondRampup: {
349 if (bitrate_bps > kExpectedHighBitrateBps && !suspended_in_stats_) {
350 webrtc::test::PrintResult("ramp_up_down_up",
353 now - state_start_ms_,
356 webrtc::test::PrintResult("ramp_up_down_up",
359 total_overuse_bytes_,
369 EventTypeWrapper Wait() { return test_done_->Wait(120 * 1000); }
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 };
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_;
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_;
392 size_t total_overuse_bytes_;
393 const size_t number_of_streams_;
394 const bool rtx_used_;
395 const VideoSendStream* send_stream_;
396 bool suspended_in_stats_ GUARDED_BY(critical_section_);
400 class RampUpTest : public ::testing::Test {
402 virtual void SetUp() { reserved_ssrcs_.clear(); }
405 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));
408 StreamObserver::SsrcMap rtx_ssrc_map;
410 for (size_t i = 0; i < ssrcs.size(); ++i)
411 rtx_ssrc_map[rtx_ssrcs[i]] = ssrcs[i];
413 test::DirectTransport receiver_transport;
414 StreamObserver stream_observer(rtx_ssrc_map,
416 Clock::GetRealTimeClock());
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();
425 receiver_transport.SetReceiver(call->Receiver());
427 test::FakeEncoder encoder(Clock::GetRealTimeClock());
428 send_config.encoder_settings =
429 test::CreateEncoderSettings(&encoder, "FAKE", 125, num_streams);
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;
436 send_config.pacing = pacing;
437 send_config.rtp.nack.rtp_history_ms = 1000;
438 send_config.rtp.ssrcs = ssrcs;
440 send_config.rtp.rtx.payload_type = 96;
441 send_config.rtp.rtx.ssrcs = rtx_ssrcs;
443 send_config.rtp.extensions.push_back(
444 RtpExtension(RtpExtension::kAbsSendTime, kAbsoluteSendTimeExtensionId));
446 if (num_streams == 1) {
447 // For single stream rampup until 1mbps
448 stream_observer.set_expected_bitrate_bps(1000000);
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
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;
457 expected_bitrate_bps +=
458 send_config.encoder_settings.streams[i].target_bitrate_bps;
460 stream_observer.set_expected_bitrate_bps(expected_bitrate_bps);
463 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
465 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
466 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()));
473 send_stream->StartSending();
474 frame_generator_capturer->Start();
476 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
478 frame_generator_capturer->Stop();
479 send_stream->StopSending();
481 call->DestroyVideoSendStream(send_stream);
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);
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();
499 receiver_transport.SetReceiver(call->Receiver());
501 test::FakeEncoder encoder(Clock::GetRealTimeClock());
502 send_config.encoder_settings =
503 test::CreateEncoderSettings(&encoder, "FAKE", 125, number_of_streams);
504 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;
511 VideoSendStream* send_stream = call->CreateVideoSendStream(send_config);
512 stream_observer.SetSendStream(send_stream);
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;
525 scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer(
526 test::FrameGeneratorCapturer::Create(send_stream->Input(),
530 Clock::GetRealTimeClock()));
532 send_stream->StartSending();
533 frame_generator_capturer->Start();
535 EXPECT_EQ(kEventSignaled, stream_observer.Wait());
537 stream_observer.StopSending();
538 receiver_transport.StopSending();
539 frame_generator_capturer->Stop();
540 send_stream->StopSending();
542 call->DestroyVideoSendStream(send_stream);
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));
554 std::map<uint32_t, bool> reserved_ssrcs_;
557 TEST_F(RampUpTest, SingleStreamWithoutPacing) {
558 RunRampUpTest(false, false, 1);
561 TEST_F(RampUpTest, SingleStreamWithPacing) {
562 RunRampUpTest(true, false, 1);
565 TEST_F(RampUpTest, SimulcastWithoutPacing) {
566 RunRampUpTest(false, false, 3);
569 TEST_F(RampUpTest, SimulcastWithPacing) {
570 RunRampUpTest(true, false, 3);
573 // TODO(pbos): Re-enable, webrtc:2992.
574 TEST_F(RampUpTest, DISABLED_SimulcastWithPacingAndRtx) {
575 RunRampUpTest(true, true, 3);
578 TEST_F(RampUpTest, UpDownUpOneStream) { RunRampUpDownUpTest(1, false); }
580 TEST_F(RampUpTest, UpDownUpThreeStreams) { RunRampUpDownUpTest(3, false); }
582 TEST_F(RampUpTest, UpDownUpOneStreamRtx) { RunRampUpDownUpTest(1, true); }
584 TEST_F(RampUpTest, UpDownUpThreeStreamsRtx) { RunRampUpDownUpTest(3, true); }
586 } // namespace webrtc