pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 1 | /* |
| 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> |
| 13 | |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
pbos@webrtc.org | 24e2089 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 16 | #include "webrtc/call.h" |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
| 18 | #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h" |
| 19 | #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" |
| 20 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" |
| 21 | #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
| 22 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 23 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
| 24 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
pbos@webrtc.org | 24e2089 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 25 | #include "webrtc/test/direct_transport.h" |
| 26 | #include "webrtc/test/fake_decoder.h" |
| 27 | #include "webrtc/test/fake_encoder.h" |
| 28 | #include "webrtc/test/frame_generator_capturer.h" |
| 29 | #include "webrtc/test/generate_ssrcs.h" |
pbos@webrtc.org | 3009c81 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 30 | #include "webrtc/video/transport_adapter.h" |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
pbos@webrtc.org | 990c5e3 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | static const int kTOffsetExtensionId = 7; |
| 36 | } |
| 37 | |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 38 | class StreamObserver : public newapi::Transport, public RemoteBitrateObserver { |
| 39 | public: |
| 40 | typedef std::map<uint32_t, int> BytesSentMap; |
| 41 | StreamObserver(int num_expected_ssrcs, |
| 42 | newapi::Transport* feedback_transport, |
| 43 | Clock* clock) |
| 44 | : critical_section_(CriticalSectionWrapper::CreateCriticalSection()), |
| 45 | all_ssrcs_sent_(EventWrapper::Create()), |
| 46 | rtp_parser_(RtpHeaderParser::Create()), |
pbos@webrtc.org | 3009c81 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 47 | feedback_transport_(feedback_transport), |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 48 | receive_stats_(ReceiveStatistics::Create(clock)), |
| 49 | clock_(clock), |
| 50 | num_expected_ssrcs_(num_expected_ssrcs) { |
| 51 | // Ideally we would only have to instantiate an RtcpSender, an |
| 52 | // RtpHeaderParser and a RemoteBitrateEstimator here, but due to the current |
| 53 | // state of the RTP module we need a full module and receive statistics to |
| 54 | // be able to produce an RTCP with REMB. |
| 55 | RtpRtcp::Configuration config; |
| 56 | config.receive_statistics = receive_stats_.get(); |
pbos@webrtc.org | 3009c81 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 57 | config.outgoing_transport = &feedback_transport_; |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 58 | rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(config)); |
| 59 | rtp_rtcp_->SetREMBStatus(true); |
| 60 | rtp_rtcp_->SetRTCPStatus(kRtcpNonCompound); |
| 61 | rtp_parser_->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset, |
pbos@webrtc.org | 990c5e3 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 62 | kTOffsetExtensionId); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 63 | AbsoluteSendTimeRemoteBitrateEstimatorFactory rbe_factory; |
| 64 | remote_bitrate_estimator_.reset(rbe_factory.Create(this, clock)); |
| 65 | } |
| 66 | |
| 67 | virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs, |
| 68 | unsigned int bitrate) { |
| 69 | CriticalSectionScoped lock(critical_section_.get()); |
| 70 | if (ssrcs.size() == num_expected_ssrcs_ && bitrate >= kExpectedBitrateBps) |
| 71 | all_ssrcs_sent_->Set(); |
| 72 | rtp_rtcp_->SetREMBData( |
| 73 | bitrate, static_cast<uint8_t>(ssrcs.size()), &ssrcs[0]); |
| 74 | rtp_rtcp_->Process(); |
| 75 | } |
| 76 | |
pbos@webrtc.org | 3009c81 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 77 | virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE { |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 78 | CriticalSectionScoped lock(critical_section_.get()); |
| 79 | RTPHeader header; |
| 80 | EXPECT_TRUE(rtp_parser_->Parse(packet, static_cast<int>(length), &header)); |
| 81 | receive_stats_->IncomingPacket(header, length, false); |
| 82 | rtp_rtcp_->SetRemoteSSRC(header.ssrc); |
| 83 | remote_bitrate_estimator_->IncomingPacket( |
| 84 | clock_->TimeInMilliseconds(), static_cast<int>(length - 12), header); |
| 85 | if (remote_bitrate_estimator_->TimeUntilNextProcess() <= 0) { |
| 86 | remote_bitrate_estimator_->Process(); |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
pbos@webrtc.org | 3009c81 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 91 | virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE { |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | EventTypeWrapper Wait() { return all_ssrcs_sent_->Wait(120 * 1000); } |
| 96 | |
| 97 | private: |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 98 | static const unsigned int kExpectedBitrateBps = 1200000; |
| 99 | |
| 100 | scoped_ptr<CriticalSectionWrapper> critical_section_; |
| 101 | scoped_ptr<EventWrapper> all_ssrcs_sent_; |
| 102 | scoped_ptr<RtpHeaderParser> rtp_parser_; |
| 103 | scoped_ptr<RtpRtcp> rtp_rtcp_; |
pbos@webrtc.org | 3009c81 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 104 | internal::TransportAdapter feedback_transport_; |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 105 | scoped_ptr<ReceiveStatistics> receive_stats_; |
| 106 | scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_; |
| 107 | Clock* clock_; |
| 108 | const size_t num_expected_ssrcs_; |
| 109 | }; |
| 110 | |
| 111 | class RampUpTest : public ::testing::TestWithParam<bool> { |
| 112 | public: |
| 113 | virtual void SetUp() { reserved_ssrcs_.clear(); } |
| 114 | |
| 115 | protected: |
| 116 | std::map<uint32_t, bool> reserved_ssrcs_; |
| 117 | }; |
| 118 | |
| 119 | TEST_P(RampUpTest, RampUpWithPadding) { |
| 120 | test::DirectTransport receiver_transport; |
| 121 | StreamObserver stream_observer( |
| 122 | 3, &receiver_transport, Clock::GetRealTimeClock()); |
| 123 | Call::Config call_config(&stream_observer); |
| 124 | scoped_ptr<Call> call(Call::Create(call_config)); |
| 125 | VideoSendStream::Config send_config = call->GetDefaultSendConfig(); |
| 126 | |
| 127 | receiver_transport.SetReceiver(call->Receiver()); |
| 128 | |
| 129 | test::FakeEncoder encoder(Clock::GetRealTimeClock()); |
| 130 | send_config.encoder = &encoder; |
| 131 | send_config.internal_source = false; |
| 132 | test::FakeEncoder::SetCodecSettings(&send_config.codec, 3); |
stefan@webrtc.org | 4985c7b | 2013-11-15 12:32:15 +0000 | [diff] [blame] | 133 | send_config.codec.plType = 125; |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 134 | send_config.pacing = GetParam(); |
pbos@webrtc.org | 990c5e3 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 135 | send_config.rtp.extensions.push_back( |
pbos@webrtc.org | 346dbe7 | 2013-11-20 11:48:56 +0000 | [diff] [blame] | 136 | RtpExtension(RtpExtension::kTOffset, kTOffsetExtensionId)); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 137 | |
| 138 | test::GenerateRandomSsrcs(&send_config, &reserved_ssrcs_); |
| 139 | |
pbos@webrtc.org | 964d78e | 2013-11-20 10:40:25 +0000 | [diff] [blame] | 140 | VideoSendStream* send_stream = call->CreateVideoSendStream(send_config); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 141 | |
| 142 | VideoReceiveStream::Config receive_config; |
| 143 | receive_config.rtp.ssrc = send_config.rtp.ssrcs[0]; |
| 144 | receive_config.rtp.nack.rtp_history_ms = send_config.rtp.nack.rtp_history_ms; |
| 145 | VideoReceiveStream* receive_stream = |
pbos@webrtc.org | 964d78e | 2013-11-20 10:40:25 +0000 | [diff] [blame] | 146 | call->CreateVideoReceiveStream(receive_config); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 147 | |
| 148 | scoped_ptr<test::FrameGeneratorCapturer> frame_generator_capturer( |
andresp@webrtc.org | 28631e7 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 149 | test::FrameGeneratorCapturer::Create(send_stream->Input(), |
| 150 | send_config.codec.width, |
| 151 | send_config.codec.height, |
| 152 | 30, |
| 153 | Clock::GetRealTimeClock())); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 154 | |
pbos@webrtc.org | 7f9f840 | 2013-11-20 11:36:47 +0000 | [diff] [blame] | 155 | receive_stream->StartReceiving(); |
| 156 | send_stream->StartSending(); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 157 | frame_generator_capturer->Start(); |
| 158 | |
| 159 | EXPECT_EQ(kEventSignaled, stream_observer.Wait()); |
| 160 | |
| 161 | frame_generator_capturer->Stop(); |
pbos@webrtc.org | 7f9f840 | 2013-11-20 11:36:47 +0000 | [diff] [blame] | 162 | send_stream->StopSending(); |
| 163 | receive_stream->StopReceiving(); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 164 | |
pbos@webrtc.org | 12a93e0 | 2013-11-21 13:49:43 +0000 | [diff] [blame^] | 165 | call->DestroyVideoReceiveStream(receive_stream); |
| 166 | call->DestroyVideoSendStream(send_stream); |
pbos@webrtc.org | fa996f2 | 2013-09-10 09:26:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | INSTANTIATE_TEST_CASE_P(RampUpTest, RampUpTest, ::testing::Bool()); |
| 170 | |
| 171 | } // namespace webrtc |