| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <list> | 11 #include <list> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webrtc/base/basictypes.h" |
| 16 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 17 #include "webrtc/modules/rtp_rtcp/source/fec_test_helper.h" | 18 #include "webrtc/modules/rtp_rtcp/source/fec_test_helper.h" |
| 18 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h" | 19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h" |
| 19 #include "webrtc/modules/rtp_rtcp/source/producer_fec.h" | 20 #include "webrtc/modules/rtp_rtcp/source/producer_fec.h" |
| 20 | 21 |
| 21 namespace webrtc { | 22 namespace webrtc { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 constexpr uint8_t kFecPayloadType = 96; | 25 using test::fec::RawRtpPacket; |
| 25 constexpr uint8_t kRedPayloadType = 97; | 26 using test::fec::UlpfecPacketGenerator; |
| 27 |
| 28 constexpr int kFecPayloadType = 96; |
| 29 constexpr int kRedPayloadType = 97; |
| 26 } // namespace | 30 } // namespace |
| 27 | 31 |
| 28 using ::webrtc::test::fec::RawRtpPacket; | |
| 29 using ::webrtc::test::fec::UlpfecPacketGenerator; | |
| 30 | |
| 31 void VerifyHeader(uint16_t seq_num, | 32 void VerifyHeader(uint16_t seq_num, |
| 32 uint32_t timestamp, | 33 uint32_t timestamp, |
| 33 int red_payload_type, | 34 int red_payload_type, |
| 34 int fec_payload_type, | 35 int fec_payload_type, |
| 35 RedPacket* packet, | 36 RedPacket* packet, |
| 36 bool marker_bit) { | 37 bool marker_bit) { |
| 37 EXPECT_GT(packet->length(), kRtpHeaderSize); | 38 EXPECT_GT(packet->length(), kRtpHeaderSize); |
| 38 EXPECT_TRUE(packet->data() != NULL); | 39 EXPECT_TRUE(packet->data() != NULL); |
| 39 uint8_t* data = packet->data(); | 40 uint8_t* data = packet->data(); |
| 40 // Marker bit not set. | 41 // Marker bit not set. |
| 41 EXPECT_EQ(marker_bit ? 0x80 : 0, data[1] & 0x80); | 42 EXPECT_EQ(marker_bit ? 0x80 : 0, data[1] & 0x80); |
| 42 EXPECT_EQ(red_payload_type, data[1] & 0x7F); | 43 EXPECT_EQ(red_payload_type, data[1] & 0x7F); |
| 43 EXPECT_EQ(seq_num, (data[2] << 8) + data[3]); | 44 EXPECT_EQ(seq_num, (data[2] << 8) + data[3]); |
| 44 uint32_t parsed_timestamp = (data[4] << 24) + (data[5] << 16) + | 45 uint32_t parsed_timestamp = |
| 45 (data[6] << 8) + data[7]; | 46 (data[4] << 24) + (data[5] << 16) + (data[6] << 8) + data[7]; |
| 46 EXPECT_EQ(timestamp, parsed_timestamp); | 47 EXPECT_EQ(timestamp, parsed_timestamp); |
| 47 EXPECT_EQ(static_cast<uint8_t>(fec_payload_type), data[kRtpHeaderSize]); | 48 EXPECT_EQ(static_cast<uint8_t>(fec_payload_type), data[kRtpHeaderSize]); |
| 48 } | 49 } |
| 49 | 50 |
| 50 class ProducerFecTest : public ::testing::Test { | 51 class ProducerFecTest : public ::testing::Test { |
| 51 protected: | 52 protected: |
| 52 ProducerFec producer_; | 53 ProducerFec producer_; |
| 53 UlpfecPacketGenerator generator_; | 54 UlpfecPacketGenerator generator_; |
| 54 }; | 55 }; |
| 55 | 56 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 79 producer_.SetFecParameters(¶ms, 0); | 80 producer_.SetFecParameters(¶ms, 0); |
| 80 uint8_t packet[28] = {0}; | 81 uint8_t packet[28] = {0}; |
| 81 for (Packet p : protected_packets) { | 82 for (Packet p : protected_packets) { |
| 82 if (p.marker_bit) { | 83 if (p.marker_bit) { |
| 83 packet[1] |= 0x80; | 84 packet[1] |= 0x80; |
| 84 } else { | 85 } else { |
| 85 packet[1] &= ~0x80; | 86 packet[1] &= ~0x80; |
| 86 } | 87 } |
| 87 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], p.seq_num); | 88 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], p.seq_num); |
| 88 producer_.AddRtpPacketAndGenerateFec(packet, p.payload_size, p.header_size); | 89 producer_.AddRtpPacketAndGenerateFec(packet, p.payload_size, p.header_size); |
| 89 uint16_t num_fec_packets = producer_.NumAvailableFecPackets(); | 90 size_t num_fec_packets = producer_.NumAvailableFecPackets(); |
| 90 if (num_fec_packets > 0) { | 91 if (num_fec_packets > 0) { |
| 91 std::vector<std::unique_ptr<RedPacket>> fec_packets = | 92 std::vector<std::unique_ptr<RedPacket>> fec_packets = |
| 92 producer_.GetFecPacketsAsRed(kRedPayloadType, kFecPayloadType, 100, | 93 producer_.GetFecPacketsAsRed(kRedPayloadType, kFecPayloadType, 100, |
| 93 p.header_size); | 94 p.header_size); |
| 94 EXPECT_EQ(num_fec_packets, fec_packets.size()); | 95 EXPECT_EQ(num_fec_packets, fec_packets.size()); |
| 95 } | 96 } |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 TEST_F(ProducerFecTest, OneFrameFec) { | 100 TEST_F(ProducerFecTest, OneFrameFec) { |
| 100 // The number of media packets (|kNumPackets|), number of frames (one for | 101 // The number of media packets (|kNumPackets|), number of frames (one for |
| 101 // this test), and the protection factor (|params->fec_rate|) are set to make | 102 // this test), and the protection factor (|params->fec_rate|) are set to make |
| 102 // sure the conditions for generating FEC are satisfied. This means: | 103 // sure the conditions for generating FEC are satisfied. This means: |
| 103 // (1) protection factor is high enough so that actual overhead over 1 frame | 104 // (1) protection factor is high enough so that actual overhead over 1 frame |
| 104 // of packets is within |kMaxExcessOverhead|, and (2) the total number of | 105 // of packets is within |kMaxExcessOverhead|, and (2) the total number of |
| 105 // media packets for 1 frame is at least |minimum_media_packets_fec_|. | 106 // media packets for 1 frame is at least |minimum_media_packets_fec_|. |
| 106 const int kNumPackets = 4; | 107 constexpr size_t kNumPackets = 4; |
| 107 FecProtectionParams params = {15, 3, kFecMaskRandom}; | 108 FecProtectionParams params = {15, 3, kFecMaskRandom}; |
| 108 std::list<RawRtpPacket*> rtp_packets; | 109 std::list<RawRtpPacket*> rtp_packets; |
| 109 generator_.NewFrame(kNumPackets); | 110 generator_.NewFrame(kNumPackets); |
| 110 producer_.SetFecParameters(¶ms, 0); // Expecting one FEC packet. | 111 producer_.SetFecParameters(¶ms, 0); // Expecting one FEC packet. |
| 111 uint32_t last_timestamp = 0; | 112 uint32_t last_timestamp = 0; |
| 112 for (int i = 0; i < kNumPackets; ++i) { | 113 for (size_t i = 0; i < kNumPackets; ++i) { |
| 113 RawRtpPacket* rtp_packet = generator_.NextPacket(i, 10); | 114 RawRtpPacket* rtp_packet = generator_.NextPacket(i, 10); |
| 114 rtp_packets.push_back(rtp_packet); | 115 rtp_packets.push_back(rtp_packet); |
| 115 EXPECT_EQ(0, producer_.AddRtpPacketAndGenerateFec( | 116 EXPECT_EQ(0, producer_.AddRtpPacketAndGenerateFec( |
| 116 rtp_packet->data, rtp_packet->length, kRtpHeaderSize)); | 117 rtp_packet->data, rtp_packet->length, kRtpHeaderSize)); |
| 117 last_timestamp = rtp_packet->header.header.timestamp; | 118 last_timestamp = rtp_packet->header.header.timestamp; |
| 118 } | 119 } |
| 119 EXPECT_TRUE(producer_.FecAvailable()); | 120 EXPECT_TRUE(producer_.FecAvailable()); |
| 120 uint16_t seq_num = generator_.NextSeqNum(); | 121 uint16_t seq_num = generator_.NextSeqNum(); |
| 121 std::vector<std::unique_ptr<RedPacket>> packets = | 122 std::vector<std::unique_ptr<RedPacket>> packets = |
| 122 producer_.GetFecPacketsAsRed(kRedPayloadType, kFecPayloadType, seq_num, | 123 producer_.GetFecPacketsAsRed(kRedPayloadType, kFecPayloadType, seq_num, |
| 123 kRtpHeaderSize); | 124 kRtpHeaderSize); |
| 124 EXPECT_FALSE(producer_.FecAvailable()); | 125 EXPECT_FALSE(producer_.FecAvailable()); |
| 125 ASSERT_EQ(1u, packets.size()); | 126 ASSERT_EQ(1u, packets.size()); |
| 126 VerifyHeader(seq_num, last_timestamp, kRedPayloadType, kFecPayloadType, | 127 VerifyHeader(seq_num, last_timestamp, kRedPayloadType, kFecPayloadType, |
| 127 packets.front().get(), false); | 128 packets.front().get(), false); |
| 128 while (!rtp_packets.empty()) { | 129 while (!rtp_packets.empty()) { |
| 129 delete rtp_packets.front(); | 130 delete rtp_packets.front(); |
| 130 rtp_packets.pop_front(); | 131 rtp_packets.pop_front(); |
| 131 } | 132 } |
| 132 } | 133 } |
| 133 | 134 |
| 134 TEST_F(ProducerFecTest, TwoFrameFec) { | 135 TEST_F(ProducerFecTest, TwoFrameFec) { |
| 135 // The number of media packets/frame (|kNumPackets|), the number of frames | 136 // The number of media packets/frame (|kNumPackets|), the number of frames |
| 136 // (|kNumFrames|), and the protection factor (|params->fec_rate|) are set to | 137 // (|kNumFrames|), and the protection factor (|params->fec_rate|) are set to |
| 137 // make sure the conditions for generating FEC are satisfied. This means: | 138 // make sure the conditions for generating FEC are satisfied. This means: |
| 138 // (1) protection factor is high enough so that actual overhead over | 139 // (1) protection factor is high enough so that actual overhead over |
| 139 // |kNumFrames| is within |kMaxExcessOverhead|, and (2) the total number of | 140 // |kNumFrames| is within |kMaxExcessOverhead|, and (2) the total number of |
| 140 // media packets for |kNumFrames| frames is at least | 141 // media packets for |kNumFrames| frames is at least |
| 141 // |minimum_media_packets_fec_|. | 142 // |minimum_media_packets_fec_|. |
| 142 const int kNumPackets = 2; | 143 constexpr size_t kNumPackets = 2; |
| 143 const int kNumFrames = 2; | 144 constexpr size_t kNumFrames = 2; |
| 144 | 145 |
| 145 FecProtectionParams params = {15, 3, kFecMaskRandom}; | 146 FecProtectionParams params = {15, 3, kFecMaskRandom}; |
| 146 std::list<RawRtpPacket*> rtp_packets; | 147 std::list<RawRtpPacket*> rtp_packets; |
| 147 producer_.SetFecParameters(¶ms, 0); // Expecting one FEC packet. | 148 producer_.SetFecParameters(¶ms, 0); // Expecting one FEC packet. |
| 148 uint32_t last_timestamp = 0; | 149 uint32_t last_timestamp = 0; |
| 149 for (int i = 0; i < kNumFrames; ++i) { | 150 for (size_t i = 0; i < kNumFrames; ++i) { |
| 150 generator_.NewFrame(kNumPackets); | 151 generator_.NewFrame(kNumPackets); |
| 151 for (int j = 0; j < kNumPackets; ++j) { | 152 for (size_t j = 0; j < kNumPackets; ++j) { |
| 152 RawRtpPacket* rtp_packet = generator_.NextPacket(i * kNumPackets + j, 10); | 153 RawRtpPacket* rtp_packet = generator_.NextPacket(i * kNumPackets + j, 10); |
| 153 rtp_packets.push_back(rtp_packet); | 154 rtp_packets.push_back(rtp_packet); |
| 154 EXPECT_EQ(0, producer_.AddRtpPacketAndGenerateFec( | 155 EXPECT_EQ(0, producer_.AddRtpPacketAndGenerateFec( |
| 155 rtp_packet->data, rtp_packet->length, kRtpHeaderSize)); | 156 rtp_packet->data, rtp_packet->length, kRtpHeaderSize)); |
| 156 last_timestamp = rtp_packet->header.header.timestamp; | 157 last_timestamp = rtp_packet->header.header.timestamp; |
| 157 } | 158 } |
| 158 } | 159 } |
| 159 EXPECT_TRUE(producer_.FecAvailable()); | 160 EXPECT_TRUE(producer_.FecAvailable()); |
| 160 uint16_t seq_num = generator_.NextSeqNum(); | 161 uint16_t seq_num = generator_.NextSeqNum(); |
| 161 std::vector<std::unique_ptr<RedPacket>> packets = | 162 std::vector<std::unique_ptr<RedPacket>> packets = |
| (...skipping 13 matching lines...) Expand all Loading... |
| 175 generator_.NewFrame(1); | 176 generator_.NewFrame(1); |
| 176 RawRtpPacket* packet = generator_.NextPacket(0, 10); | 177 RawRtpPacket* packet = generator_.NextPacket(0, 10); |
| 177 std::unique_ptr<RedPacket> red_packet = | 178 std::unique_ptr<RedPacket> red_packet = |
| 178 ProducerFec::BuildRedPacket(packet->data, packet->length - kRtpHeaderSize, | 179 ProducerFec::BuildRedPacket(packet->data, packet->length - kRtpHeaderSize, |
| 179 kRtpHeaderSize, kRedPayloadType); | 180 kRtpHeaderSize, kRedPayloadType); |
| 180 EXPECT_EQ(packet->length + 1, red_packet->length()); | 181 EXPECT_EQ(packet->length + 1, red_packet->length()); |
| 181 VerifyHeader(packet->header.header.sequenceNumber, | 182 VerifyHeader(packet->header.header.sequenceNumber, |
| 182 packet->header.header.timestamp, kRedPayloadType, | 183 packet->header.header.timestamp, kRedPayloadType, |
| 183 packet->header.header.payloadType, red_packet.get(), | 184 packet->header.header.payloadType, red_packet.get(), |
| 184 true); // Marker bit set. | 185 true); // Marker bit set. |
| 185 for (int i = 0; i < 10; ++i) | 186 for (int i = 0; i < 10; ++i) { |
| 186 EXPECT_EQ(i, red_packet->data()[kRtpHeaderSize + 1 + i]); | 187 EXPECT_EQ(i, red_packet->data()[kRtpHeaderSize + 1 + i]); |
| 188 } |
| 187 delete packet; | 189 delete packet; |
| 188 } | 190 } |
| 189 | 191 |
| 190 TEST_F(ProducerFecTest, BuildRedPacketWithEmptyPayload) { | 192 TEST_F(ProducerFecTest, BuildRedPacketWithEmptyPayload) { |
| 191 constexpr size_t kNumFrames = 1; | 193 constexpr size_t kNumFrames = 1; |
| 192 constexpr size_t kPayloadLength = 0; | 194 constexpr size_t kPayloadLength = 0; |
| 193 constexpr size_t kRedForFecHeaderLength = 1; | 195 constexpr size_t kRedForFecHeaderLength = 1; |
| 194 | 196 |
| 195 generator_.NewFrame(kNumFrames); | 197 generator_.NewFrame(kNumFrames); |
| 196 std::unique_ptr<RawRtpPacket> packet( | 198 std::unique_ptr<RawRtpPacket> packet( |
| 197 generator_.NextPacket(0, kPayloadLength)); | 199 generator_.NextPacket(0, kPayloadLength)); |
| 198 std::unique_ptr<RedPacket> red_packet = | 200 std::unique_ptr<RedPacket> red_packet = |
| 199 ProducerFec::BuildRedPacket(packet->data, packet->length - kRtpHeaderSize, | 201 ProducerFec::BuildRedPacket(packet->data, packet->length - kRtpHeaderSize, |
| 200 kRtpHeaderSize, kRedPayloadType); | 202 kRtpHeaderSize, kRedPayloadType); |
| 201 EXPECT_EQ(packet->length + kRedForFecHeaderLength, red_packet->length()); | 203 EXPECT_EQ(packet->length + kRedForFecHeaderLength, red_packet->length()); |
| 202 VerifyHeader(packet->header.header.sequenceNumber, | 204 VerifyHeader(packet->header.header.sequenceNumber, |
| 203 packet->header.header.timestamp, kRedPayloadType, | 205 packet->header.header.timestamp, kRedPayloadType, |
| 204 packet->header.header.payloadType, red_packet.get(), | 206 packet->header.header.payloadType, red_packet.get(), |
| 205 true); // Marker bit set. | 207 true); // Marker bit set. |
| 206 } | 208 } |
| 207 | 209 |
| 208 } // namespace webrtc | 210 } // namespace webrtc |
| OLD | NEW |