OLD | NEW |
| (Empty) |
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 | |
11 #include <list> | |
12 #include <memory> | |
13 #include <utility> | |
14 #include <vector> | |
15 | |
16 #include "webrtc/base/basictypes.h" | |
17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
18 #include "webrtc/modules/rtp_rtcp/source/fec_test_helper.h" | |
19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h" | |
20 #include "webrtc/modules/rtp_rtcp/source/producer_fec.h" | |
21 #include "webrtc/test/gtest.h" | |
22 | |
23 namespace webrtc { | |
24 | |
25 namespace { | |
26 using test::fec::AugmentedPacket; | |
27 using test::fec::AugmentedPacketGenerator; | |
28 | |
29 constexpr int kFecPayloadType = 96; | |
30 constexpr int kRedPayloadType = 97; | |
31 constexpr uint32_t kMediaSsrc = 835424; | |
32 } // namespace | |
33 | |
34 void VerifyHeader(uint16_t seq_num, | |
35 uint32_t timestamp, | |
36 int red_payload_type, | |
37 int fec_payload_type, | |
38 RedPacket* packet, | |
39 bool marker_bit) { | |
40 EXPECT_GT(packet->length(), kRtpHeaderSize); | |
41 EXPECT_TRUE(packet->data() != NULL); | |
42 uint8_t* data = packet->data(); | |
43 // Marker bit not set. | |
44 EXPECT_EQ(marker_bit ? 0x80 : 0, data[1] & 0x80); | |
45 EXPECT_EQ(red_payload_type, data[1] & 0x7F); | |
46 EXPECT_EQ(seq_num, (data[2] << 8) + data[3]); | |
47 uint32_t parsed_timestamp = | |
48 (data[4] << 24) + (data[5] << 16) + (data[6] << 8) + data[7]; | |
49 EXPECT_EQ(timestamp, parsed_timestamp); | |
50 EXPECT_EQ(static_cast<uint8_t>(fec_payload_type), data[kRtpHeaderSize]); | |
51 } | |
52 | |
53 class ProducerFecTest : public ::testing::Test { | |
54 protected: | |
55 ProducerFecTest() : packet_generator_(kMediaSsrc) {} | |
56 | |
57 ProducerFec producer_; | |
58 AugmentedPacketGenerator packet_generator_; | |
59 }; | |
60 | |
61 // Verifies bug found via fuzzing, where a gap in the packet sequence caused us | |
62 // to move past the end of the current FEC packet mask byte without moving to | |
63 // the next byte. That likely caused us to repeatedly read from the same byte, | |
64 // and if that byte didn't protect packets we would generate empty FEC. | |
65 TEST_F(ProducerFecTest, NoEmptyFecWithSeqNumGaps) { | |
66 struct Packet { | |
67 size_t header_size; | |
68 size_t payload_size; | |
69 uint16_t seq_num; | |
70 bool marker_bit; | |
71 }; | |
72 std::vector<Packet> protected_packets; | |
73 protected_packets.push_back({15, 3, 41, 0}); | |
74 protected_packets.push_back({14, 1, 43, 0}); | |
75 protected_packets.push_back({19, 0, 48, 0}); | |
76 protected_packets.push_back({19, 0, 50, 0}); | |
77 protected_packets.push_back({14, 3, 51, 0}); | |
78 protected_packets.push_back({13, 8, 52, 0}); | |
79 protected_packets.push_back({19, 2, 53, 0}); | |
80 protected_packets.push_back({12, 3, 54, 0}); | |
81 protected_packets.push_back({21, 0, 55, 0}); | |
82 protected_packets.push_back({13, 3, 57, 1}); | |
83 FecProtectionParams params = {117, 3, kFecMaskBursty}; | |
84 producer_.SetFecParameters(¶ms); | |
85 uint8_t packet[28] = {0}; | |
86 for (Packet p : protected_packets) { | |
87 if (p.marker_bit) { | |
88 packet[1] |= 0x80; | |
89 } else { | |
90 packet[1] &= ~0x80; | |
91 } | |
92 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], p.seq_num); | |
93 producer_.AddRtpPacketAndGenerateFec(packet, p.payload_size, p.header_size); | |
94 size_t num_fec_packets = producer_.NumAvailableFecPackets(); | |
95 if (num_fec_packets > 0) { | |
96 std::vector<std::unique_ptr<RedPacket>> fec_packets = | |
97 producer_.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType, 100, | |
98 p.header_size); | |
99 EXPECT_EQ(num_fec_packets, fec_packets.size()); | |
100 } | |
101 } | |
102 } | |
103 | |
104 TEST_F(ProducerFecTest, OneFrameFec) { | |
105 // The number of media packets (|kNumPackets|), number of frames (one for | |
106 // this test), and the protection factor (|params->fec_rate|) are set to make | |
107 // sure the conditions for generating FEC are satisfied. This means: | |
108 // (1) protection factor is high enough so that actual overhead over 1 frame | |
109 // of packets is within |kMaxExcessOverhead|, and (2) the total number of | |
110 // media packets for 1 frame is at least |minimum_media_packets_fec_|. | |
111 constexpr size_t kNumPackets = 4; | |
112 FecProtectionParams params = {15, 3, kFecMaskRandom}; | |
113 packet_generator_.NewFrame(kNumPackets); | |
114 producer_.SetFecParameters(¶ms); // Expecting one FEC packet. | |
115 uint32_t last_timestamp = 0; | |
116 for (size_t i = 0; i < kNumPackets; ++i) { | |
117 std::unique_ptr<AugmentedPacket> packet = | |
118 packet_generator_.NextPacket(i, 10); | |
119 EXPECT_EQ(0, producer_.AddRtpPacketAndGenerateFec( | |
120 packet->data, packet->length, kRtpHeaderSize)); | |
121 last_timestamp = packet->header.header.timestamp; | |
122 } | |
123 EXPECT_TRUE(producer_.FecAvailable()); | |
124 uint16_t seq_num = packet_generator_.NextPacketSeqNum(); | |
125 std::vector<std::unique_ptr<RedPacket>> red_packets = | |
126 producer_.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType, seq_num, | |
127 kRtpHeaderSize); | |
128 EXPECT_FALSE(producer_.FecAvailable()); | |
129 ASSERT_EQ(1u, red_packets.size()); | |
130 VerifyHeader(seq_num, last_timestamp, kRedPayloadType, kFecPayloadType, | |
131 red_packets.front().get(), false); | |
132 } | |
133 | |
134 TEST_F(ProducerFecTest, TwoFrameFec) { | |
135 // The number of media packets/frame (|kNumPackets|), the number of frames | |
136 // (|kNumFrames|), and the protection factor (|params->fec_rate|) are set to | |
137 // make sure the conditions for generating FEC are satisfied. This means: | |
138 // (1) protection factor is high enough so that actual overhead over | |
139 // |kNumFrames| is within |kMaxExcessOverhead|, and (2) the total number of | |
140 // media packets for |kNumFrames| frames is at least | |
141 // |minimum_media_packets_fec_|. | |
142 constexpr size_t kNumPackets = 2; | |
143 constexpr size_t kNumFrames = 2; | |
144 | |
145 FecProtectionParams params = {15, 3, kFecMaskRandom}; | |
146 producer_.SetFecParameters(¶ms); // Expecting one FEC packet. | |
147 uint32_t last_timestamp = 0; | |
148 for (size_t i = 0; i < kNumFrames; ++i) { | |
149 packet_generator_.NewFrame(kNumPackets); | |
150 for (size_t j = 0; j < kNumPackets; ++j) { | |
151 std::unique_ptr<AugmentedPacket> packet = | |
152 packet_generator_.NextPacket(i * kNumPackets + j, 10); | |
153 EXPECT_EQ(0, producer_.AddRtpPacketAndGenerateFec( | |
154 packet->data, packet->length, kRtpHeaderSize)); | |
155 last_timestamp = packet->header.header.timestamp; | |
156 } | |
157 } | |
158 EXPECT_TRUE(producer_.FecAvailable()); | |
159 uint16_t seq_num = packet_generator_.NextPacketSeqNum(); | |
160 std::vector<std::unique_ptr<RedPacket>> red_packets = | |
161 producer_.GetUlpfecPacketsAsRed(kRedPayloadType, kFecPayloadType, seq_num, | |
162 kRtpHeaderSize); | |
163 EXPECT_FALSE(producer_.FecAvailable()); | |
164 ASSERT_EQ(1u, red_packets.size()); | |
165 VerifyHeader(seq_num, last_timestamp, kRedPayloadType, kFecPayloadType, | |
166 red_packets.front().get(), false); | |
167 } | |
168 | |
169 TEST_F(ProducerFecTest, BuildRedPacket) { | |
170 packet_generator_.NewFrame(1); | |
171 std::unique_ptr<AugmentedPacket> packet = packet_generator_.NextPacket(0, 10); | |
172 std::unique_ptr<RedPacket> red_packet = | |
173 ProducerFec::BuildRedPacket(packet->data, packet->length - kRtpHeaderSize, | |
174 kRtpHeaderSize, kRedPayloadType); | |
175 EXPECT_EQ(packet->length + 1, red_packet->length()); | |
176 VerifyHeader(packet->header.header.sequenceNumber, | |
177 packet->header.header.timestamp, kRedPayloadType, | |
178 packet->header.header.payloadType, red_packet.get(), | |
179 true); // Marker bit set. | |
180 for (int i = 0; i < 10; ++i) { | |
181 EXPECT_EQ(i, red_packet->data()[kRtpHeaderSize + 1 + i]); | |
182 } | |
183 } | |
184 | |
185 TEST_F(ProducerFecTest, BuildRedPacketWithEmptyPayload) { | |
186 constexpr size_t kNumFrames = 1; | |
187 constexpr size_t kPayloadLength = 0; | |
188 constexpr size_t kRedForFecHeaderLength = 1; | |
189 | |
190 packet_generator_.NewFrame(kNumFrames); | |
191 std::unique_ptr<AugmentedPacket> packet( | |
192 packet_generator_.NextPacket(0, kPayloadLength)); | |
193 std::unique_ptr<RedPacket> red_packet = | |
194 ProducerFec::BuildRedPacket(packet->data, packet->length - kRtpHeaderSize, | |
195 kRtpHeaderSize, kRedPayloadType); | |
196 EXPECT_EQ(packet->length + kRedForFecHeaderLength, red_packet->length()); | |
197 VerifyHeader(packet->header.header.sequenceNumber, | |
198 packet->header.header.timestamp, kRedPayloadType, | |
199 packet->header.header.payloadType, red_packet.get(), | |
200 true); // Marker bit set. | |
201 } | |
202 | |
203 } // namespace webrtc | |
OLD | NEW |