blob: d260db4166201a151c210834e3bbd8d56c81f6c6 [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.
Yves Gerey665174f2018-06-19 15:03:05 +0200158 auto cmp =
159 [](const std::unique_ptr<ForwardErrorCorrection::Packet>& media_packet,
160 const std::unique_ptr<ForwardErrorCorrection::RecoveredPacket>&
161 recovered_packet) {
162 if (media_packet->length != recovered_packet->pkt->length) {
163 return false;
164 }
165 if (memcmp(media_packet->data, recovered_packet->pkt->data,
166 media_packet->length) != 0) {
167 return false;
168 }
169 return true;
170 };
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200171 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>;
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100219TYPED_TEST_SUITE(RtpFecTest, FecTypes);
brandtrece4aba2016-09-20 23:16:28 -0700220
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_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200289 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700290 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000291
292 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700293 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000294}
295
brandtrece4aba2016-09-20 23:16:28 -0700296TYPED_TEST(RtpFecTest, FecRecoveryWithLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700297 constexpr int kNumImportantPackets = 0;
298 constexpr bool kUseUnequalProtection = false;
299 constexpr int kNumMediaPackets = 4;
300 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000301
brandtrece4aba2016-09-20 23:16:28 -0700302 this->media_packets_ =
303 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000304
brandtrece4aba2016-09-20 23:16:28 -0700305 EXPECT_EQ(
306 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
307 kNumImportantPackets, kUseUnequalProtection,
308 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000309
310 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700311 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000312
313 // 1 media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700314 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
315 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
316 this->media_loss_mask_[3] = 1;
317 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000318
nissea5f043f2017-09-18 07:58:59 -0700319 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200320 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700321 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000322
323 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700324 EXPECT_TRUE(this->IsRecoveryComplete());
325 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000326
327 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700328 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
329 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
330 this->media_loss_mask_[1] = 1;
331 this->media_loss_mask_[3] = 1;
332 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000333
nissea5f043f2017-09-18 07:58:59 -0700334 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200335 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700336 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000337
338 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700339 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000340}
341
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000342// Verify that we don't use an old FEC packet for FEC decoding.
Niels Möller958288a2017-10-02 13:51:36 +0200343TYPED_TEST(RtpFecTest, NoFecRecoveryWithOldFecPacket) {
brandtrd90fa0b2016-08-09 06:57:14 -0700344 constexpr int kNumImportantPackets = 0;
345 constexpr bool kUseUnequalProtection = false;
346 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000347
348 // Two frames: first frame (old) with two media packets and 1 FEC packet.
Niels Möller958288a2017-10-02 13:51:36 +0200349 // Third frame (new) with 3 media packets, and no FEC packets.
350 //
351 // #0(media) #1(media) #2(FEC) ----Frame 1-----
352 // #32767(media) 32768(media) 32769(media) ----Frame 2-----
353 // #65535(media) #0(media) #1(media). ----Frame 3-----
354 // If we lose either packet 0 or 1 of third frame, FEC decoding should not
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000355 // try to decode using "old" FEC packet #2.
356
357 // Construct media packets for first frame, starting at sequence number 0.
brandtrece4aba2016-09-20 23:16:28 -0700358 this->media_packets_ =
359 this->media_packet_generator_.ConstructMediaPackets(2, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000360
brandtrece4aba2016-09-20 23:16:28 -0700361 EXPECT_EQ(
362 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
363 kNumImportantPackets, kUseUnequalProtection,
364 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000365 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700366 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000367 // Add FEC packet (seq#2) of this first frame to received list (i.e., assume
368 // the two media packet were lost).
brandtrece4aba2016-09-20 23:16:28 -0700369 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
370 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
371 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000372
373 // Construct media packets for second frame, with sequence number wrap.
brandtrece4aba2016-09-20 23:16:28 -0700374 this->media_packets_ =
Niels Möller958288a2017-10-02 13:51:36 +0200375 this->media_packet_generator_.ConstructMediaPackets(3, 32767);
376
377 // Expect 3 media packets for this frame.
378 EXPECT_EQ(3u, this->media_packets_.size());
379
380 // No packets lost
381 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
382 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
383
384 // Construct media packets for third frame, with sequence number wrap.
385 this->media_packets_ =
brandtrece4aba2016-09-20 23:16:28 -0700386 this->media_packet_generator_.ConstructMediaPackets(3, 65535);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000387
388 // Expect 3 media packets for this frame.
brandtrece4aba2016-09-20 23:16:28 -0700389 EXPECT_EQ(3u, this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000390
391 // Second media packet lost (seq#0).
brandtrece4aba2016-09-20 23:16:28 -0700392 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
393 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000394 // Add packets #65535, and #1 to received list.
brandtrece4aba2016-09-20 23:16:28 -0700395 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000396
nissea5f043f2017-09-18 07:58:59 -0700397 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200398 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700399 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000400
Niels Möller958288a2017-10-02 13:51:36 +0200401 // Expect that no decoding is done to get missing packet (seq#0) of third
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000402 // frame, using old FEC packet (seq#2) from first (old) frame. So number of
Niels Möller958288a2017-10-02 13:51:36 +0200403 // recovered packets is 5 (0 from first frame, three from second frame, and 2
404 // for the third frame, with no packets recovered via FEC).
405 EXPECT_EQ(5u, this->recovered_packets_.size());
brandtrece4aba2016-09-20 23:16:28 -0700406 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000407}
408
brandtrd90fa0b2016-08-09 06:57:14 -0700409// Verify we can still recover frame if sequence number wrap occurs within
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000410// the frame and FEC packet following wrap is received after media packets.
brandtrece4aba2016-09-20 23:16:28 -0700411TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700412 constexpr int kNumImportantPackets = 0;
413 constexpr bool kUseUnequalProtection = false;
414 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000415
416 // One frame, with sequence number wrap in media packets.
417 // -----Frame 1----
418 // #65534(media) #65535(media) #0(media) #1(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700419 this->media_packets_ =
420 this->media_packet_generator_.ConstructMediaPackets(3, 65534);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000421
brandtrece4aba2016-09-20 23:16:28 -0700422 EXPECT_EQ(
423 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
424 kNumImportantPackets, kUseUnequalProtection,
425 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000426
427 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700428 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000429
430 // Lose one media packet (seq# 65535).
brandtrece4aba2016-09-20 23:16:28 -0700431 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
432 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
433 this->media_loss_mask_[1] = 1;
434 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000435 // Add FEC packet to received list following the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700436 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
437 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000438
nissea5f043f2017-09-18 07:58:59 -0700439 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200440 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700441 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000442
443 // Expect 3 media packets in recovered list, and complete recovery.
444 // Wrap-around won't remove FEC packet, as it follows the wrap.
brandtrece4aba2016-09-20 23:16:28 -0700445 EXPECT_EQ(3u, this->recovered_packets_.size());
446 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000447}
448
brandtrd726a3f2017-06-29 02:45:35 -0700449// Sequence number wrap occurs within the ULPFEC packets for the frame.
brandtrd726a3f2017-06-29 02:45:35 -0700450// Same problem will occur if wrap is within media packets but ULPFEC packet is
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000451// received before the media packets. This may be improved if timing information
brandtrd726a3f2017-06-29 02:45:35 -0700452// is used to detect old ULPFEC packets.
nissea5f043f2017-09-18 07:58:59 -0700453
454// TODO(nisse): There's some logic to discard ULPFEC packets at wrap-around,
455// however, that is not actually exercised by this test: When the first FEC
456// packet is processed, it results in full recovery of one media packet and the
457// FEC packet is forgotten. And then the wraparound isn't noticed when the next
458// FEC packet is received. We should fix wraparound handling, which currently
459// appears broken, and then figure out how to test it properly.
brandtrd726a3f2017-06-29 02:45:35 -0700460using RtpFecTestUlpfecOnly = RtpFecTest<UlpfecForwardErrorCorrection>;
nissea5f043f2017-09-18 07:58:59 -0700461TEST_F(RtpFecTestUlpfecOnly, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700462 constexpr int kNumImportantPackets = 0;
463 constexpr bool kUseUnequalProtection = false;
464 constexpr uint8_t kProtectionFactor = 200;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000465
466 // 1 frame: 3 media packets and 2 FEC packets.
467 // Sequence number wrap in FEC packets.
468 // -----Frame 1----
469 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700470 this->media_packets_ =
471 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000472
brandtrece4aba2016-09-20 23:16:28 -0700473 EXPECT_EQ(
474 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
475 kNumImportantPackets, kUseUnequalProtection,
476 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000477
478 // Expect 2 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700479 EXPECT_EQ(2u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000480
481 // Lose the last two media packets (seq# 65533, 65534).
brandtrece4aba2016-09-20 23:16:28 -0700482 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
483 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
484 this->media_loss_mask_[1] = 1;
485 this->media_loss_mask_[2] = 1;
486 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
487 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
488 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000489
nissea5f043f2017-09-18 07:58:59 -0700490 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200491 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700492 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000493
494 // The two FEC packets are received and should allow for complete recovery,
brandtrd726a3f2017-06-29 02:45:35 -0700495 // but because of the wrap the first FEC packet will be discarded, and only
496 // one media packet is recoverable. So expect 2 media packets on recovered
497 // list and no complete recovery.
nissea5f043f2017-09-18 07:58:59 -0700498 EXPECT_EQ(3u, this->recovered_packets_.size());
499 EXPECT_EQ(this->recovered_packets_.size(), this->media_packets_.size());
500 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd726a3f2017-06-29 02:45:35 -0700501}
502
503// TODO(brandtr): This test mimics the one above, ensuring that the recovery
504// strategy of FlexFEC matches the recovery strategy of ULPFEC. Since FlexFEC
505// does not share the sequence number space with the media, however, having a
506// matching recovery strategy may be suboptimal. Study this further.
nissea5f043f2017-09-18 07:58:59 -0700507// TODO(nisse): In this test, recovery based on the first FEC packet fails with
508// the log message "The recovered packet had a length larger than a typical IP
509// packet, and is thus dropped." This is probably not intended, and needs
510// investigation.
brandtrd726a3f2017-06-29 02:45:35 -0700511using RtpFecTestFlexfecOnly = RtpFecTest<FlexfecForwardErrorCorrection>;
512TEST_F(RtpFecTestFlexfecOnly, FecRecoveryWithSeqNumGapOneFrameNoRecovery) {
513 constexpr int kNumImportantPackets = 0;
514 constexpr bool kUseUnequalProtection = false;
515 constexpr uint8_t kProtectionFactor = 200;
516
517 // 1 frame: 3 media packets and 2 FEC packets.
518 // Sequence number wrap in FEC packets.
519 // -----Frame 1----
520 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
521 this->media_packets_ =
522 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
523
524 EXPECT_EQ(
525 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
526 kNumImportantPackets, kUseUnequalProtection,
527 kFecMaskBursty, &this->generated_fec_packets_));
528
529 // Expect 2 FEC packets.
530 EXPECT_EQ(2u, this->generated_fec_packets_.size());
531
532 // Overwrite the sequence numbers generated by ConstructMediaPackets,
533 // to make sure that we do have a wrap.
534 auto it = this->generated_fec_packets_.begin();
535 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 65535);
536 ++it;
537 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 0);
538
539 // Lose the last two media packets (seq# 65533, 65534).
540 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
541 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
542 this->media_loss_mask_[1] = 1;
543 this->media_loss_mask_[2] = 1;
544 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
545 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
546 true);
547
nissea5f043f2017-09-18 07:58:59 -0700548 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200549 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700550 }
brandtrd726a3f2017-06-29 02:45:35 -0700551
552 // The two FEC packets are received and should allow for complete recovery,
553 // but because of the wrap the first FEC packet will be discarded, and only
554 // one media packet is recoverable. So expect 2 media packets on recovered
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000555 // list and no complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700556 EXPECT_EQ(2u, this->recovered_packets_.size());
557 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
558 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000559}
560
brandtrd90fa0b2016-08-09 06:57:14 -0700561// Verify we can still recover frame if media packets are reordered.
brandtrece4aba2016-09-20 23:16:28 -0700562TYPED_TEST(RtpFecTest, FecRecoveryWithMediaOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700563 constexpr int kNumImportantPackets = 0;
564 constexpr bool kUseUnequalProtection = false;
565 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000566
567 // One frame: 3 media packets, 1 FEC packet.
568 // -----Frame 1----
569 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700570 this->media_packets_ =
571 this->media_packet_generator_.ConstructMediaPackets(3, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000572
brandtrece4aba2016-09-20 23:16:28 -0700573 EXPECT_EQ(
574 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
575 kNumImportantPackets, kUseUnequalProtection,
576 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000577
578 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700579 EXPECT_EQ(1u, this->generated_fec_packets_.size());
brandtrd90fa0b2016-08-09 06:57:14 -0700580
581 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700582 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
583 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
584 this->media_loss_mask_[1] = 1;
585 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
brandtrd90fa0b2016-08-09 06:57:14 -0700586
587 // Reorder received media packets.
brandtrece4aba2016-09-20 23:16:28 -0700588 auto it0 = this->received_packets_.begin();
brandtrd726a3f2017-06-29 02:45:35 -0700589 auto it1 = this->received_packets_.begin();
590 it1++;
591 std::swap(*it0, *it1);
brandtrd90fa0b2016-08-09 06:57:14 -0700592
nissea5f043f2017-09-18 07:58:59 -0700593 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200594 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700595 }
brandtrd90fa0b2016-08-09 06:57:14 -0700596
597 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700598 EXPECT_EQ(3u, this->recovered_packets_.size());
599 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd90fa0b2016-08-09 06:57:14 -0700600}
601
602// Verify we can still recover frame if FEC is received before media packets.
brandtrece4aba2016-09-20 23:16:28 -0700603TYPED_TEST(RtpFecTest, FecRecoveryWithFecOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700604 constexpr int kNumImportantPackets = 0;
605 constexpr bool kUseUnequalProtection = false;
606 constexpr uint8_t kProtectionFactor = 20;
607
608 // One frame: 3 media packets, 1 FEC packet.
609 // -----Frame 1----
610 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700611 this->media_packets_ =
612 this->media_packet_generator_.ConstructMediaPackets(3, 0);
brandtrd90fa0b2016-08-09 06:57:14 -0700613
brandtrece4aba2016-09-20 23:16:28 -0700614 EXPECT_EQ(
615 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
616 kNumImportantPackets, kUseUnequalProtection,
617 kFecMaskBursty, &this->generated_fec_packets_));
brandtrd90fa0b2016-08-09 06:57:14 -0700618
619 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700620 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000621
622 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700623 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
624 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
625 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000626 // Add FEC packet to received list before the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700627 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
628 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000629 // Add media packets to received list.
brandtrece4aba2016-09-20 23:16:28 -0700630 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000631
nissea5f043f2017-09-18 07:58:59 -0700632 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200633 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700634 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000635
636 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700637 EXPECT_EQ(3u, this->recovered_packets_.size());
638 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000639}
640
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000641// Test 50% protection with random mask type: Two cases are considered:
642// a 50% non-consecutive loss which can be fully recovered, and a 50%
643// consecutive loss which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700644TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700645 constexpr int kNumImportantPackets = 0;
646 constexpr bool kUseUnequalProtection = false;
647 constexpr int kNumMediaPackets = 4;
648 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000649
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000650 // Packet Mask for (4,4,0) code, from random mask table.
651 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000652
653 // media#0 media#1 media#2 media#3
654 // fec#0: 1 1 0 0
655 // fec#1: 1 0 1 0
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000656 // fec#2: 0 0 1 1
657 // fec#3: 0 1 0 1
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000658 //
659
brandtrece4aba2016-09-20 23:16:28 -0700660 this->media_packets_ =
661 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000662
brandtrece4aba2016-09-20 23:16:28 -0700663 EXPECT_EQ(
664 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
665 kNumImportantPackets, kUseUnequalProtection,
666 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000667
668 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700669 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000670
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000671 // 4 packets lost: 3 media packets (0, 2, 3), and one FEC packet (0) lost.
brandtrece4aba2016-09-20 23:16:28 -0700672 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
673 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
674 this->fec_loss_mask_[0] = 1;
675 this->media_loss_mask_[0] = 1;
676 this->media_loss_mask_[2] = 1;
677 this->media_loss_mask_[3] = 1;
678 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000679
nissea5f043f2017-09-18 07:58:59 -0700680 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200681 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700682 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000683
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000684 // With media packet#1 and FEC packets #1, #2, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700685 EXPECT_TRUE(this->IsRecoveryComplete());
686 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000687
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000688 // 4 consecutive packets lost: media packets 0, 1, 2, 3.
brandtrece4aba2016-09-20 23:16:28 -0700689 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
690 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
691 this->media_loss_mask_[0] = 1;
692 this->media_loss_mask_[1] = 1;
693 this->media_loss_mask_[2] = 1;
694 this->media_loss_mask_[3] = 1;
695 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000696
nissea5f043f2017-09-18 07:58:59 -0700697 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200698 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700699 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000700
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000701 // Cannot get complete recovery for this loss configuration with random mask.
brandtrece4aba2016-09-20 23:16:28 -0700702 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000703}
704
705// Test 50% protection with bursty type: Three cases are considered:
706// two 50% consecutive losses which can be fully recovered, and one
707// non-consecutive which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700708TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percBurstyMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700709 constexpr int kNumImportantPackets = 0;
710 constexpr bool kUseUnequalProtection = false;
711 constexpr int kNumMediaPackets = 4;
712 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000713
714 // Packet Mask for (4,4,0) code, from bursty mask table.
715 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
716
717 // media#0 media#1 media#2 media#3
718 // fec#0: 1 0 0 0
719 // fec#1: 1 1 0 0
720 // fec#2: 0 1 1 0
721 // fec#3: 0 0 1 1
722 //
723
brandtrece4aba2016-09-20 23:16:28 -0700724 this->media_packets_ =
725 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000726
brandtrece4aba2016-09-20 23:16:28 -0700727 EXPECT_EQ(
728 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
729 kNumImportantPackets, kUseUnequalProtection,
730 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000731
732 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700733 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000734
735 // 4 consecutive packets lost: media packets 0,1,2,3.
brandtrece4aba2016-09-20 23:16:28 -0700736 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
737 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
738 this->media_loss_mask_[0] = 1;
739 this->media_loss_mask_[1] = 1;
740 this->media_loss_mask_[2] = 1;
741 this->media_loss_mask_[3] = 1;
742 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000743
nissea5f043f2017-09-18 07:58:59 -0700744 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200745 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700746 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000747
748 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700749 EXPECT_TRUE(this->IsRecoveryComplete());
750 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000751
752 // 4 consecutive packets lost: media packets 1,2, 3, and FEC packet 0.
brandtrece4aba2016-09-20 23:16:28 -0700753 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
754 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
755 this->fec_loss_mask_[0] = 1;
756 this->media_loss_mask_[1] = 1;
757 this->media_loss_mask_[2] = 1;
758 this->media_loss_mask_[3] = 1;
759 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000760
nissea5f043f2017-09-18 07:58:59 -0700761 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200762 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700763 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000764
765 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700766 EXPECT_TRUE(this->IsRecoveryComplete());
767 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000768
769 // 4 packets lost (non-consecutive loss): media packets 0, 3, and FEC# 0, 3.
brandtrece4aba2016-09-20 23:16:28 -0700770 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
771 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
772 this->fec_loss_mask_[0] = 1;
773 this->fec_loss_mask_[3] = 1;
774 this->media_loss_mask_[0] = 1;
775 this->media_loss_mask_[3] = 1;
776 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000777
nissea5f043f2017-09-18 07:58:59 -0700778 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200779 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700780 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000781
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000782 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700783 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000784}
785
brandtrece4aba2016-09-20 23:16:28 -0700786TYPED_TEST(RtpFecTest, FecRecoveryNoLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700787 constexpr int kNumImportantPackets = 2;
788 constexpr bool kUseUnequalProtection = true;
789 constexpr int kNumMediaPackets = 4;
790 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000791
brandtrece4aba2016-09-20 23:16:28 -0700792 this->media_packets_ =
793 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000794
brandtrece4aba2016-09-20 23:16:28 -0700795 EXPECT_EQ(
796 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
797 kNumImportantPackets, kUseUnequalProtection,
798 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000799
800 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700801 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000802
803 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700804 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
805 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
806 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000807
nissea5f043f2017-09-18 07:58:59 -0700808 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200809 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700810 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000811
812 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700813 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000814}
815
brandtrece4aba2016-09-20 23:16:28 -0700816TYPED_TEST(RtpFecTest, FecRecoveryWithLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700817 constexpr int kNumImportantPackets = 2;
818 constexpr bool kUseUnequalProtection = true;
819 constexpr int kNumMediaPackets = 4;
820 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000821
brandtrece4aba2016-09-20 23:16:28 -0700822 this->media_packets_ =
823 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000824
brandtrece4aba2016-09-20 23:16:28 -0700825 EXPECT_EQ(
826 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
827 kNumImportantPackets, kUseUnequalProtection,
828 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000829
830 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700831 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000832
833 // 1 media packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700834 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
835 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
836 this->media_loss_mask_[3] = 1;
837 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000838
nissea5f043f2017-09-18 07:58:59 -0700839 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200840 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700841 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000842
843 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700844 EXPECT_TRUE(this->IsRecoveryComplete());
845 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000846
847 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700848 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
849 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
850 this->media_loss_mask_[1] = 1;
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_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200855 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700856 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000857
858 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700859 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000860}
861
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000862// Test 50% protection with random mask type for UEP on.
brandtrece4aba2016-09-20 23:16:28 -0700863TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percUepRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700864 constexpr int kNumImportantPackets = 1;
865 constexpr bool kUseUnequalProtection = true;
866 constexpr int kNumMediaPackets = 4;
867 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000868
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000869 // Packet Mask for (4,4,1) code, from random mask table.
870 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 1)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000871
872 // media#0 media#1 media#2 media#3
873 // fec#0: 1 0 0 0
874 // fec#1: 1 1 0 0
875 // fec#2: 1 0 1 1
876 // fec#3: 0 1 1 0
877 //
878
brandtrece4aba2016-09-20 23:16:28 -0700879 this->media_packets_ =
880 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000881
brandtrece4aba2016-09-20 23:16:28 -0700882 EXPECT_EQ(
883 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
884 kNumImportantPackets, kUseUnequalProtection,
885 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000886
887 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700888 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000889
890 // 4 packets lost: 3 media packets and FEC packet#1 lost.
brandtrece4aba2016-09-20 23:16:28 -0700891 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
892 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
893 this->fec_loss_mask_[1] = 1;
894 this->media_loss_mask_[0] = 1;
895 this->media_loss_mask_[2] = 1;
896 this->media_loss_mask_[3] = 1;
897 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000898
nissea5f043f2017-09-18 07:58:59 -0700899 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200900 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700901 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000902
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000903 // With media packet#3 and FEC packets #0, #1, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700904 EXPECT_TRUE(this->IsRecoveryComplete());
905 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000906
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000907 // 5 packets lost: 4 media packets and one FEC packet#2 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_[2] = 1;
911 this->media_loss_mask_[0] = 1;
912 this->media_loss_mask_[1] = 1;
913 this->media_loss_mask_[2] = 1;
914 this->media_loss_mask_[3] = 1;
915 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000916
nissea5f043f2017-09-18 07:58:59 -0700917 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200918 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700919 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000920
921 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700922 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000923}
924
brandtrece4aba2016-09-20 23:16:28 -0700925TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePackets) {
brandtrd90fa0b2016-08-09 06:57:14 -0700926 constexpr int kNumImportantPackets = 0;
927 constexpr bool kUseUnequalProtection = false;
928 constexpr int kNumMediaPackets = 5;
929 constexpr uint8_t kProtectionFactor = 60;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000930
brandtrece4aba2016-09-20 23:16:28 -0700931 this->media_packets_ =
932 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000933
934 // Create a new temporary packet list for generating FEC packets.
935 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700936 ForwardErrorCorrection::PacketList protected_media_packets;
937 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000938
brandtrece4aba2016-09-20 23:16:28 -0700939 EXPECT_EQ(
940 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
941 kNumImportantPackets, kUseUnequalProtection,
942 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000943
944 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700945 EXPECT_EQ(1u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000946
947 // 1 protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700948 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
949 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
950 this->media_loss_mask_[2] = 1;
951 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000952
nissea5f043f2017-09-18 07:58:59 -0700953 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200954 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700955 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000956
957 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700958 EXPECT_TRUE(this->IsRecoveryComplete());
959 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000960
961 // Unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700962 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
963 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
964 this->media_loss_mask_[1] = 1;
965 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000966
nissea5f043f2017-09-18 07:58:59 -0700967 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200968 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700969 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000970
971 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -0700972 EXPECT_FALSE(this->IsRecoveryComplete());
973 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000974
975 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700976 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
977 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
978 this->media_loss_mask_[0] = 1;
979 this->media_loss_mask_[2] = 1;
980 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000981
nissea5f043f2017-09-18 07:58:59 -0700982 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200983 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700984 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000985
986 // 2 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700987 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000988}
989
brandtrece4aba2016-09-20 23:16:28 -0700990TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsExtension) {
brandtrd90fa0b2016-08-09 06:57:14 -0700991 constexpr int kNumImportantPackets = 0;
992 constexpr bool kUseUnequalProtection = false;
993 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000994 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000995
brandtrece4aba2016-09-20 23:16:28 -0700996 this->media_packets_ =
997 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000998
999 // Create a new temporary packet list for generating FEC packets.
1000 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001001 ForwardErrorCorrection::PacketList protected_media_packets;
1002 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001003
1004 // Zero column insertion will have to extend the size of the packet
1005 // mask since the number of actual packets are 21, while the number
1006 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001007 EXPECT_EQ(
1008 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1009 kNumImportantPackets, kUseUnequalProtection,
1010 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001011
1012 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001013 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001014
1015 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001016 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1017 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1018 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1019 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001020
nissea5f043f2017-09-18 07:58:59 -07001021 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001022 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001023 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001024
1025 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001026 EXPECT_TRUE(this->IsRecoveryComplete());
1027 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001028
1029 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001030 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1031 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1032 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1033 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001034
nissea5f043f2017-09-18 07:58:59 -07001035 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001036 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001037 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001038
1039 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001040 EXPECT_FALSE(this->IsRecoveryComplete());
1041 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001042
1043 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001044 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1045 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1046 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1047 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1048 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1049 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1050 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1051 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1052 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001053
nissea5f043f2017-09-18 07:58:59 -07001054 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001055 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001056 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001057
1058 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001059 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001060}
1061
brandtrece4aba2016-09-20 23:16:28 -07001062TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
brandtrd90fa0b2016-08-09 06:57:14 -07001063 constexpr int kNumImportantPackets = 0;
1064 constexpr bool kUseUnequalProtection = false;
1065 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001066 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001067
brandtrece4aba2016-09-20 23:16:28 -07001068 this->media_packets_ = this->media_packet_generator_.ConstructMediaPackets(
1069 kNumMediaPackets, 0xFFFF - 5);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001070
1071 // Create a new temporary packet list for generating FEC packets.
1072 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001073 ForwardErrorCorrection::PacketList protected_media_packets;
1074 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001075
1076 // Zero column insertion will have to extend the size of the packet
1077 // mask since the number of actual packets are 21, while the number
1078 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001079 EXPECT_EQ(
1080 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1081 kNumImportantPackets, kUseUnequalProtection,
1082 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001083
1084 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001085 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001086
1087 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001088 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1089 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1090 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1091 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001092
nissea5f043f2017-09-18 07:58:59 -07001093 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001094 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001095 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001096
1097 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001098 EXPECT_TRUE(this->IsRecoveryComplete());
1099 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001100
1101 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001102 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1103 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1104 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1105 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001106
nissea5f043f2017-09-18 07:58:59 -07001107 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001108 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001109 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001110
1111 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001112 EXPECT_FALSE(this->IsRecoveryComplete());
1113 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001114
1115 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001116 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1117 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1118 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1119 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1120 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1121 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1122 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1123 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1124 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001125
nissea5f043f2017-09-18 07:58:59 -07001126 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001127 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001128 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001129
1130 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001131 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001132}
1133
brandtrece4aba2016-09-20 23:16:28 -07001134} // namespace webrtc