blob: eb559f2bd9f4155f216575891f9a1f091eee4904 [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
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000011#include <list>
brandtrece4aba2016-09-20 23:16:28 -070012#include <memory>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000013
Steve Anton91c26062019-03-28 10:56:11 -070014#include "absl/algorithm/container.h"
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();
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200123 received_packet->pkt->data = packet->data;
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200124 received_packet->is_fec = is_fec;
125 if (!is_fec) {
brandtrd726a3f2017-06-29 02:45:35 -0700126 received_packet->ssrc = kMediaSsrc;
127 // For media packets, the sequence number is obtained from the
128 // RTP header as written by MediaPacketGenerator::ConstructMediaPackets.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200129 received_packet->seq_num =
130 ByteReader<uint16_t>::ReadBigEndian(&packet->data[2]);
131 } else {
brandtrd726a3f2017-06-29 02:45:35 -0700132 received_packet->ssrc = ForwardErrorCorrectionType::kFecSsrc;
133 // For FEC packets, we simulate the sequence numbers differently
134 // depending on if ULPFEC or FlexFEC is used. See the definition of
135 // ForwardErrorCorrectionType::GetFirstFecSeqNum.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200136 received_packet->seq_num = fec_seq_num;
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200137 }
138 received_packets_.push_back(std::move(received_packet));
139 }
140 packet_idx++;
141 // Sequence number of FEC packets are defined as increment by 1 from
142 // last media packet in frame.
143 if (is_fec)
144 fec_seq_num++;
145 }
146}
147
148template <typename ForwardErrorCorrectionType>
149bool RtpFecTest<ForwardErrorCorrectionType>::IsRecoveryComplete() {
Steve Anton91c26062019-03-28 10:56:11 -0700150 // We must have equally many recovered packets as original packets and all
151 // recovered packets must be identical to the corresponding original packets.
152 return absl::c_equal(
153 media_packets_, recovered_packets_,
Yves Gerey665174f2018-06-19 15:03:05 +0200154 [](const std::unique_ptr<ForwardErrorCorrection::Packet>& media_packet,
155 const std::unique_ptr<ForwardErrorCorrection::RecoveredPacket>&
156 recovered_packet) {
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200157 if (media_packet->data.size() != recovered_packet->pkt->data.size()) {
Yves Gerey665174f2018-06-19 15:03:05 +0200158 return false;
159 }
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +0200160 if (memcmp(media_packet->data.cdata(),
161 recovered_packet->pkt->data.cdata(),
162 media_packet->data.size()) != 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200163 return false;
164 }
165 return true;
Steve Anton91c26062019-03-28 10:56:11 -0700166 });
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200167}
168
brandtrece4aba2016-09-20 23:16:28 -0700169// Define gTest typed test to loop over both ULPFEC and FlexFEC.
170// Since the tests now are parameterized, we need to access
171// member variables using |this|, thereby enforcing runtime
172// resolution.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200173
brandtr0496de22016-10-03 00:43:25 -0700174class FlexfecForwardErrorCorrection : public ForwardErrorCorrection {
175 public:
brandtrd726a3f2017-06-29 02:45:35 -0700176 static const uint32_t kFecSsrc = kFlexfecSsrc;
177
brandtr0496de22016-10-03 00:43:25 -0700178 FlexfecForwardErrorCorrection()
179 : ForwardErrorCorrection(
180 std::unique_ptr<FecHeaderReader>(new FlexfecHeaderReader()),
brandtrd726a3f2017-06-29 02:45:35 -0700181 std::unique_ptr<FecHeaderWriter>(new FlexfecHeaderWriter()),
182 kFecSsrc,
183 kMediaSsrc) {}
184
185 // For FlexFEC we let the FEC packet sequence numbers be independent of
186 // the media packet sequence numbers.
187 static uint16_t GetFirstFecSeqNum(uint16_t next_media_seq_num) {
188 Random random(0xbe110);
189 return random.Rand<uint16_t>();
190 }
brandtr0496de22016-10-03 00:43:25 -0700191};
192
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200193class UlpfecForwardErrorCorrection : public ForwardErrorCorrection {
194 public:
brandtrd726a3f2017-06-29 02:45:35 -0700195 static const uint32_t kFecSsrc = kMediaSsrc;
196
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200197 UlpfecForwardErrorCorrection()
198 : ForwardErrorCorrection(
199 std::unique_ptr<FecHeaderReader>(new UlpfecHeaderReader()),
brandtrd726a3f2017-06-29 02:45:35 -0700200 std::unique_ptr<FecHeaderWriter>(new UlpfecHeaderWriter()),
201 kFecSsrc,
202 kMediaSsrc) {}
203
204 // For ULPFEC we assume that the FEC packets are subsequent to the media
205 // packets in terms of sequence number.
206 static uint16_t GetFirstFecSeqNum(uint16_t next_media_seq_num) {
207 return next_media_seq_num;
208 }
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200209};
210
brandtr0496de22016-10-03 00:43:25 -0700211using FecTypes =
212 Types<FlexfecForwardErrorCorrection, UlpfecForwardErrorCorrection>;
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100213TYPED_TEST_SUITE(RtpFecTest, FecTypes);
brandtrece4aba2016-09-20 23:16:28 -0700214
Rasmus Brandtd73ba122017-12-07 10:22:49 +0100215TYPED_TEST(RtpFecTest, WillProtectMediaPacketsWithLargeSequenceNumberGap) {
216 constexpr int kNumImportantPackets = 0;
217 constexpr bool kUseUnequalProtection = false;
218 constexpr int kNumMediaPackets = 2;
219 constexpr uint8_t kProtectionFactor = 127;
220
221 this->media_packets_ =
222 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
223
224 // Create |kMaxMediaPackets - 1| sequence number difference.
225 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.front()->data[2],
226 1);
227 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.back()->data[2],
228 kMaxMediaPackets);
229
230 EXPECT_EQ(
231 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
232 kNumImportantPackets, kUseUnequalProtection,
233 kFecMaskBursty, &this->generated_fec_packets_));
234 EXPECT_EQ(1u, this->generated_fec_packets_.size());
235}
236
237TYPED_TEST(RtpFecTest,
238 WillNotProtectMediaPacketsWithTooLargeSequenceNumberGap) {
239 constexpr int kNumImportantPackets = 0;
240 constexpr bool kUseUnequalProtection = false;
241 constexpr int kNumMediaPackets = 2;
242 constexpr uint8_t kProtectionFactor = 127;
243
244 this->media_packets_ =
245 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
246
247 // Create |kMaxMediaPackets| sequence number difference.
248 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.front()->data[2],
249 1);
250 ByteWriter<uint16_t>::WriteBigEndian(&this->media_packets_.back()->data[2],
251 kMaxMediaPackets + 1);
252
253 EXPECT_EQ(
254 -1, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
255 kNumImportantPackets, kUseUnequalProtection,
256 kFecMaskBursty, &this->generated_fec_packets_));
257 EXPECT_TRUE(this->generated_fec_packets_.empty());
258}
259
brandtrece4aba2016-09-20 23:16:28 -0700260TYPED_TEST(RtpFecTest, FecRecoveryNoLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700261 constexpr int kNumImportantPackets = 0;
262 constexpr bool kUseUnequalProtection = false;
263 constexpr int kNumMediaPackets = 4;
264 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000265
brandtrece4aba2016-09-20 23:16:28 -0700266 this->media_packets_ =
267 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000268
brandtrece4aba2016-09-20 23:16:28 -0700269 EXPECT_EQ(
270 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
271 kNumImportantPackets, kUseUnequalProtection,
272 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000273
274 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700275 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000276
277 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700278 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
279 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
280 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000281
nissea5f043f2017-09-18 07:58:59 -0700282 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200283 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700284 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000285
286 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700287 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000288}
289
brandtrece4aba2016-09-20 23:16:28 -0700290TYPED_TEST(RtpFecTest, FecRecoveryWithLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700291 constexpr int kNumImportantPackets = 0;
292 constexpr bool kUseUnequalProtection = false;
293 constexpr int kNumMediaPackets = 4;
294 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000295
brandtrece4aba2016-09-20 23:16:28 -0700296 this->media_packets_ =
297 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000298
brandtrece4aba2016-09-20 23:16:28 -0700299 EXPECT_EQ(
300 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
301 kNumImportantPackets, kUseUnequalProtection,
302 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000303
304 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700305 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000306
307 // 1 media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700308 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
309 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
310 this->media_loss_mask_[3] = 1;
311 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000312
nissea5f043f2017-09-18 07:58:59 -0700313 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200314 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700315 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000316
317 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700318 EXPECT_TRUE(this->IsRecoveryComplete());
319 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000320
321 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700322 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
323 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
324 this->media_loss_mask_[1] = 1;
325 this->media_loss_mask_[3] = 1;
326 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000327
nissea5f043f2017-09-18 07:58:59 -0700328 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200329 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700330 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000331
332 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700333 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000334}
335
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000336// Verify that we don't use an old FEC packet for FEC decoding.
Niels Möller958288a2017-10-02 13:51:36 +0200337TYPED_TEST(RtpFecTest, NoFecRecoveryWithOldFecPacket) {
brandtrd90fa0b2016-08-09 06:57:14 -0700338 constexpr int kNumImportantPackets = 0;
339 constexpr bool kUseUnequalProtection = false;
340 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000341
342 // Two frames: first frame (old) with two media packets and 1 FEC packet.
Niels Möller958288a2017-10-02 13:51:36 +0200343 // Third frame (new) with 3 media packets, and no FEC packets.
344 //
345 // #0(media) #1(media) #2(FEC) ----Frame 1-----
346 // #32767(media) 32768(media) 32769(media) ----Frame 2-----
347 // #65535(media) #0(media) #1(media). ----Frame 3-----
348 // If we lose either packet 0 or 1 of third frame, FEC decoding should not
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000349 // try to decode using "old" FEC packet #2.
350
351 // Construct media packets for first frame, starting at sequence number 0.
brandtrece4aba2016-09-20 23:16:28 -0700352 this->media_packets_ =
353 this->media_packet_generator_.ConstructMediaPackets(2, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000354
brandtrece4aba2016-09-20 23:16:28 -0700355 EXPECT_EQ(
356 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
357 kNumImportantPackets, kUseUnequalProtection,
358 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000359 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700360 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000361 // Add FEC packet (seq#2) of this first frame to received list (i.e., assume
362 // the two media packet were lost).
brandtrece4aba2016-09-20 23:16:28 -0700363 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
364 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
365 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000366
367 // Construct media packets for second frame, with sequence number wrap.
brandtrece4aba2016-09-20 23:16:28 -0700368 this->media_packets_ =
Niels Möller958288a2017-10-02 13:51:36 +0200369 this->media_packet_generator_.ConstructMediaPackets(3, 32767);
370
371 // Expect 3 media packets for this frame.
372 EXPECT_EQ(3u, this->media_packets_.size());
373
374 // No packets lost
375 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
376 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
377
378 // Construct media packets for third frame, with sequence number wrap.
379 this->media_packets_ =
brandtrece4aba2016-09-20 23:16:28 -0700380 this->media_packet_generator_.ConstructMediaPackets(3, 65535);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000381
382 // Expect 3 media packets for this frame.
brandtrece4aba2016-09-20 23:16:28 -0700383 EXPECT_EQ(3u, this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000384
385 // Second media packet lost (seq#0).
brandtrece4aba2016-09-20 23:16:28 -0700386 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
387 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000388 // Add packets #65535, and #1 to received list.
brandtrece4aba2016-09-20 23:16:28 -0700389 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000390
nissea5f043f2017-09-18 07:58:59 -0700391 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200392 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700393 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000394
Niels Möller958288a2017-10-02 13:51:36 +0200395 // Expect that no decoding is done to get missing packet (seq#0) of third
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000396 // frame, using old FEC packet (seq#2) from first (old) frame. So number of
Niels Möller958288a2017-10-02 13:51:36 +0200397 // recovered packets is 5 (0 from first frame, three from second frame, and 2
398 // for the third frame, with no packets recovered via FEC).
399 EXPECT_EQ(5u, this->recovered_packets_.size());
brandtrece4aba2016-09-20 23:16:28 -0700400 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000401}
402
brandtrd90fa0b2016-08-09 06:57:14 -0700403// Verify we can still recover frame if sequence number wrap occurs within
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000404// the frame and FEC packet following wrap is received after media packets.
brandtrece4aba2016-09-20 23:16:28 -0700405TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700406 constexpr int kNumImportantPackets = 0;
407 constexpr bool kUseUnequalProtection = false;
408 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000409
410 // One frame, with sequence number wrap in media packets.
411 // -----Frame 1----
412 // #65534(media) #65535(media) #0(media) #1(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700413 this->media_packets_ =
414 this->media_packet_generator_.ConstructMediaPackets(3, 65534);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000415
brandtrece4aba2016-09-20 23:16:28 -0700416 EXPECT_EQ(
417 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
418 kNumImportantPackets, kUseUnequalProtection,
419 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000420
421 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700422 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000423
424 // Lose one media packet (seq# 65535).
brandtrece4aba2016-09-20 23:16:28 -0700425 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
426 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
427 this->media_loss_mask_[1] = 1;
428 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000429 // Add FEC packet to received list following the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700430 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
431 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000432
nissea5f043f2017-09-18 07:58:59 -0700433 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200434 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700435 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000436
437 // Expect 3 media packets in recovered list, and complete recovery.
438 // Wrap-around won't remove FEC packet, as it follows the wrap.
brandtrece4aba2016-09-20 23:16:28 -0700439 EXPECT_EQ(3u, this->recovered_packets_.size());
440 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000441}
442
brandtrd726a3f2017-06-29 02:45:35 -0700443// Sequence number wrap occurs within the ULPFEC packets for the frame.
brandtrd726a3f2017-06-29 02:45:35 -0700444// Same problem will occur if wrap is within media packets but ULPFEC packet is
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000445// received before the media packets. This may be improved if timing information
brandtrd726a3f2017-06-29 02:45:35 -0700446// is used to detect old ULPFEC packets.
nissea5f043f2017-09-18 07:58:59 -0700447
448// TODO(nisse): There's some logic to discard ULPFEC packets at wrap-around,
449// however, that is not actually exercised by this test: When the first FEC
450// packet is processed, it results in full recovery of one media packet and the
451// FEC packet is forgotten. And then the wraparound isn't noticed when the next
452// FEC packet is received. We should fix wraparound handling, which currently
453// appears broken, and then figure out how to test it properly.
brandtrd726a3f2017-06-29 02:45:35 -0700454using RtpFecTestUlpfecOnly = RtpFecTest<UlpfecForwardErrorCorrection>;
nissea5f043f2017-09-18 07:58:59 -0700455TEST_F(RtpFecTestUlpfecOnly, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700456 constexpr int kNumImportantPackets = 0;
457 constexpr bool kUseUnequalProtection = false;
458 constexpr uint8_t kProtectionFactor = 200;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000459
460 // 1 frame: 3 media packets and 2 FEC packets.
461 // Sequence number wrap in FEC packets.
462 // -----Frame 1----
463 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700464 this->media_packets_ =
465 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000466
brandtrece4aba2016-09-20 23:16:28 -0700467 EXPECT_EQ(
468 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
469 kNumImportantPackets, kUseUnequalProtection,
470 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000471
472 // Expect 2 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700473 EXPECT_EQ(2u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000474
475 // Lose the last two media packets (seq# 65533, 65534).
brandtrece4aba2016-09-20 23:16:28 -0700476 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
477 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
478 this->media_loss_mask_[1] = 1;
479 this->media_loss_mask_[2] = 1;
480 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
481 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
482 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000483
nissea5f043f2017-09-18 07:58:59 -0700484 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200485 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700486 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000487
488 // The two FEC packets are received and should allow for complete recovery,
brandtrd726a3f2017-06-29 02:45:35 -0700489 // but because of the wrap the first FEC packet will be discarded, and only
490 // one media packet is recoverable. So expect 2 media packets on recovered
491 // list and no complete recovery.
nissea5f043f2017-09-18 07:58:59 -0700492 EXPECT_EQ(3u, this->recovered_packets_.size());
493 EXPECT_EQ(this->recovered_packets_.size(), this->media_packets_.size());
494 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd726a3f2017-06-29 02:45:35 -0700495}
496
497// TODO(brandtr): This test mimics the one above, ensuring that the recovery
498// strategy of FlexFEC matches the recovery strategy of ULPFEC. Since FlexFEC
499// does not share the sequence number space with the media, however, having a
500// matching recovery strategy may be suboptimal. Study this further.
nissea5f043f2017-09-18 07:58:59 -0700501// TODO(nisse): In this test, recovery based on the first FEC packet fails with
502// the log message "The recovered packet had a length larger than a typical IP
503// packet, and is thus dropped." This is probably not intended, and needs
504// investigation.
brandtrd726a3f2017-06-29 02:45:35 -0700505using RtpFecTestFlexfecOnly = RtpFecTest<FlexfecForwardErrorCorrection>;
506TEST_F(RtpFecTestFlexfecOnly, FecRecoveryWithSeqNumGapOneFrameNoRecovery) {
507 constexpr int kNumImportantPackets = 0;
508 constexpr bool kUseUnequalProtection = false;
509 constexpr uint8_t kProtectionFactor = 200;
510
511 // 1 frame: 3 media packets and 2 FEC packets.
512 // Sequence number wrap in FEC packets.
513 // -----Frame 1----
514 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
515 this->media_packets_ =
516 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
517
518 EXPECT_EQ(
519 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
520 kNumImportantPackets, kUseUnequalProtection,
521 kFecMaskBursty, &this->generated_fec_packets_));
522
523 // Expect 2 FEC packets.
524 EXPECT_EQ(2u, this->generated_fec_packets_.size());
525
526 // Overwrite the sequence numbers generated by ConstructMediaPackets,
527 // to make sure that we do have a wrap.
528 auto it = this->generated_fec_packets_.begin();
529 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 65535);
530 ++it;
531 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 0);
532
533 // Lose the last two media packets (seq# 65533, 65534).
534 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
535 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
536 this->media_loss_mask_[1] = 1;
537 this->media_loss_mask_[2] = 1;
538 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
539 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
540 true);
541
nissea5f043f2017-09-18 07:58:59 -0700542 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200543 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700544 }
brandtrd726a3f2017-06-29 02:45:35 -0700545
546 // The two FEC packets are received and should allow for complete recovery,
547 // but because of the wrap the first FEC packet will be discarded, and only
548 // one media packet is recoverable. So expect 2 media packets on recovered
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000549 // list and no complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700550 EXPECT_EQ(2u, this->recovered_packets_.size());
551 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
552 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000553}
554
brandtrd90fa0b2016-08-09 06:57:14 -0700555// Verify we can still recover frame if media packets are reordered.
brandtrece4aba2016-09-20 23:16:28 -0700556TYPED_TEST(RtpFecTest, FecRecoveryWithMediaOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700557 constexpr int kNumImportantPackets = 0;
558 constexpr bool kUseUnequalProtection = false;
559 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000560
561 // One frame: 3 media packets, 1 FEC packet.
562 // -----Frame 1----
563 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700564 this->media_packets_ =
565 this->media_packet_generator_.ConstructMediaPackets(3, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000566
brandtrece4aba2016-09-20 23:16:28 -0700567 EXPECT_EQ(
568 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
569 kNumImportantPackets, kUseUnequalProtection,
570 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000571
572 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700573 EXPECT_EQ(1u, this->generated_fec_packets_.size());
brandtrd90fa0b2016-08-09 06:57:14 -0700574
575 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700576 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
577 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
578 this->media_loss_mask_[1] = 1;
579 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
brandtrd90fa0b2016-08-09 06:57:14 -0700580
581 // Reorder received media packets.
brandtrece4aba2016-09-20 23:16:28 -0700582 auto it0 = this->received_packets_.begin();
brandtrd726a3f2017-06-29 02:45:35 -0700583 auto it1 = this->received_packets_.begin();
584 it1++;
585 std::swap(*it0, *it1);
brandtrd90fa0b2016-08-09 06:57:14 -0700586
nissea5f043f2017-09-18 07:58:59 -0700587 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200588 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700589 }
brandtrd90fa0b2016-08-09 06:57:14 -0700590
591 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700592 EXPECT_EQ(3u, this->recovered_packets_.size());
593 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd90fa0b2016-08-09 06:57:14 -0700594}
595
596// Verify we can still recover frame if FEC is received before media packets.
brandtrece4aba2016-09-20 23:16:28 -0700597TYPED_TEST(RtpFecTest, FecRecoveryWithFecOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700598 constexpr int kNumImportantPackets = 0;
599 constexpr bool kUseUnequalProtection = false;
600 constexpr uint8_t kProtectionFactor = 20;
601
602 // One frame: 3 media packets, 1 FEC packet.
603 // -----Frame 1----
604 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700605 this->media_packets_ =
606 this->media_packet_generator_.ConstructMediaPackets(3, 0);
brandtrd90fa0b2016-08-09 06:57:14 -0700607
brandtrece4aba2016-09-20 23:16:28 -0700608 EXPECT_EQ(
609 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
610 kNumImportantPackets, kUseUnequalProtection,
611 kFecMaskBursty, &this->generated_fec_packets_));
brandtrd90fa0b2016-08-09 06:57:14 -0700612
613 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700614 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000615
616 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700617 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
618 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
619 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000620 // Add FEC packet to received list before the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700621 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
622 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000623 // Add media packets to received list.
brandtrece4aba2016-09-20 23:16:28 -0700624 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000625
nissea5f043f2017-09-18 07:58:59 -0700626 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200627 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700628 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000629
630 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700631 EXPECT_EQ(3u, this->recovered_packets_.size());
632 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000633}
634
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000635// Test 50% protection with random mask type: Two cases are considered:
636// a 50% non-consecutive loss which can be fully recovered, and a 50%
637// consecutive loss which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700638TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700639 constexpr int kNumImportantPackets = 0;
640 constexpr bool kUseUnequalProtection = false;
641 constexpr int kNumMediaPackets = 4;
642 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000643
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000644 // Packet Mask for (4,4,0) code, from random mask table.
645 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000646
647 // media#0 media#1 media#2 media#3
648 // fec#0: 1 1 0 0
649 // fec#1: 1 0 1 0
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000650 // fec#2: 0 0 1 1
651 // fec#3: 0 1 0 1
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000652 //
653
brandtrece4aba2016-09-20 23:16:28 -0700654 this->media_packets_ =
655 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000656
brandtrece4aba2016-09-20 23:16:28 -0700657 EXPECT_EQ(
658 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
659 kNumImportantPackets, kUseUnequalProtection,
660 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000661
662 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700663 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000664
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000665 // 4 packets lost: 3 media packets (0, 2, 3), and one FEC packet (0) lost.
brandtrece4aba2016-09-20 23:16:28 -0700666 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
667 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
668 this->fec_loss_mask_[0] = 1;
669 this->media_loss_mask_[0] = 1;
670 this->media_loss_mask_[2] = 1;
671 this->media_loss_mask_[3] = 1;
672 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000673
nissea5f043f2017-09-18 07:58:59 -0700674 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200675 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700676 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000677
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000678 // With media packet#1 and FEC packets #1, #2, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700679 EXPECT_TRUE(this->IsRecoveryComplete());
680 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000681
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000682 // 4 consecutive packets lost: media packets 0, 1, 2, 3.
brandtrece4aba2016-09-20 23:16:28 -0700683 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
684 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
685 this->media_loss_mask_[0] = 1;
686 this->media_loss_mask_[1] = 1;
687 this->media_loss_mask_[2] = 1;
688 this->media_loss_mask_[3] = 1;
689 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000690
nissea5f043f2017-09-18 07:58:59 -0700691 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200692 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700693 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000694
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000695 // Cannot get complete recovery for this loss configuration with random mask.
brandtrece4aba2016-09-20 23:16:28 -0700696 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000697}
698
699// Test 50% protection with bursty type: Three cases are considered:
700// two 50% consecutive losses which can be fully recovered, and one
701// non-consecutive which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700702TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percBurstyMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700703 constexpr int kNumImportantPackets = 0;
704 constexpr bool kUseUnequalProtection = false;
705 constexpr int kNumMediaPackets = 4;
706 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000707
708 // Packet Mask for (4,4,0) code, from bursty mask table.
709 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
710
711 // media#0 media#1 media#2 media#3
712 // fec#0: 1 0 0 0
713 // fec#1: 1 1 0 0
714 // fec#2: 0 1 1 0
715 // fec#3: 0 0 1 1
716 //
717
brandtrece4aba2016-09-20 23:16:28 -0700718 this->media_packets_ =
719 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000720
brandtrece4aba2016-09-20 23:16:28 -0700721 EXPECT_EQ(
722 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
723 kNumImportantPackets, kUseUnequalProtection,
724 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000725
726 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700727 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000728
729 // 4 consecutive packets lost: media packets 0,1,2,3.
brandtrece4aba2016-09-20 23:16:28 -0700730 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
731 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
732 this->media_loss_mask_[0] = 1;
733 this->media_loss_mask_[1] = 1;
734 this->media_loss_mask_[2] = 1;
735 this->media_loss_mask_[3] = 1;
736 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000737
nissea5f043f2017-09-18 07:58:59 -0700738 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200739 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700740 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000741
742 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700743 EXPECT_TRUE(this->IsRecoveryComplete());
744 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000745
746 // 4 consecutive packets lost: media packets 1,2, 3, and FEC packet 0.
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->fec_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_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200756 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700757 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000758
759 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700760 EXPECT_TRUE(this->IsRecoveryComplete());
761 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000762
763 // 4 packets lost (non-consecutive loss): media packets 0, 3, and FEC# 0, 3.
brandtrece4aba2016-09-20 23:16:28 -0700764 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
765 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
766 this->fec_loss_mask_[0] = 1;
767 this->fec_loss_mask_[3] = 1;
768 this->media_loss_mask_[0] = 1;
769 this->media_loss_mask_[3] = 1;
770 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000771
nissea5f043f2017-09-18 07:58:59 -0700772 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200773 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700774 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000775
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000776 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700777 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000778}
779
brandtrece4aba2016-09-20 23:16:28 -0700780TYPED_TEST(RtpFecTest, FecRecoveryNoLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700781 constexpr int kNumImportantPackets = 2;
782 constexpr bool kUseUnequalProtection = true;
783 constexpr int kNumMediaPackets = 4;
784 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000785
brandtrece4aba2016-09-20 23:16:28 -0700786 this->media_packets_ =
787 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000788
brandtrece4aba2016-09-20 23:16:28 -0700789 EXPECT_EQ(
790 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
791 kNumImportantPackets, kUseUnequalProtection,
792 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000793
794 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700795 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000796
797 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700798 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
799 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
800 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000801
nissea5f043f2017-09-18 07:58:59 -0700802 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200803 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700804 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000805
806 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700807 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000808}
809
brandtrece4aba2016-09-20 23:16:28 -0700810TYPED_TEST(RtpFecTest, FecRecoveryWithLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700811 constexpr int kNumImportantPackets = 2;
812 constexpr bool kUseUnequalProtection = true;
813 constexpr int kNumMediaPackets = 4;
814 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000815
brandtrece4aba2016-09-20 23:16:28 -0700816 this->media_packets_ =
817 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000818
brandtrece4aba2016-09-20 23:16:28 -0700819 EXPECT_EQ(
820 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
821 kNumImportantPackets, kUseUnequalProtection,
822 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000823
824 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700825 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000826
827 // 1 media packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700828 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
829 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
830 this->media_loss_mask_[3] = 1;
831 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000832
nissea5f043f2017-09-18 07:58:59 -0700833 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200834 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700835 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000836
837 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700838 EXPECT_TRUE(this->IsRecoveryComplete());
839 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000840
841 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700842 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
843 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
844 this->media_loss_mask_[1] = 1;
845 this->media_loss_mask_[3] = 1;
846 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000847
nissea5f043f2017-09-18 07:58:59 -0700848 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200849 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700850 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000851
852 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700853 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000854}
855
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000856// Test 50% protection with random mask type for UEP on.
brandtrece4aba2016-09-20 23:16:28 -0700857TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percUepRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700858 constexpr int kNumImportantPackets = 1;
859 constexpr bool kUseUnequalProtection = true;
860 constexpr int kNumMediaPackets = 4;
861 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000862
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000863 // Packet Mask for (4,4,1) code, from random mask table.
864 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 1)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000865
866 // media#0 media#1 media#2 media#3
867 // fec#0: 1 0 0 0
868 // fec#1: 1 1 0 0
869 // fec#2: 1 0 1 1
870 // fec#3: 0 1 1 0
871 //
872
brandtrece4aba2016-09-20 23:16:28 -0700873 this->media_packets_ =
874 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000875
brandtrece4aba2016-09-20 23:16:28 -0700876 EXPECT_EQ(
877 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
878 kNumImportantPackets, kUseUnequalProtection,
879 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000880
881 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700882 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000883
884 // 4 packets lost: 3 media packets and FEC packet#1 lost.
brandtrece4aba2016-09-20 23:16:28 -0700885 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
886 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
887 this->fec_loss_mask_[1] = 1;
888 this->media_loss_mask_[0] = 1;
889 this->media_loss_mask_[2] = 1;
890 this->media_loss_mask_[3] = 1;
891 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000892
nissea5f043f2017-09-18 07:58:59 -0700893 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200894 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700895 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000896
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000897 // With media packet#3 and FEC packets #0, #1, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700898 EXPECT_TRUE(this->IsRecoveryComplete());
899 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000900
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000901 // 5 packets lost: 4 media packets and one FEC packet#2 lost.
brandtrece4aba2016-09-20 23:16:28 -0700902 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
903 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
904 this->fec_loss_mask_[2] = 1;
905 this->media_loss_mask_[0] = 1;
906 this->media_loss_mask_[1] = 1;
907 this->media_loss_mask_[2] = 1;
908 this->media_loss_mask_[3] = 1;
909 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000910
nissea5f043f2017-09-18 07:58:59 -0700911 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200912 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700913 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000914
915 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700916 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000917}
918
brandtrece4aba2016-09-20 23:16:28 -0700919TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePackets) {
brandtrd90fa0b2016-08-09 06:57:14 -0700920 constexpr int kNumImportantPackets = 0;
921 constexpr bool kUseUnequalProtection = false;
922 constexpr int kNumMediaPackets = 5;
923 constexpr uint8_t kProtectionFactor = 60;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000924
brandtrece4aba2016-09-20 23:16:28 -0700925 this->media_packets_ =
926 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000927
928 // Create a new temporary packet list for generating FEC packets.
929 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700930 ForwardErrorCorrection::PacketList protected_media_packets;
931 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000932
brandtrece4aba2016-09-20 23:16:28 -0700933 EXPECT_EQ(
934 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
935 kNumImportantPackets, kUseUnequalProtection,
936 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000937
938 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700939 EXPECT_EQ(1u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000940
941 // 1 protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700942 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
943 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
944 this->media_loss_mask_[2] = 1;
945 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000946
nissea5f043f2017-09-18 07:58:59 -0700947 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200948 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700949 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000950
951 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700952 EXPECT_TRUE(this->IsRecoveryComplete());
953 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000954
955 // Unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700956 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
957 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
958 this->media_loss_mask_[1] = 1;
959 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000960
nissea5f043f2017-09-18 07:58:59 -0700961 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200962 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700963 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000964
965 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -0700966 EXPECT_FALSE(this->IsRecoveryComplete());
967 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000968
969 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700970 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
971 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
972 this->media_loss_mask_[0] = 1;
973 this->media_loss_mask_[2] = 1;
974 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000975
nissea5f043f2017-09-18 07:58:59 -0700976 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200977 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700978 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000979
980 // 2 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700981 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000982}
983
brandtrece4aba2016-09-20 23:16:28 -0700984TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsExtension) {
brandtrd90fa0b2016-08-09 06:57:14 -0700985 constexpr int kNumImportantPackets = 0;
986 constexpr bool kUseUnequalProtection = false;
987 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000988 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000989
brandtrece4aba2016-09-20 23:16:28 -0700990 this->media_packets_ =
991 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000992
993 // Create a new temporary packet list for generating FEC packets.
994 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700995 ForwardErrorCorrection::PacketList protected_media_packets;
996 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000997
998 // Zero column insertion will have to extend the size of the packet
999 // mask since the number of actual packets are 21, while the number
1000 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001001 EXPECT_EQ(
1002 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1003 kNumImportantPackets, kUseUnequalProtection,
1004 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001005
1006 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001007 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001008
1009 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001010 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1011 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1012 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1013 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001014
nissea5f043f2017-09-18 07:58:59 -07001015 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001016 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001017 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001018
1019 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001020 EXPECT_TRUE(this->IsRecoveryComplete());
1021 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001022
1023 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001024 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1025 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1026 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1027 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001028
nissea5f043f2017-09-18 07:58:59 -07001029 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001030 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001031 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001032
1033 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001034 EXPECT_FALSE(this->IsRecoveryComplete());
1035 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001036
1037 // 6 media packets 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 - 11] = 1;
1041 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1042 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1043 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1044 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1045 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1046 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001047
nissea5f043f2017-09-18 07:58:59 -07001048 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001049 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001050 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001051
1052 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001053 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001054}
1055
brandtrece4aba2016-09-20 23:16:28 -07001056TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
brandtrd90fa0b2016-08-09 06:57:14 -07001057 constexpr int kNumImportantPackets = 0;
1058 constexpr bool kUseUnequalProtection = false;
1059 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001060 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001061
brandtrece4aba2016-09-20 23:16:28 -07001062 this->media_packets_ = this->media_packet_generator_.ConstructMediaPackets(
1063 kNumMediaPackets, 0xFFFF - 5);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001064
1065 // Create a new temporary packet list for generating FEC packets.
1066 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001067 ForwardErrorCorrection::PacketList protected_media_packets;
1068 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001069
1070 // Zero column insertion will have to extend the size of the packet
1071 // mask since the number of actual packets are 21, while the number
1072 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001073 EXPECT_EQ(
1074 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1075 kNumImportantPackets, kUseUnequalProtection,
1076 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001077
1078 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001079 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001080
1081 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001082 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1083 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1084 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1085 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001086
nissea5f043f2017-09-18 07:58:59 -07001087 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001088 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001089 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001090
1091 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001092 EXPECT_TRUE(this->IsRecoveryComplete());
1093 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001094
1095 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001096 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1097 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1098 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1099 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001100
nissea5f043f2017-09-18 07:58:59 -07001101 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001102 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001103 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001104
1105 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001106 EXPECT_FALSE(this->IsRecoveryComplete());
1107 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001108
1109 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001110 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1111 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1112 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1113 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1114 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1115 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1116 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1117 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1118 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001119
nissea5f043f2017-09-18 07:58:59 -07001120 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001121 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001122 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001123
1124 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001125 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001126}
1127
brandtrece4aba2016-09-20 23:16:28 -07001128} // namespace webrtc