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