blob: ba18fc61d4ff1f4f16d2149f0456ff51d84de43d [file] [log] [blame]
marpan@webrtc.orgb783a552012-02-04 02:46:35 +00001/*
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
Rasmus Brandtae4f7672016-07-07 09:40:51 +020011#include <algorithm>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000012#include <list>
brandtrece4aba2016-09-20 23:16:28 -070013#include <memory>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/fec_test_helper.h"
17#include "modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
18#include "modules/rtp_rtcp/source/forward_error_correction.h"
19#include "modules/rtp_rtcp/source/ulpfec_header_reader_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/random.h"
21#include "test/gtest.h"
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000022
brandtrece4aba2016-09-20 23:16:28 -070023namespace webrtc {
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000024
brandtrece4aba2016-09-20 23:16:28 -070025namespace {
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000026
27// Transport header size in bytes. Assume UDP/IPv4 as a reasonable minimum.
brandtrece4aba2016-09-20 23:16:28 -070028constexpr size_t kTransportOverhead = 28;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000029
brandtrece4aba2016-09-20 23:16:28 -070030constexpr uint32_t kMediaSsrc = 83542;
brandtr0496de22016-10-03 00:43:25 -070031constexpr uint32_t kFlexfecSsrc = 43245;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000032
Rasmus Brandtd73ba122017-12-07 10:22:49 +010033constexpr size_t kMaxMediaPackets = 48;
34
brandtrece4aba2016-09-20 23:16:28 -070035// Deep copies |src| to |dst|, but only keeps every Nth packet.
36void DeepCopyEveryNthPacket(const ForwardErrorCorrection::PacketList& src,
37 int n,
38 ForwardErrorCorrection::PacketList* dst) {
39 RTC_DCHECK_GT(n, 0);
40 int i = 0;
41 for (const auto& packet : src) {
42 if (i % n == 0) {
43 dst->emplace_back(new ForwardErrorCorrection::Packet(*packet));
44 }
45 ++i;
46 }
47}
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +000048
brandtrece4aba2016-09-20 23:16:28 -070049} // namespace
50
51using ::testing::Types;
52
53template <typename ForwardErrorCorrectionType>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000054class RtpFecTest : public ::testing::Test {
55 protected:
56 RtpFecTest()
brandtrece4aba2016-09-20 23:16:28 -070057 : random_(0xabcdef123456),
58 media_packet_generator_(
59 kRtpHeaderSize, // Minimum packet size.
60 IP_PACKET_SIZE - kRtpHeaderSize - kTransportOverhead -
61 fec_.MaxPacketOverhead(), // Maximum packet size.
62 kMediaSsrc,
63 &random_) {}
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000064
brandtrece4aba2016-09-20 23:16:28 -070065 // Construct |received_packets_|: a subset of the media and FEC packets.
brandtrd90fa0b2016-08-09 06:57:14 -070066 //
67 // Media packet "i" is lost if media_loss_mask_[i] = 1, received if
68 // media_loss_mask_[i] = 0.
69 // FEC packet "i" is lost if fec_loss_mask_[i] = 1, received if
70 // fec_loss_mask_[i] = 0.
71 void NetworkReceivedPackets(int* media_loss_mask, int* fec_loss_mask);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000072
73 // Add packet from |packet_list| to list of received packets, using the
74 // |loss_mask|.
75 // The |packet_list| may be a media packet list (is_fec = false), or a
76 // FEC packet list (is_fec = true).
brandtr35c480c2016-08-09 01:23:23 -070077 template <typename T>
78 void ReceivedPackets(const T& packet_list, int* loss_mask, bool is_fec);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000079
80 // Check for complete recovery after FEC decoding.
81 bool IsRecoveryComplete();
82
brandtrece4aba2016-09-20 23:16:28 -070083 ForwardErrorCorrectionType fec_;
brandtrd90fa0b2016-08-09 06:57:14 -070084
brandtrece4aba2016-09-20 23:16:28 -070085 Random random_;
86 test::fec::MediaPacketGenerator media_packet_generator_;
brandtrd90fa0b2016-08-09 06:57:14 -070087
brandtrece4aba2016-09-20 23:16:28 -070088 ForwardErrorCorrection::PacketList media_packets_;
89 std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_;
nissea5f043f2017-09-18 07:58:59 -070090 std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
91 received_packets_;
brandtrece4aba2016-09-20 23:16:28 -070092 ForwardErrorCorrection::RecoveredPacketList recovered_packets_;
brandtrd90fa0b2016-08-09 06:57:14 -070093
Rasmus Brandt78db1582016-09-21 09:19:34 +020094 int media_loss_mask_[kUlpfecMaxMediaPackets];
95 int fec_loss_mask_[kUlpfecMaxMediaPackets];
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000096};
97
Rasmus Brandtea7beb92016-09-21 12:01:19 +020098template <typename ForwardErrorCorrectionType>
99void RtpFecTest<ForwardErrorCorrectionType>::NetworkReceivedPackets(
100 int* media_loss_mask,
101 int* fec_loss_mask) {
102 constexpr bool kFecPacket = true;
nissea5f043f2017-09-18 07:58:59 -0700103 this->received_packets_.clear();
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200104 ReceivedPackets(media_packets_, media_loss_mask, !kFecPacket);
105 ReceivedPackets(generated_fec_packets_, fec_loss_mask, kFecPacket);
106}
107
108template <typename ForwardErrorCorrectionType>
109template <typename PacketListType>
110void RtpFecTest<ForwardErrorCorrectionType>::ReceivedPackets(
111 const PacketListType& packet_list,
112 int* loss_mask,
113 bool is_fec) {
brandtrd726a3f2017-06-29 02:45:35 -0700114 uint16_t fec_seq_num = ForwardErrorCorrectionType::GetFirstFecSeqNum(
115 media_packet_generator_.GetNextSeqNum());
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200116 int packet_idx = 0;
117
118 for (const auto& packet : packet_list) {
119 if (loss_mask[packet_idx] == 0) {
120 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet(
121 new ForwardErrorCorrection::ReceivedPacket());
122 received_packet->pkt = new ForwardErrorCorrection::Packet();
123 received_packet->pkt->length = packet->length;
124 memcpy(received_packet->pkt->data, packet->data, packet->length);
125 received_packet->is_fec = is_fec;
126 if (!is_fec) {
brandtrd726a3f2017-06-29 02:45:35 -0700127 received_packet->ssrc = kMediaSsrc;
128 // For media packets, the sequence number is obtained from the
129 // RTP header as written by MediaPacketGenerator::ConstructMediaPackets.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200130 received_packet->seq_num =
131 ByteReader<uint16_t>::ReadBigEndian(&packet->data[2]);
132 } else {
brandtrd726a3f2017-06-29 02:45:35 -0700133 received_packet->ssrc = ForwardErrorCorrectionType::kFecSsrc;
134 // For FEC packets, we simulate the sequence numbers differently
135 // depending on if ULPFEC or FlexFEC is used. See the definition of
136 // ForwardErrorCorrectionType::GetFirstFecSeqNum.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200137 received_packet->seq_num = fec_seq_num;
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200138 }
139 received_packets_.push_back(std::move(received_packet));
140 }
141 packet_idx++;
142 // Sequence number of FEC packets are defined as increment by 1 from
143 // last media packet in frame.
144 if (is_fec)
145 fec_seq_num++;
146 }
147}
148
149template <typename ForwardErrorCorrectionType>
150bool RtpFecTest<ForwardErrorCorrectionType>::IsRecoveryComplete() {
151 // We must have equally many recovered packets as original packets.
152 if (recovered_packets_.size() != media_packets_.size()) {
153 return false;
154 }
155
156 // All recovered packets must be identical to the corresponding
157 // original packets.
brandtr0496de22016-10-03 00:43:25 -0700158 auto cmp = [](
159 const std::unique_ptr<ForwardErrorCorrection::Packet>& media_packet,
160 const std::unique_ptr<ForwardErrorCorrection::RecoveredPacket>&
161 recovered_packet) {
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200162 if (media_packet->length != recovered_packet->pkt->length) {
163 return false;
164 }
brandtr0496de22016-10-03 00:43:25 -0700165 if (memcmp(media_packet->data, recovered_packet->pkt->data,
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200166 media_packet->length) != 0) {
167 return false;
168 }
169 return true;
170 };
171 return std::equal(media_packets_.cbegin(), media_packets_.cend(),
172 recovered_packets_.cbegin(), cmp);
173}
174
brandtrece4aba2016-09-20 23:16:28 -0700175// Define gTest typed test to loop over both ULPFEC and FlexFEC.
176// Since the tests now are parameterized, we need to access
177// member variables using |this|, thereby enforcing runtime
178// resolution.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200179
brandtr0496de22016-10-03 00:43:25 -0700180class FlexfecForwardErrorCorrection : public ForwardErrorCorrection {
181 public:
brandtrd726a3f2017-06-29 02:45:35 -0700182 static const uint32_t kFecSsrc = kFlexfecSsrc;
183
brandtr0496de22016-10-03 00:43:25 -0700184 FlexfecForwardErrorCorrection()
185 : ForwardErrorCorrection(
186 std::unique_ptr<FecHeaderReader>(new FlexfecHeaderReader()),
brandtrd726a3f2017-06-29 02:45:35 -0700187 std::unique_ptr<FecHeaderWriter>(new FlexfecHeaderWriter()),
188 kFecSsrc,
189 kMediaSsrc) {}
190
191 // For FlexFEC we let the FEC packet sequence numbers be independent of
192 // the media packet sequence numbers.
193 static uint16_t GetFirstFecSeqNum(uint16_t next_media_seq_num) {
194 Random random(0xbe110);
195 return random.Rand<uint16_t>();
196 }
brandtr0496de22016-10-03 00:43:25 -0700197};
198
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200199class UlpfecForwardErrorCorrection : public ForwardErrorCorrection {
200 public:
brandtrd726a3f2017-06-29 02:45:35 -0700201 static const uint32_t kFecSsrc = kMediaSsrc;
202
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200203 UlpfecForwardErrorCorrection()
204 : ForwardErrorCorrection(
205 std::unique_ptr<FecHeaderReader>(new UlpfecHeaderReader()),
brandtrd726a3f2017-06-29 02:45:35 -0700206 std::unique_ptr<FecHeaderWriter>(new UlpfecHeaderWriter()),
207 kFecSsrc,
208 kMediaSsrc) {}
209
210 // For ULPFEC we assume that the FEC packets are subsequent to the media
211 // packets in terms of sequence number.
212 static uint16_t GetFirstFecSeqNum(uint16_t next_media_seq_num) {
213 return next_media_seq_num;
214 }
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200215};
216
brandtr0496de22016-10-03 00:43:25 -0700217using FecTypes =
218 Types<FlexfecForwardErrorCorrection, UlpfecForwardErrorCorrection>;
brandtrece4aba2016-09-20 23:16:28 -0700219TYPED_TEST_CASE(RtpFecTest, FecTypes);
220
Rasmus Brandtd73ba122017-12-07 10:22:49 +0100221TYPED_TEST(RtpFecTest, WillProtectMediaPacketsWithLargeSequenceNumberGap) {
222 constexpr int kNumImportantPackets = 0;
223 constexpr bool kUseUnequalProtection = false;
224 constexpr int kNumMediaPackets = 2;
225 constexpr uint8_t kProtectionFactor = 127;
226
227 this->media_packets_ =
228 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
229
230 // Create |kMaxMediaPackets - 1| sequence number difference.
231 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.front()->data[2],
232 1);
233 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.back()->data[2],
234 kMaxMediaPackets);
235
236 EXPECT_EQ(
237 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
238 kNumImportantPackets, kUseUnequalProtection,
239 kFecMaskBursty, &this->generated_fec_packets_));
240 EXPECT_EQ(1u, this->generated_fec_packets_.size());
241}
242
243TYPED_TEST(RtpFecTest,
244 WillNotProtectMediaPacketsWithTooLargeSequenceNumberGap) {
245 constexpr int kNumImportantPackets = 0;
246 constexpr bool kUseUnequalProtection = false;
247 constexpr int kNumMediaPackets = 2;
248 constexpr uint8_t kProtectionFactor = 127;
249
250 this->media_packets_ =
251 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
252
253 // Create |kMaxMediaPackets| sequence number difference.
254 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.front()->data[2],
255 1);
256 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.back()->data[2],
257 kMaxMediaPackets + 1);
258
259 EXPECT_EQ(
260 -1, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
261 kNumImportantPackets, kUseUnequalProtection,
262 kFecMaskBursty, &this->generated_fec_packets_));
263 EXPECT_TRUE(this->generated_fec_packets_.empty());
264}
265
brandtrece4aba2016-09-20 23:16:28 -0700266TYPED_TEST(RtpFecTest, FecRecoveryNoLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700267 constexpr int kNumImportantPackets = 0;
268 constexpr bool kUseUnequalProtection = false;
269 constexpr int kNumMediaPackets = 4;
270 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000271
brandtrece4aba2016-09-20 23:16:28 -0700272 this->media_packets_ =
273 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000274
brandtrece4aba2016-09-20 23:16:28 -0700275 EXPECT_EQ(
276 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
277 kNumImportantPackets, kUseUnequalProtection,
278 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000279
280 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700281 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000282
283 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700284 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
285 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
286 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000287
nissea5f043f2017-09-18 07:58:59 -0700288 for (const auto& received_packet : this->received_packets_) {
289 this->fec_.DecodeFec(*received_packet,
290 &this->recovered_packets_);
291 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000292
293 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700294 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000295}
296
brandtrece4aba2016-09-20 23:16:28 -0700297TYPED_TEST(RtpFecTest, FecRecoveryWithLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700298 constexpr int kNumImportantPackets = 0;
299 constexpr bool kUseUnequalProtection = false;
300 constexpr int kNumMediaPackets = 4;
301 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000302
brandtrece4aba2016-09-20 23:16:28 -0700303 this->media_packets_ =
304 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000305
brandtrece4aba2016-09-20 23:16:28 -0700306 EXPECT_EQ(
307 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
308 kNumImportantPackets, kUseUnequalProtection,
309 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000310
311 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700312 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000313
314 // 1 media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700315 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
316 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
317 this->media_loss_mask_[3] = 1;
318 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000319
nissea5f043f2017-09-18 07:58:59 -0700320 for (const auto& received_packet : this->received_packets_) {
321 this->fec_.DecodeFec(*received_packet,
322 &this->recovered_packets_);
323 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000324
325 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700326 EXPECT_TRUE(this->IsRecoveryComplete());
327 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000328
329 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700330 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
331 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
332 this->media_loss_mask_[1] = 1;
333 this->media_loss_mask_[3] = 1;
334 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000335
nissea5f043f2017-09-18 07:58:59 -0700336 for (const auto& received_packet : this->received_packets_) {
337 this->fec_.DecodeFec(*received_packet,
338 &this->recovered_packets_);
339 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000340
341 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700342 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000343}
344
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000345// Verify that we don't use an old FEC packet for FEC decoding.
Niels Möller958288a2017-10-02 13:51:36 +0200346TYPED_TEST(RtpFecTest, NoFecRecoveryWithOldFecPacket) {
brandtrd90fa0b2016-08-09 06:57:14 -0700347 constexpr int kNumImportantPackets = 0;
348 constexpr bool kUseUnequalProtection = false;
349 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000350
351 // Two frames: first frame (old) with two media packets and 1 FEC packet.
Niels Möller958288a2017-10-02 13:51:36 +0200352 // Third frame (new) with 3 media packets, and no FEC packets.
353 //
354 // #0(media) #1(media) #2(FEC) ----Frame 1-----
355 // #32767(media) 32768(media) 32769(media) ----Frame 2-----
356 // #65535(media) #0(media) #1(media). ----Frame 3-----
357 // If we lose either packet 0 or 1 of third frame, FEC decoding should not
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000358 // try to decode using "old" FEC packet #2.
359
360 // Construct media packets for first frame, starting at sequence number 0.
brandtrece4aba2016-09-20 23:16:28 -0700361 this->media_packets_ =
362 this->media_packet_generator_.ConstructMediaPackets(2, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000363
brandtrece4aba2016-09-20 23:16:28 -0700364 EXPECT_EQ(
365 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
366 kNumImportantPackets, kUseUnequalProtection,
367 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000368 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700369 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000370 // Add FEC packet (seq#2) of this first frame to received list (i.e., assume
371 // the two media packet were lost).
brandtrece4aba2016-09-20 23:16:28 -0700372 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
373 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
374 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000375
376 // Construct media packets for second frame, with sequence number wrap.
brandtrece4aba2016-09-20 23:16:28 -0700377 this->media_packets_ =
Niels Möller958288a2017-10-02 13:51:36 +0200378 this->media_packet_generator_.ConstructMediaPackets(3, 32767);
379
380 // Expect 3 media packets for this frame.
381 EXPECT_EQ(3u, this->media_packets_.size());
382
383 // No packets lost
384 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
385 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
386
387 // Construct media packets for third frame, with sequence number wrap.
388 this->media_packets_ =
brandtrece4aba2016-09-20 23:16:28 -0700389 this->media_packet_generator_.ConstructMediaPackets(3, 65535);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000390
391 // Expect 3 media packets for this frame.
brandtrece4aba2016-09-20 23:16:28 -0700392 EXPECT_EQ(3u, this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000393
394 // Second media packet lost (seq#0).
brandtrece4aba2016-09-20 23:16:28 -0700395 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
396 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000397 // Add packets #65535, and #1 to received list.
brandtrece4aba2016-09-20 23:16:28 -0700398 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000399
nissea5f043f2017-09-18 07:58:59 -0700400 for (const auto& received_packet : this->received_packets_) {
401 this->fec_.DecodeFec(*received_packet,
402 &this->recovered_packets_);
403 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000404
Niels Möller958288a2017-10-02 13:51:36 +0200405 // Expect that no decoding is done to get missing packet (seq#0) of third
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000406 // frame, using old FEC packet (seq#2) from first (old) frame. So number of
Niels Möller958288a2017-10-02 13:51:36 +0200407 // recovered packets is 5 (0 from first frame, three from second frame, and 2
408 // for the third frame, with no packets recovered via FEC).
409 EXPECT_EQ(5u, this->recovered_packets_.size());
brandtrece4aba2016-09-20 23:16:28 -0700410 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000411}
412
brandtrd90fa0b2016-08-09 06:57:14 -0700413// Verify we can still recover frame if sequence number wrap occurs within
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000414// the frame and FEC packet following wrap is received after media packets.
brandtrece4aba2016-09-20 23:16:28 -0700415TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700416 constexpr int kNumImportantPackets = 0;
417 constexpr bool kUseUnequalProtection = false;
418 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000419
420 // One frame, with sequence number wrap in media packets.
421 // -----Frame 1----
422 // #65534(media) #65535(media) #0(media) #1(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700423 this->media_packets_ =
424 this->media_packet_generator_.ConstructMediaPackets(3, 65534);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000425
brandtrece4aba2016-09-20 23:16:28 -0700426 EXPECT_EQ(
427 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
428 kNumImportantPackets, kUseUnequalProtection,
429 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000430
431 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700432 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000433
434 // Lose one media packet (seq# 65535).
brandtrece4aba2016-09-20 23:16:28 -0700435 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
436 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
437 this->media_loss_mask_[1] = 1;
438 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000439 // Add FEC packet to received list following the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700440 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
441 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000442
nissea5f043f2017-09-18 07:58:59 -0700443 for (const auto& received_packet : this->received_packets_) {
444 this->fec_.DecodeFec(*received_packet,
445 &this->recovered_packets_);
446 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000447
448 // Expect 3 media packets in recovered list, and complete recovery.
449 // Wrap-around won't remove FEC packet, as it follows the wrap.
brandtrece4aba2016-09-20 23:16:28 -0700450 EXPECT_EQ(3u, this->recovered_packets_.size());
451 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000452}
453
brandtrd726a3f2017-06-29 02:45:35 -0700454// Sequence number wrap occurs within the ULPFEC packets for the frame.
brandtrd726a3f2017-06-29 02:45:35 -0700455// Same problem will occur if wrap is within media packets but ULPFEC packet is
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000456// received before the media packets. This may be improved if timing information
brandtrd726a3f2017-06-29 02:45:35 -0700457// is used to detect old ULPFEC packets.
nissea5f043f2017-09-18 07:58:59 -0700458
459// TODO(nisse): There's some logic to discard ULPFEC packets at wrap-around,
460// however, that is not actually exercised by this test: When the first FEC
461// packet is processed, it results in full recovery of one media packet and the
462// FEC packet is forgotten. And then the wraparound isn't noticed when the next
463// FEC packet is received. We should fix wraparound handling, which currently
464// appears broken, and then figure out how to test it properly.
brandtrd726a3f2017-06-29 02:45:35 -0700465using RtpFecTestUlpfecOnly = RtpFecTest<UlpfecForwardErrorCorrection>;
nissea5f043f2017-09-18 07:58:59 -0700466TEST_F(RtpFecTestUlpfecOnly, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700467 constexpr int kNumImportantPackets = 0;
468 constexpr bool kUseUnequalProtection = false;
469 constexpr uint8_t kProtectionFactor = 200;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000470
471 // 1 frame: 3 media packets and 2 FEC packets.
472 // Sequence number wrap in FEC packets.
473 // -----Frame 1----
474 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700475 this->media_packets_ =
476 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000477
brandtrece4aba2016-09-20 23:16:28 -0700478 EXPECT_EQ(
479 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
480 kNumImportantPackets, kUseUnequalProtection,
481 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000482
483 // Expect 2 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700484 EXPECT_EQ(2u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000485
486 // Lose the last two media packets (seq# 65533, 65534).
brandtrece4aba2016-09-20 23:16:28 -0700487 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
488 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
489 this->media_loss_mask_[1] = 1;
490 this->media_loss_mask_[2] = 1;
491 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
492 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
493 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000494
nissea5f043f2017-09-18 07:58:59 -0700495 for (const auto& received_packet : this->received_packets_) {
496 this->fec_.DecodeFec(*received_packet,
497 &this->recovered_packets_);
498 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000499
500 // The two FEC packets are received and should allow for complete recovery,
brandtrd726a3f2017-06-29 02:45:35 -0700501 // but because of the wrap the first FEC packet will be discarded, and only
502 // one media packet is recoverable. So expect 2 media packets on recovered
503 // list and no complete recovery.
nissea5f043f2017-09-18 07:58:59 -0700504 EXPECT_EQ(3u, this->recovered_packets_.size());
505 EXPECT_EQ(this->recovered_packets_.size(), this->media_packets_.size());
506 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd726a3f2017-06-29 02:45:35 -0700507}
508
509// TODO(brandtr): This test mimics the one above, ensuring that the recovery
510// strategy of FlexFEC matches the recovery strategy of ULPFEC. Since FlexFEC
511// does not share the sequence number space with the media, however, having a
512// matching recovery strategy may be suboptimal. Study this further.
nissea5f043f2017-09-18 07:58:59 -0700513// TODO(nisse): In this test, recovery based on the first FEC packet fails with
514// the log message "The recovered packet had a length larger than a typical IP
515// packet, and is thus dropped." This is probably not intended, and needs
516// investigation.
brandtrd726a3f2017-06-29 02:45:35 -0700517using RtpFecTestFlexfecOnly = RtpFecTest<FlexfecForwardErrorCorrection>;
518TEST_F(RtpFecTestFlexfecOnly, FecRecoveryWithSeqNumGapOneFrameNoRecovery) {
519 constexpr int kNumImportantPackets = 0;
520 constexpr bool kUseUnequalProtection = false;
521 constexpr uint8_t kProtectionFactor = 200;
522
523 // 1 frame: 3 media packets and 2 FEC packets.
524 // Sequence number wrap in FEC packets.
525 // -----Frame 1----
526 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
527 this->media_packets_ =
528 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
529
530 EXPECT_EQ(
531 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
532 kNumImportantPackets, kUseUnequalProtection,
533 kFecMaskBursty, &this->generated_fec_packets_));
534
535 // Expect 2 FEC packets.
536 EXPECT_EQ(2u, this->generated_fec_packets_.size());
537
538 // Overwrite the sequence numbers generated by ConstructMediaPackets,
539 // to make sure that we do have a wrap.
540 auto it = this->generated_fec_packets_.begin();
541 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 65535);
542 ++it;
543 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 0);
544
545 // Lose the last two media packets (seq# 65533, 65534).
546 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
547 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
548 this->media_loss_mask_[1] = 1;
549 this->media_loss_mask_[2] = 1;
550 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
551 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
552 true);
553
nissea5f043f2017-09-18 07:58:59 -0700554 for (const auto& received_packet : this->received_packets_) {
555 this->fec_.DecodeFec(*received_packet,
556 &this->recovered_packets_);
557 }
brandtrd726a3f2017-06-29 02:45:35 -0700558
559 // The two FEC packets are received and should allow for complete recovery,
560 // but because of the wrap the first FEC packet will be discarded, and only
561 // one media packet is recoverable. So expect 2 media packets on recovered
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000562 // list and no complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700563 EXPECT_EQ(2u, this->recovered_packets_.size());
564 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
565 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000566}
567
brandtrd90fa0b2016-08-09 06:57:14 -0700568// Verify we can still recover frame if media packets are reordered.
brandtrece4aba2016-09-20 23:16:28 -0700569TYPED_TEST(RtpFecTest, FecRecoveryWithMediaOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700570 constexpr int kNumImportantPackets = 0;
571 constexpr bool kUseUnequalProtection = false;
572 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000573
574 // One frame: 3 media packets, 1 FEC packet.
575 // -----Frame 1----
576 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700577 this->media_packets_ =
578 this->media_packet_generator_.ConstructMediaPackets(3, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000579
brandtrece4aba2016-09-20 23:16:28 -0700580 EXPECT_EQ(
581 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
582 kNumImportantPackets, kUseUnequalProtection,
583 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000584
585 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700586 EXPECT_EQ(1u, this->generated_fec_packets_.size());
brandtrd90fa0b2016-08-09 06:57:14 -0700587
588 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700589 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
590 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
591 this->media_loss_mask_[1] = 1;
592 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
brandtrd90fa0b2016-08-09 06:57:14 -0700593
594 // Reorder received media packets.
brandtrece4aba2016-09-20 23:16:28 -0700595 auto it0 = this->received_packets_.begin();
brandtrd726a3f2017-06-29 02:45:35 -0700596 auto it1 = this->received_packets_.begin();
597 it1++;
598 std::swap(*it0, *it1);
brandtrd90fa0b2016-08-09 06:57:14 -0700599
nissea5f043f2017-09-18 07:58:59 -0700600 for (const auto& received_packet : this->received_packets_) {
601 this->fec_.DecodeFec(*received_packet,
602 &this->recovered_packets_);
603 }
brandtrd90fa0b2016-08-09 06:57:14 -0700604
605 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700606 EXPECT_EQ(3u, this->recovered_packets_.size());
607 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd90fa0b2016-08-09 06:57:14 -0700608}
609
610// Verify we can still recover frame if FEC is received before media packets.
brandtrece4aba2016-09-20 23:16:28 -0700611TYPED_TEST(RtpFecTest, FecRecoveryWithFecOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700612 constexpr int kNumImportantPackets = 0;
613 constexpr bool kUseUnequalProtection = false;
614 constexpr uint8_t kProtectionFactor = 20;
615
616 // One frame: 3 media packets, 1 FEC packet.
617 // -----Frame 1----
618 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700619 this->media_packets_ =
620 this->media_packet_generator_.ConstructMediaPackets(3, 0);
brandtrd90fa0b2016-08-09 06:57:14 -0700621
brandtrece4aba2016-09-20 23:16:28 -0700622 EXPECT_EQ(
623 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
624 kNumImportantPackets, kUseUnequalProtection,
625 kFecMaskBursty, &this->generated_fec_packets_));
brandtrd90fa0b2016-08-09 06:57:14 -0700626
627 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700628 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000629
630 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700631 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
632 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
633 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000634 // Add FEC packet to received list before the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700635 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
636 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000637 // Add media packets to received list.
brandtrece4aba2016-09-20 23:16:28 -0700638 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000639
nissea5f043f2017-09-18 07:58:59 -0700640 for (const auto& received_packet : this->received_packets_) {
641 this->fec_.DecodeFec(*received_packet,
642 &this->recovered_packets_);
643 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000644
645 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700646 EXPECT_EQ(3u, this->recovered_packets_.size());
647 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000648}
649
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000650// Test 50% protection with random mask type: Two cases are considered:
651// a 50% non-consecutive loss which can be fully recovered, and a 50%
652// consecutive loss which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700653TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700654 constexpr int kNumImportantPackets = 0;
655 constexpr bool kUseUnequalProtection = false;
656 constexpr int kNumMediaPackets = 4;
657 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000658
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000659 // Packet Mask for (4,4,0) code, from random mask table.
660 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000661
662 // media#0 media#1 media#2 media#3
663 // fec#0: 1 1 0 0
664 // fec#1: 1 0 1 0
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000665 // fec#2: 0 0 1 1
666 // fec#3: 0 1 0 1
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000667 //
668
brandtrece4aba2016-09-20 23:16:28 -0700669 this->media_packets_ =
670 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000671
brandtrece4aba2016-09-20 23:16:28 -0700672 EXPECT_EQ(
673 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
674 kNumImportantPackets, kUseUnequalProtection,
675 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000676
677 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700678 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000679
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000680 // 4 packets lost: 3 media packets (0, 2, 3), and one FEC packet (0) lost.
brandtrece4aba2016-09-20 23:16:28 -0700681 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
682 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
683 this->fec_loss_mask_[0] = 1;
684 this->media_loss_mask_[0] = 1;
685 this->media_loss_mask_[2] = 1;
686 this->media_loss_mask_[3] = 1;
687 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000688
nissea5f043f2017-09-18 07:58:59 -0700689 for (const auto& received_packet : this->received_packets_) {
690 this->fec_.DecodeFec(*received_packet,
691 &this->recovered_packets_);
692 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000693
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000694 // With media packet#1 and FEC packets #1, #2, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700695 EXPECT_TRUE(this->IsRecoveryComplete());
696 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000697
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000698 // 4 consecutive packets lost: media packets 0, 1, 2, 3.
brandtrece4aba2016-09-20 23:16:28 -0700699 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
700 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
701 this->media_loss_mask_[0] = 1;
702 this->media_loss_mask_[1] = 1;
703 this->media_loss_mask_[2] = 1;
704 this->media_loss_mask_[3] = 1;
705 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000706
nissea5f043f2017-09-18 07:58:59 -0700707 for (const auto& received_packet : this->received_packets_) {
708 this->fec_.DecodeFec(*received_packet,
709 &this->recovered_packets_);
710 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000711
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000712 // Cannot get complete recovery for this loss configuration with random mask.
brandtrece4aba2016-09-20 23:16:28 -0700713 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000714}
715
716// Test 50% protection with bursty type: Three cases are considered:
717// two 50% consecutive losses which can be fully recovered, and one
718// non-consecutive which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700719TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percBurstyMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700720 constexpr int kNumImportantPackets = 0;
721 constexpr bool kUseUnequalProtection = false;
722 constexpr int kNumMediaPackets = 4;
723 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000724
725 // Packet Mask for (4,4,0) code, from bursty mask table.
726 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
727
728 // media#0 media#1 media#2 media#3
729 // fec#0: 1 0 0 0
730 // fec#1: 1 1 0 0
731 // fec#2: 0 1 1 0
732 // fec#3: 0 0 1 1
733 //
734
brandtrece4aba2016-09-20 23:16:28 -0700735 this->media_packets_ =
736 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000737
brandtrece4aba2016-09-20 23:16:28 -0700738 EXPECT_EQ(
739 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
740 kNumImportantPackets, kUseUnequalProtection,
741 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000742
743 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700744 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000745
746 // 4 consecutive packets lost: media packets 0,1,2,3.
brandtrece4aba2016-09-20 23:16:28 -0700747 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
748 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
749 this->media_loss_mask_[0] = 1;
750 this->media_loss_mask_[1] = 1;
751 this->media_loss_mask_[2] = 1;
752 this->media_loss_mask_[3] = 1;
753 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000754
nissea5f043f2017-09-18 07:58:59 -0700755 for (const auto& received_packet : this->received_packets_) {
756 this->fec_.DecodeFec(*received_packet,
757 &this->recovered_packets_);
758 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000759
760 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700761 EXPECT_TRUE(this->IsRecoveryComplete());
762 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000763
764 // 4 consecutive packets lost: media packets 1,2, 3, and FEC packet 0.
brandtrece4aba2016-09-20 23:16:28 -0700765 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
766 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
767 this->fec_loss_mask_[0] = 1;
768 this->media_loss_mask_[1] = 1;
769 this->media_loss_mask_[2] = 1;
770 this->media_loss_mask_[3] = 1;
771 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000772
nissea5f043f2017-09-18 07:58:59 -0700773 for (const auto& received_packet : this->received_packets_) {
774 this->fec_.DecodeFec(*received_packet,
775 &this->recovered_packets_);
776 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000777
778 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700779 EXPECT_TRUE(this->IsRecoveryComplete());
780 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000781
782 // 4 packets lost (non-consecutive loss): media packets 0, 3, and FEC# 0, 3.
brandtrece4aba2016-09-20 23:16:28 -0700783 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
784 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
785 this->fec_loss_mask_[0] = 1;
786 this->fec_loss_mask_[3] = 1;
787 this->media_loss_mask_[0] = 1;
788 this->media_loss_mask_[3] = 1;
789 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000790
nissea5f043f2017-09-18 07:58:59 -0700791 for (const auto& received_packet : this->received_packets_) {
792 this->fec_.DecodeFec(*received_packet,
793 &this->recovered_packets_);
794 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000795
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000796 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700797 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000798}
799
brandtrece4aba2016-09-20 23:16:28 -0700800TYPED_TEST(RtpFecTest, FecRecoveryNoLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700801 constexpr int kNumImportantPackets = 2;
802 constexpr bool kUseUnequalProtection = true;
803 constexpr int kNumMediaPackets = 4;
804 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000805
brandtrece4aba2016-09-20 23:16:28 -0700806 this->media_packets_ =
807 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000808
brandtrece4aba2016-09-20 23:16:28 -0700809 EXPECT_EQ(
810 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
811 kNumImportantPackets, kUseUnequalProtection,
812 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000813
814 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700815 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000816
817 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700818 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
819 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
820 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000821
nissea5f043f2017-09-18 07:58:59 -0700822 for (const auto& received_packet : this->received_packets_) {
823 this->fec_.DecodeFec(*received_packet,
824 &this->recovered_packets_);
825 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000826
827 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700828 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000829}
830
brandtrece4aba2016-09-20 23:16:28 -0700831TYPED_TEST(RtpFecTest, FecRecoveryWithLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700832 constexpr int kNumImportantPackets = 2;
833 constexpr bool kUseUnequalProtection = true;
834 constexpr int kNumMediaPackets = 4;
835 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000836
brandtrece4aba2016-09-20 23:16:28 -0700837 this->media_packets_ =
838 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000839
brandtrece4aba2016-09-20 23:16:28 -0700840 EXPECT_EQ(
841 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
842 kNumImportantPackets, kUseUnequalProtection,
843 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000844
845 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700846 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000847
848 // 1 media packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700849 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
850 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
851 this->media_loss_mask_[3] = 1;
852 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000853
nissea5f043f2017-09-18 07:58:59 -0700854 for (const auto& received_packet : this->received_packets_) {
855 this->fec_.DecodeFec(*received_packet,
856 &this->recovered_packets_);
857 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000858
859 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700860 EXPECT_TRUE(this->IsRecoveryComplete());
861 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000862
863 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700864 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
865 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
866 this->media_loss_mask_[1] = 1;
867 this->media_loss_mask_[3] = 1;
868 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000869
nissea5f043f2017-09-18 07:58:59 -0700870 for (const auto& received_packet : this->received_packets_) {
871 this->fec_.DecodeFec(*received_packet,
872 &this->recovered_packets_);
873 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000874
875 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700876 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000877}
878
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000879// Test 50% protection with random mask type for UEP on.
brandtrece4aba2016-09-20 23:16:28 -0700880TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percUepRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700881 constexpr int kNumImportantPackets = 1;
882 constexpr bool kUseUnequalProtection = true;
883 constexpr int kNumMediaPackets = 4;
884 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000885
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000886 // Packet Mask for (4,4,1) code, from random mask table.
887 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 1)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000888
889 // media#0 media#1 media#2 media#3
890 // fec#0: 1 0 0 0
891 // fec#1: 1 1 0 0
892 // fec#2: 1 0 1 1
893 // fec#3: 0 1 1 0
894 //
895
brandtrece4aba2016-09-20 23:16:28 -0700896 this->media_packets_ =
897 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000898
brandtrece4aba2016-09-20 23:16:28 -0700899 EXPECT_EQ(
900 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
901 kNumImportantPackets, kUseUnequalProtection,
902 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000903
904 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700905 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000906
907 // 4 packets lost: 3 media packets and FEC packet#1 lost.
brandtrece4aba2016-09-20 23:16:28 -0700908 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
909 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
910 this->fec_loss_mask_[1] = 1;
911 this->media_loss_mask_[0] = 1;
912 this->media_loss_mask_[2] = 1;
913 this->media_loss_mask_[3] = 1;
914 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000915
nissea5f043f2017-09-18 07:58:59 -0700916 for (const auto& received_packet : this->received_packets_) {
917 this->fec_.DecodeFec(*received_packet,
918 &this->recovered_packets_);
919 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000920
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000921 // With media packet#3 and FEC packets #0, #1, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700922 EXPECT_TRUE(this->IsRecoveryComplete());
923 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000924
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000925 // 5 packets lost: 4 media packets and one FEC packet#2 lost.
brandtrece4aba2016-09-20 23:16:28 -0700926 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
927 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
928 this->fec_loss_mask_[2] = 1;
929 this->media_loss_mask_[0] = 1;
930 this->media_loss_mask_[1] = 1;
931 this->media_loss_mask_[2] = 1;
932 this->media_loss_mask_[3] = 1;
933 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000934
nissea5f043f2017-09-18 07:58:59 -0700935 for (const auto& received_packet : this->received_packets_) {
936 this->fec_.DecodeFec(*received_packet,
937 &this->recovered_packets_);
938 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000939
940 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700941 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000942}
943
brandtrece4aba2016-09-20 23:16:28 -0700944TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePackets) {
brandtrd90fa0b2016-08-09 06:57:14 -0700945 constexpr int kNumImportantPackets = 0;
946 constexpr bool kUseUnequalProtection = false;
947 constexpr int kNumMediaPackets = 5;
948 constexpr uint8_t kProtectionFactor = 60;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000949
brandtrece4aba2016-09-20 23:16:28 -0700950 this->media_packets_ =
951 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000952
953 // Create a new temporary packet list for generating FEC packets.
954 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700955 ForwardErrorCorrection::PacketList protected_media_packets;
956 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000957
brandtrece4aba2016-09-20 23:16:28 -0700958 EXPECT_EQ(
959 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
960 kNumImportantPackets, kUseUnequalProtection,
961 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000962
963 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700964 EXPECT_EQ(1u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000965
966 // 1 protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700967 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
968 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
969 this->media_loss_mask_[2] = 1;
970 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000971
nissea5f043f2017-09-18 07:58:59 -0700972 for (const auto& received_packet : this->received_packets_) {
973 this->fec_.DecodeFec(*received_packet,
974 &this->recovered_packets_);
975 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000976
977 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700978 EXPECT_TRUE(this->IsRecoveryComplete());
979 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000980
981 // Unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700982 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
983 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
984 this->media_loss_mask_[1] = 1;
985 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000986
nissea5f043f2017-09-18 07:58:59 -0700987 for (const auto& received_packet : this->received_packets_) {
988 this->fec_.DecodeFec(*received_packet,
989 &this->recovered_packets_);
990 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000991
992 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -0700993 EXPECT_FALSE(this->IsRecoveryComplete());
994 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000995
996 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700997 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
998 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
999 this->media_loss_mask_[0] = 1;
1000 this->media_loss_mask_[2] = 1;
1001 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001002
nissea5f043f2017-09-18 07:58:59 -07001003 for (const auto& received_packet : this->received_packets_) {
1004 this->fec_.DecodeFec(*received_packet,
1005 &this->recovered_packets_);
1006 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001007
1008 // 2 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001009 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001010}
1011
brandtrece4aba2016-09-20 23:16:28 -07001012TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsExtension) {
brandtrd90fa0b2016-08-09 06:57:14 -07001013 constexpr int kNumImportantPackets = 0;
1014 constexpr bool kUseUnequalProtection = false;
1015 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001016 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001017
brandtrece4aba2016-09-20 23:16:28 -07001018 this->media_packets_ =
1019 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001020
1021 // Create a new temporary packet list for generating FEC packets.
1022 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001023 ForwardErrorCorrection::PacketList protected_media_packets;
1024 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001025
1026 // Zero column insertion will have to extend the size of the packet
1027 // mask since the number of actual packets are 21, while the number
1028 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001029 EXPECT_EQ(
1030 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1031 kNumImportantPackets, kUseUnequalProtection,
1032 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001033
1034 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001035 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001036
1037 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001038 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1039 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1040 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1041 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001042
nissea5f043f2017-09-18 07:58:59 -07001043 for (const auto& received_packet : this->received_packets_) {
1044 this->fec_.DecodeFec(*received_packet,
1045 &this->recovered_packets_);
1046 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001047
1048 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001049 EXPECT_TRUE(this->IsRecoveryComplete());
1050 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001051
1052 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001053 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1054 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1055 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1056 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001057
nissea5f043f2017-09-18 07:58:59 -07001058 for (const auto& received_packet : this->received_packets_) {
1059 this->fec_.DecodeFec(*received_packet,
1060 &this->recovered_packets_);
1061 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001062
1063 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001064 EXPECT_FALSE(this->IsRecoveryComplete());
1065 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001066
1067 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001068 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1069 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1070 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1071 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1072 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1073 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1074 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1075 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1076 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001077
nissea5f043f2017-09-18 07:58:59 -07001078 for (const auto& received_packet : this->received_packets_) {
1079 this->fec_.DecodeFec(*received_packet,
1080 &this->recovered_packets_);
1081 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001082
1083 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001084 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001085}
1086
brandtrece4aba2016-09-20 23:16:28 -07001087TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
brandtrd90fa0b2016-08-09 06:57:14 -07001088 constexpr int kNumImportantPackets = 0;
1089 constexpr bool kUseUnequalProtection = false;
1090 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001091 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001092
brandtrece4aba2016-09-20 23:16:28 -07001093 this->media_packets_ = this->media_packet_generator_.ConstructMediaPackets(
1094 kNumMediaPackets, 0xFFFF - 5);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001095
1096 // Create a new temporary packet list for generating FEC packets.
1097 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001098 ForwardErrorCorrection::PacketList protected_media_packets;
1099 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001100
1101 // Zero column insertion will have to extend the size of the packet
1102 // mask since the number of actual packets are 21, while the number
1103 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001104 EXPECT_EQ(
1105 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1106 kNumImportantPackets, kUseUnequalProtection,
1107 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001108
1109 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001110 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001111
1112 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001113 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1114 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1115 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1116 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001117
nissea5f043f2017-09-18 07:58:59 -07001118 for (const auto& received_packet : this->received_packets_) {
1119 this->fec_.DecodeFec(*received_packet,
1120 &this->recovered_packets_);
1121 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001122
1123 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001124 EXPECT_TRUE(this->IsRecoveryComplete());
1125 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001126
1127 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001128 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1129 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1130 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1131 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001132
nissea5f043f2017-09-18 07:58:59 -07001133 for (const auto& received_packet : this->received_packets_) {
1134 this->fec_.DecodeFec(*received_packet,
1135 &this->recovered_packets_);
1136 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001137
1138 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001139 EXPECT_FALSE(this->IsRecoveryComplete());
1140 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001141
1142 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001143 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1144 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1145 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1146 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1147 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1148 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1149 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1150 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1151 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001152
nissea5f043f2017-09-18 07:58:59 -07001153 for (const auto& received_packet : this->received_packets_) {
1154 this->fec_.DecodeFec(*received_packet,
1155 &this->recovered_packets_);
1156 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001157
1158 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001159 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001160}
1161
brandtrece4aba2016-09-20 23:16:28 -07001162} // namespace webrtc