andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 13 | #include <algorithm> |
kwiberg | 6e27acd | 2016-04-27 08:19:58 | [diff] [blame] | 14 | #include <memory> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 15 | #include <vector> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 16 | |
kwiberg | 15b966d | 2016-09-29 00:42:01 | [diff] [blame^] | 17 | #include "webrtc/test/gtest.h" |
Erik Språng | 0077997 | 2016-07-29 10:59:36 | [diff] [blame] | 18 | #include "webrtc/base/rate_limiter.h" |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 19 | #include "webrtc/common_types.h" |
Henrik Kjellander | 36a14b5 | 2015-11-04 07:31:52 | [diff] [blame] | 20 | #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h" |
| 21 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 22 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
sprang@webrtc.org | 1557a0b | 2015-03-17 16:42:49 | [diff] [blame] | 23 | #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
pbos@webrtc.org | d3756f7 | 2013-07-10 15:40:42 | [diff] [blame] | 24 | #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_video.h" |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 25 | #include "webrtc/modules/rtp_rtcp/test/testAPI/test_api.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 26 | |
pkasting@chromium.org | be487b4 | 2015-02-23 21:28:22 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | const unsigned char kPayloadType = 100; |
| 30 | |
| 31 | }; |
| 32 | |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 33 | namespace webrtc { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 34 | |
| 35 | class RtpRtcpVideoTest : public ::testing::Test { |
| 36 | protected: |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 37 | RtpRtcpVideoTest() |
Peter Boström | 00a7fa4 | 2015-09-17 21:03:57 | [diff] [blame] | 38 | : rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)), |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 39 | test_ssrc_(3456), |
| 40 | test_timestamp_(4567), |
stefan@webrtc.org | 1bb2146 | 2013-01-21 07:42:11 | [diff] [blame] | 41 | test_sequence_number_(2345), |
Erik Språng | 0077997 | 2016-07-29 10:59:36 | [diff] [blame] | 42 | fake_clock(123456), |
| 43 | retransmission_rate_limiter_(&fake_clock, 1000) {} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 44 | ~RtpRtcpVideoTest() {} |
| 45 | |
| 46 | virtual void SetUp() { |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 47 | transport_ = new LoopBackTransport(); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 48 | receiver_ = new TestRtpReceiver(); |
| 49 | receive_statistics_.reset(ReceiveStatistics::Create(&fake_clock)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 50 | RtpRtcp::Configuration configuration; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 51 | configuration.audio = false; |
| 52 | configuration.clock = &fake_clock; |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 53 | configuration.outgoing_transport = transport_; |
Erik Språng | 0077997 | 2016-07-29 10:59:36 | [diff] [blame] | 54 | configuration.retransmission_rate_limiter = &retransmission_rate_limiter_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 55 | |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 56 | video_module_ = RtpRtcp::CreateRtpRtcp(configuration); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 57 | rtp_receiver_.reset(RtpReceiver::CreateVideoReceiver( |
Peter Boström | 00a7fa4 | 2015-09-17 21:03:57 | [diff] [blame] | 58 | &fake_clock, receiver_, NULL, &rtp_payload_registry_)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 59 | |
pbos | ba01e95 | 2015-10-02 09:36:56 | [diff] [blame] | 60 | video_module_->SetRTCPStatus(RtcpMode::kCompound); |
stefan@webrtc.org | 903e746 | 2014-06-05 08:25:29 | [diff] [blame] | 61 | video_module_->SetSSRC(test_ssrc_); |
pbos@webrtc.org | f377da9 | 2014-12-19 13:49:55 | [diff] [blame] | 62 | video_module_->SetStorePacketsStatus(true, 600); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 63 | EXPECT_EQ(0, video_module_->SetSendingStatus(true)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 64 | |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 65 | transport_->SetSendModule(video_module_, &rtp_payload_registry_, |
| 66 | rtp_receiver_.get(), receive_statistics_.get()); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 67 | |
| 68 | VideoCodec video_codec; |
| 69 | memset(&video_codec, 0, sizeof(video_codec)); |
| 70 | video_codec.plType = 123; |
| 71 | memcpy(video_codec.plName, "I420", 5); |
| 72 | |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 73 | EXPECT_EQ(0, video_module_->RegisterSendPayload(video_codec)); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 74 | EXPECT_EQ(0, rtp_receiver_->RegisterReceivePayload(video_codec.plName, |
| 75 | video_codec.plType, |
| 76 | 90000, |
| 77 | 0, |
| 78 | video_codec.maxBitrate)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 79 | |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 80 | payload_data_length_ = sizeof(video_frame_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 81 | |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 82 | for (size_t n = 0; n < payload_data_length_; n++) { |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 83 | video_frame_[n] = n%10; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 87 | size_t BuildRTPheader(uint8_t* dataBuffer, |
| 88 | uint32_t timestamp, |
| 89 | uint32_t sequence_number) { |
pbos@webrtc.org | b57da65 | 2013-04-08 11:08:41 | [diff] [blame] | 90 | dataBuffer[0] = static_cast<uint8_t>(0x80); // version 2 |
| 91 | dataBuffer[1] = static_cast<uint8_t>(kPayloadType); |
sprang@webrtc.org | 1557a0b | 2015-03-17 16:42:49 | [diff] [blame] | 92 | ByteWriter<uint16_t>::WriteBigEndian(dataBuffer + 2, sequence_number); |
| 93 | ByteWriter<uint32_t>::WriteBigEndian(dataBuffer + 4, timestamp); |
| 94 | ByteWriter<uint32_t>::WriteBigEndian(dataBuffer + 8, 0x1234); // SSRC. |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 95 | size_t rtpHeaderLength = 12; |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 96 | return rtpHeaderLength; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 97 | } |
| 98 | |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 99 | size_t PaddingPacket(uint8_t* buffer, |
| 100 | uint32_t timestamp, |
| 101 | uint32_t sequence_number, |
| 102 | size_t bytes) { |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 103 | // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP. |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 104 | size_t max_length = 224; |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 105 | |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 106 | size_t padding_bytes_in_packet = max_length; |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 107 | if (bytes < max_length) { |
| 108 | padding_bytes_in_packet = (bytes + 16) & 0xffe0; // Keep our modulus 32. |
| 109 | } |
| 110 | // Correct seq num, timestamp and payload type. |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 111 | size_t header_length = BuildRTPheader(buffer, timestamp, sequence_number); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 112 | buffer[0] |= 0x20; // Set padding bit. |
pbos@webrtc.org | b57da65 | 2013-04-08 11:08:41 | [diff] [blame] | 113 | int32_t* data = |
| 114 | reinterpret_cast<int32_t*>(&(buffer[header_length])); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 115 | |
| 116 | // Fill data buffer with random data. |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 117 | for (size_t j = 0; j < (padding_bytes_in_packet >> 2); j++) { |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 118 | data[j] = rand(); // NOLINT |
| 119 | } |
| 120 | // Set number of padding bytes in the last byte of the packet. |
| 121 | buffer[header_length + padding_bytes_in_packet - 1] = |
| 122 | padding_bytes_in_packet; |
| 123 | return padding_bytes_in_packet + header_length; |
| 124 | } |
| 125 | |
| 126 | virtual void TearDown() { |
| 127 | delete video_module_; |
| 128 | delete transport_; |
| 129 | delete receiver_; |
| 130 | } |
| 131 | |
| 132 | int test_id_; |
kwiberg | 6e27acd | 2016-04-27 08:19:58 | [diff] [blame] | 133 | std::unique_ptr<ReceiveStatistics> receive_statistics_; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 134 | RTPPayloadRegistry rtp_payload_registry_; |
kwiberg | 6e27acd | 2016-04-27 08:19:58 | [diff] [blame] | 135 | std::unique_ptr<RtpReceiver> rtp_receiver_; |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 136 | RtpRtcp* video_module_; |
| 137 | LoopBackTransport* transport_; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 138 | TestRtpReceiver* receiver_; |
pbos@webrtc.org | b57da65 | 2013-04-08 11:08:41 | [diff] [blame] | 139 | uint32_t test_ssrc_; |
| 140 | uint32_t test_timestamp_; |
| 141 | uint16_t test_sequence_number_; |
| 142 | uint8_t video_frame_[65000]; |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 143 | size_t payload_data_length_; |
stefan@webrtc.org | 1bb2146 | 2013-01-21 07:42:11 | [diff] [blame] | 144 | SimulatedClock fake_clock; |
Erik Språng | 0077997 | 2016-07-29 10:59:36 | [diff] [blame] | 145 | RateLimiter retransmission_rate_limiter_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | TEST_F(RtpRtcpVideoTest, BasicVideo) { |
pbos@webrtc.org | b57da65 | 2013-04-08 11:08:41 | [diff] [blame] | 149 | uint32_t timestamp = 3000; |
Sergey Ulanov | 83a148b | 2016-08-03 00:46:41 | [diff] [blame] | 150 | EXPECT_TRUE(video_module_->SendOutgoingData( |
| 151 | kVideoFrameDelta, 123, timestamp, timestamp / 90, video_frame_, |
| 152 | payload_data_length_, nullptr, nullptr, nullptr)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 153 | } |
| 154 | |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 155 | TEST_F(RtpRtcpVideoTest, PaddingOnlyFrames) { |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 156 | const size_t kPadSize = 255; |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 157 | uint8_t padding_packet[kPadSize]; |
| 158 | uint32_t seq_num = 0; |
| 159 | uint32_t timestamp = 3000; |
| 160 | VideoCodec codec; |
| 161 | codec.codecType = kVideoCodecVP8; |
| 162 | codec.plType = kPayloadType; |
| 163 | strncpy(codec.plName, "VP8", 4); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 164 | EXPECT_EQ(0, rtp_receiver_->RegisterReceivePayload(codec.plName, |
| 165 | codec.plType, |
| 166 | 90000, |
| 167 | 0, |
| 168 | codec.maxBitrate)); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 169 | for (int frame_idx = 0; frame_idx < 10; ++frame_idx) { |
| 170 | for (int packet_idx = 0; packet_idx < 5; ++packet_idx) { |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 171 | size_t packet_size = PaddingPacket(padding_packet, timestamp, seq_num, |
| 172 | kPadSize); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 173 | ++seq_num; |
stefan@webrtc.org | 6696fba | 2013-05-29 12:12:51 | [diff] [blame] | 174 | RTPHeader header; |
kwiberg | 6e27acd | 2016-04-27 08:19:58 | [diff] [blame] | 175 | std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create()); |
stefan@webrtc.org | 6696fba | 2013-05-29 12:12:51 | [diff] [blame] | 176 | EXPECT_TRUE(parser->Parse(padding_packet, packet_size, &header)); |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 177 | PayloadUnion payload_specific; |
| 178 | EXPECT_TRUE(rtp_payload_registry_.GetPayloadSpecifics(header.payloadType, |
pkasting@chromium.org | be487b4 | 2015-02-23 21:28:22 | [diff] [blame] | 179 | &payload_specific)); |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 180 | const uint8_t* payload = padding_packet + header.headerLength; |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 181 | const size_t payload_length = packet_size - header.headerLength; |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 182 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, payload, |
| 183 | payload_length, |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 | [diff] [blame] | 184 | payload_specific, true)); |
pkasting@chromium.org | 0ab923a | 2014-11-20 22:28:14 | [diff] [blame] | 185 | EXPECT_EQ(0u, receiver_->payload_size()); |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 | [diff] [blame] | 186 | EXPECT_EQ(payload_length, receiver_->rtp_header().header.paddingLength); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 187 | } |
| 188 | timestamp += 3000; |
stefan@webrtc.org | 1bb2146 | 2013-01-21 07:42:11 | [diff] [blame] | 189 | fake_clock.AdvanceTimeMilliseconds(33); |
stefan@webrtc.org | cf4441c | 2012-12-03 14:01:46 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | } // namespace webrtc |