blob: a90e61a7318eff58502bb511b253a0e6e7a9d20c [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 =
Danil Chapovalove15dc582021-01-07 15:24:05 +0100130 ByteReader<uint16_t>::ReadBigEndian(packet->data.data() + 2);
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200131 } 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.
Danil Chapovalove15dc582021-01-07 15:24:05 +0100225 ByteWriter<uint16_t>::WriteBigEndian(
226 this->media_packets_.front()->data.MutableData() + 2, 1);
227 ByteWriter<uint16_t>::WriteBigEndian(
228 this->media_packets_.back()->data.MutableData() + 2, kMaxMediaPackets);
Rasmus Brandtd73ba122017-12-07 10:22:49 +0100229
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.
Danil Chapovalove15dc582021-01-07 15:24:05 +0100248 ByteWriter<uint16_t>::WriteBigEndian(
249 this->media_packets_.front()->data.MutableData() + 2, 1);
250 ByteWriter<uint16_t>::WriteBigEndian(
251 this->media_packets_.back()->data.MutableData() + 2,
252 kMaxMediaPackets + 1);
Rasmus Brandtd73ba122017-12-07 10:22:49 +0100253
254 EXPECT_EQ(
255 -1, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
256 kNumImportantPackets, kUseUnequalProtection,
257 kFecMaskBursty, &this->generated_fec_packets_));
258 EXPECT_TRUE(this->generated_fec_packets_.empty());
259}
260
brandtrece4aba2016-09-20 23:16:28 -0700261TYPED_TEST(RtpFecTest, FecRecoveryNoLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700262 constexpr int kNumImportantPackets = 0;
263 constexpr bool kUseUnequalProtection = false;
264 constexpr int kNumMediaPackets = 4;
265 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000266
brandtrece4aba2016-09-20 23:16:28 -0700267 this->media_packets_ =
268 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000269
brandtrece4aba2016-09-20 23:16:28 -0700270 EXPECT_EQ(
271 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
272 kNumImportantPackets, kUseUnequalProtection,
273 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000274
275 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700276 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000277
278 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700279 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
280 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
281 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000282
nissea5f043f2017-09-18 07:58:59 -0700283 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200284 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700285 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000286
287 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700288 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000289}
290
brandtrece4aba2016-09-20 23:16:28 -0700291TYPED_TEST(RtpFecTest, FecRecoveryWithLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700292 constexpr int kNumImportantPackets = 0;
293 constexpr bool kUseUnequalProtection = false;
294 constexpr int kNumMediaPackets = 4;
295 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000296
brandtrece4aba2016-09-20 23:16:28 -0700297 this->media_packets_ =
298 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000299
brandtrece4aba2016-09-20 23:16:28 -0700300 EXPECT_EQ(
301 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
302 kNumImportantPackets, kUseUnequalProtection,
303 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000304
305 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700306 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000307
308 // 1 media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700309 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
310 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
311 this->media_loss_mask_[3] = 1;
312 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000313
nissea5f043f2017-09-18 07:58:59 -0700314 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200315 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700316 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000317
318 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700319 EXPECT_TRUE(this->IsRecoveryComplete());
320 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000321
322 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700323 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
324 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
325 this->media_loss_mask_[1] = 1;
326 this->media_loss_mask_[3] = 1;
327 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000328
nissea5f043f2017-09-18 07:58:59 -0700329 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200330 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700331 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000332
333 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700334 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000335}
336
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000337// Verify that we don't use an old FEC packet for FEC decoding.
Niels Möller958288a2017-10-02 13:51:36 +0200338TYPED_TEST(RtpFecTest, NoFecRecoveryWithOldFecPacket) {
brandtrd90fa0b2016-08-09 06:57:14 -0700339 constexpr int kNumImportantPackets = 0;
340 constexpr bool kUseUnequalProtection = false;
341 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000342
343 // Two frames: first frame (old) with two media packets and 1 FEC packet.
Niels Möller958288a2017-10-02 13:51:36 +0200344 // Third frame (new) with 3 media packets, and no FEC packets.
345 //
346 // #0(media) #1(media) #2(FEC) ----Frame 1-----
347 // #32767(media) 32768(media) 32769(media) ----Frame 2-----
348 // #65535(media) #0(media) #1(media). ----Frame 3-----
349 // If we lose either packet 0 or 1 of third frame, FEC decoding should not
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000350 // try to decode using "old" FEC packet #2.
351
352 // Construct media packets for first frame, starting at sequence number 0.
brandtrece4aba2016-09-20 23:16:28 -0700353 this->media_packets_ =
354 this->media_packet_generator_.ConstructMediaPackets(2, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000355
brandtrece4aba2016-09-20 23:16:28 -0700356 EXPECT_EQ(
357 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
358 kNumImportantPackets, kUseUnequalProtection,
359 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000360 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700361 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000362 // Add FEC packet (seq#2) of this first frame to received list (i.e., assume
363 // the two media packet were lost).
brandtrece4aba2016-09-20 23:16:28 -0700364 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
365 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
366 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000367
368 // Construct media packets for second frame, with sequence number wrap.
brandtrece4aba2016-09-20 23:16:28 -0700369 this->media_packets_ =
Niels Möller958288a2017-10-02 13:51:36 +0200370 this->media_packet_generator_.ConstructMediaPackets(3, 32767);
371
372 // Expect 3 media packets for this frame.
373 EXPECT_EQ(3u, this->media_packets_.size());
374
375 // No packets lost
376 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
377 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
378
379 // Construct media packets for third frame, with sequence number wrap.
380 this->media_packets_ =
brandtrece4aba2016-09-20 23:16:28 -0700381 this->media_packet_generator_.ConstructMediaPackets(3, 65535);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000382
383 // Expect 3 media packets for this frame.
brandtrece4aba2016-09-20 23:16:28 -0700384 EXPECT_EQ(3u, this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000385
386 // Second media packet lost (seq#0).
brandtrece4aba2016-09-20 23:16:28 -0700387 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
388 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000389 // Add packets #65535, and #1 to received list.
brandtrece4aba2016-09-20 23:16:28 -0700390 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000391
nissea5f043f2017-09-18 07:58:59 -0700392 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200393 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700394 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000395
Niels Möller958288a2017-10-02 13:51:36 +0200396 // Expect that no decoding is done to get missing packet (seq#0) of third
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000397 // frame, using old FEC packet (seq#2) from first (old) frame. So number of
Niels Möller958288a2017-10-02 13:51:36 +0200398 // recovered packets is 5 (0 from first frame, three from second frame, and 2
399 // for the third frame, with no packets recovered via FEC).
400 EXPECT_EQ(5u, this->recovered_packets_.size());
brandtrece4aba2016-09-20 23:16:28 -0700401 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000402}
403
brandtrd90fa0b2016-08-09 06:57:14 -0700404// Verify we can still recover frame if sequence number wrap occurs within
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000405// the frame and FEC packet following wrap is received after media packets.
brandtrece4aba2016-09-20 23:16:28 -0700406TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700407 constexpr int kNumImportantPackets = 0;
408 constexpr bool kUseUnequalProtection = false;
409 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000410
411 // One frame, with sequence number wrap in media packets.
412 // -----Frame 1----
413 // #65534(media) #65535(media) #0(media) #1(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700414 this->media_packets_ =
415 this->media_packet_generator_.ConstructMediaPackets(3, 65534);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000416
brandtrece4aba2016-09-20 23:16:28 -0700417 EXPECT_EQ(
418 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
419 kNumImportantPackets, kUseUnequalProtection,
420 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000421
422 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700423 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000424
425 // Lose one media packet (seq# 65535).
brandtrece4aba2016-09-20 23:16:28 -0700426 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
427 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
428 this->media_loss_mask_[1] = 1;
429 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000430 // Add FEC packet to received list following the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700431 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
432 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000433
nissea5f043f2017-09-18 07:58:59 -0700434 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200435 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700436 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000437
438 // Expect 3 media packets in recovered list, and complete recovery.
439 // Wrap-around won't remove FEC packet, as it follows the wrap.
brandtrece4aba2016-09-20 23:16:28 -0700440 EXPECT_EQ(3u, this->recovered_packets_.size());
441 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000442}
443
brandtrd726a3f2017-06-29 02:45:35 -0700444// Sequence number wrap occurs within the ULPFEC packets for the frame.
brandtrd726a3f2017-06-29 02:45:35 -0700445// Same problem will occur if wrap is within media packets but ULPFEC packet is
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000446// received before the media packets. This may be improved if timing information
brandtrd726a3f2017-06-29 02:45:35 -0700447// is used to detect old ULPFEC packets.
nissea5f043f2017-09-18 07:58:59 -0700448
449// TODO(nisse): There's some logic to discard ULPFEC packets at wrap-around,
450// however, that is not actually exercised by this test: When the first FEC
451// packet is processed, it results in full recovery of one media packet and the
452// FEC packet is forgotten. And then the wraparound isn't noticed when the next
453// FEC packet is received. We should fix wraparound handling, which currently
454// appears broken, and then figure out how to test it properly.
brandtrd726a3f2017-06-29 02:45:35 -0700455using RtpFecTestUlpfecOnly = RtpFecTest<UlpfecForwardErrorCorrection>;
nissea5f043f2017-09-18 07:58:59 -0700456TEST_F(RtpFecTestUlpfecOnly, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700457 constexpr int kNumImportantPackets = 0;
458 constexpr bool kUseUnequalProtection = false;
459 constexpr uint8_t kProtectionFactor = 200;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000460
461 // 1 frame: 3 media packets and 2 FEC packets.
462 // Sequence number wrap in FEC packets.
463 // -----Frame 1----
464 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700465 this->media_packets_ =
466 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000467
brandtrece4aba2016-09-20 23:16:28 -0700468 EXPECT_EQ(
469 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
470 kNumImportantPackets, kUseUnequalProtection,
471 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000472
473 // Expect 2 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700474 EXPECT_EQ(2u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000475
476 // Lose the last two media packets (seq# 65533, 65534).
brandtrece4aba2016-09-20 23:16:28 -0700477 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
478 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
479 this->media_loss_mask_[1] = 1;
480 this->media_loss_mask_[2] = 1;
481 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
482 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
483 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000484
nissea5f043f2017-09-18 07:58:59 -0700485 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200486 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700487 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000488
489 // The two FEC packets are received and should allow for complete recovery,
brandtrd726a3f2017-06-29 02:45:35 -0700490 // but because of the wrap the first FEC packet will be discarded, and only
491 // one media packet is recoverable. So expect 2 media packets on recovered
492 // list and no complete recovery.
nissea5f043f2017-09-18 07:58:59 -0700493 EXPECT_EQ(3u, this->recovered_packets_.size());
494 EXPECT_EQ(this->recovered_packets_.size(), this->media_packets_.size());
495 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd726a3f2017-06-29 02:45:35 -0700496}
497
498// TODO(brandtr): This test mimics the one above, ensuring that the recovery
499// strategy of FlexFEC matches the recovery strategy of ULPFEC. Since FlexFEC
500// does not share the sequence number space with the media, however, having a
501// matching recovery strategy may be suboptimal. Study this further.
nissea5f043f2017-09-18 07:58:59 -0700502// TODO(nisse): In this test, recovery based on the first FEC packet fails with
503// the log message "The recovered packet had a length larger than a typical IP
504// packet, and is thus dropped." This is probably not intended, and needs
505// investigation.
brandtrd726a3f2017-06-29 02:45:35 -0700506using RtpFecTestFlexfecOnly = RtpFecTest<FlexfecForwardErrorCorrection>;
507TEST_F(RtpFecTestFlexfecOnly, FecRecoveryWithSeqNumGapOneFrameNoRecovery) {
508 constexpr int kNumImportantPackets = 0;
509 constexpr bool kUseUnequalProtection = false;
510 constexpr uint8_t kProtectionFactor = 200;
511
512 // 1 frame: 3 media packets and 2 FEC packets.
513 // Sequence number wrap in FEC packets.
514 // -----Frame 1----
515 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
516 this->media_packets_ =
517 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
518
519 EXPECT_EQ(
520 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
521 kNumImportantPackets, kUseUnequalProtection,
522 kFecMaskBursty, &this->generated_fec_packets_));
523
524 // Expect 2 FEC packets.
525 EXPECT_EQ(2u, this->generated_fec_packets_.size());
526
527 // Overwrite the sequence numbers generated by ConstructMediaPackets,
528 // to make sure that we do have a wrap.
529 auto it = this->generated_fec_packets_.begin();
Danil Chapovalove15dc582021-01-07 15:24:05 +0100530 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data.MutableData()[2], 65535);
brandtrd726a3f2017-06-29 02:45:35 -0700531 ++it;
Danil Chapovalove15dc582021-01-07 15:24:05 +0100532 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data.MutableData()[2], 0);
brandtrd726a3f2017-06-29 02:45:35 -0700533
534 // Lose the last two media packets (seq# 65533, 65534).
535 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
536 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
537 this->media_loss_mask_[1] = 1;
538 this->media_loss_mask_[2] = 1;
539 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
540 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
541 true);
542
nissea5f043f2017-09-18 07:58:59 -0700543 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200544 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700545 }
brandtrd726a3f2017-06-29 02:45:35 -0700546
547 // The two FEC packets are received and should allow for complete recovery,
548 // but because of the wrap the first FEC packet will be discarded, and only
549 // one media packet is recoverable. So expect 2 media packets on recovered
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000550 // list and no complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700551 EXPECT_EQ(2u, this->recovered_packets_.size());
552 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
553 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000554}
555
brandtrd90fa0b2016-08-09 06:57:14 -0700556// Verify we can still recover frame if media packets are reordered.
brandtrece4aba2016-09-20 23:16:28 -0700557TYPED_TEST(RtpFecTest, FecRecoveryWithMediaOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700558 constexpr int kNumImportantPackets = 0;
559 constexpr bool kUseUnequalProtection = false;
560 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000561
562 // One frame: 3 media packets, 1 FEC packet.
563 // -----Frame 1----
564 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700565 this->media_packets_ =
566 this->media_packet_generator_.ConstructMediaPackets(3, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000567
brandtrece4aba2016-09-20 23:16:28 -0700568 EXPECT_EQ(
569 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
570 kNumImportantPackets, kUseUnequalProtection,
571 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000572
573 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700574 EXPECT_EQ(1u, this->generated_fec_packets_.size());
brandtrd90fa0b2016-08-09 06:57:14 -0700575
576 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700577 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
578 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
579 this->media_loss_mask_[1] = 1;
580 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
brandtrd90fa0b2016-08-09 06:57:14 -0700581
582 // Reorder received media packets.
brandtrece4aba2016-09-20 23:16:28 -0700583 auto it0 = this->received_packets_.begin();
brandtrd726a3f2017-06-29 02:45:35 -0700584 auto it1 = this->received_packets_.begin();
585 it1++;
586 std::swap(*it0, *it1);
brandtrd90fa0b2016-08-09 06:57:14 -0700587
nissea5f043f2017-09-18 07:58:59 -0700588 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200589 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700590 }
brandtrd90fa0b2016-08-09 06:57:14 -0700591
592 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700593 EXPECT_EQ(3u, this->recovered_packets_.size());
594 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd90fa0b2016-08-09 06:57:14 -0700595}
596
597// Verify we can still recover frame if FEC is received before media packets.
brandtrece4aba2016-09-20 23:16:28 -0700598TYPED_TEST(RtpFecTest, FecRecoveryWithFecOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700599 constexpr int kNumImportantPackets = 0;
600 constexpr bool kUseUnequalProtection = false;
601 constexpr uint8_t kProtectionFactor = 20;
602
603 // One frame: 3 media packets, 1 FEC packet.
604 // -----Frame 1----
605 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700606 this->media_packets_ =
607 this->media_packet_generator_.ConstructMediaPackets(3, 0);
brandtrd90fa0b2016-08-09 06:57:14 -0700608
brandtrece4aba2016-09-20 23:16:28 -0700609 EXPECT_EQ(
610 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
611 kNumImportantPackets, kUseUnequalProtection,
612 kFecMaskBursty, &this->generated_fec_packets_));
brandtrd90fa0b2016-08-09 06:57:14 -0700613
614 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700615 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000616
617 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700618 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
619 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
620 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000621 // Add FEC packet to received list before the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700622 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
623 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000624 // Add media packets to received list.
brandtrece4aba2016-09-20 23:16:28 -0700625 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000626
nissea5f043f2017-09-18 07:58:59 -0700627 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200628 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700629 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000630
631 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700632 EXPECT_EQ(3u, this->recovered_packets_.size());
633 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000634}
635
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000636// Test 50% protection with random mask type: Two cases are considered:
637// a 50% non-consecutive loss which can be fully recovered, and a 50%
638// consecutive loss which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700639TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700640 constexpr int kNumImportantPackets = 0;
641 constexpr bool kUseUnequalProtection = false;
642 constexpr int kNumMediaPackets = 4;
643 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000644
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000645 // Packet Mask for (4,4,0) code, from random mask table.
646 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000647
648 // media#0 media#1 media#2 media#3
649 // fec#0: 1 1 0 0
650 // fec#1: 1 0 1 0
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000651 // fec#2: 0 0 1 1
652 // fec#3: 0 1 0 1
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000653 //
654
brandtrece4aba2016-09-20 23:16:28 -0700655 this->media_packets_ =
656 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000657
brandtrece4aba2016-09-20 23:16:28 -0700658 EXPECT_EQ(
659 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
660 kNumImportantPackets, kUseUnequalProtection,
661 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000662
663 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700664 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000665
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000666 // 4 packets lost: 3 media packets (0, 2, 3), and one FEC packet (0) lost.
brandtrece4aba2016-09-20 23:16:28 -0700667 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
668 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
669 this->fec_loss_mask_[0] = 1;
670 this->media_loss_mask_[0] = 1;
671 this->media_loss_mask_[2] = 1;
672 this->media_loss_mask_[3] = 1;
673 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000674
nissea5f043f2017-09-18 07:58:59 -0700675 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200676 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700677 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000678
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000679 // With media packet#1 and FEC packets #1, #2, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700680 EXPECT_TRUE(this->IsRecoveryComplete());
681 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000682
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000683 // 4 consecutive packets lost: media packets 0, 1, 2, 3.
brandtrece4aba2016-09-20 23:16:28 -0700684 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
685 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
686 this->media_loss_mask_[0] = 1;
687 this->media_loss_mask_[1] = 1;
688 this->media_loss_mask_[2] = 1;
689 this->media_loss_mask_[3] = 1;
690 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000691
nissea5f043f2017-09-18 07:58:59 -0700692 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200693 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700694 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000695
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000696 // Cannot get complete recovery for this loss configuration with random mask.
brandtrece4aba2016-09-20 23:16:28 -0700697 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000698}
699
700// Test 50% protection with bursty type: Three cases are considered:
701// two 50% consecutive losses which can be fully recovered, and one
702// non-consecutive which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700703TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percBurstyMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700704 constexpr int kNumImportantPackets = 0;
705 constexpr bool kUseUnequalProtection = false;
706 constexpr int kNumMediaPackets = 4;
707 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000708
709 // Packet Mask for (4,4,0) code, from bursty mask table.
710 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
711
712 // media#0 media#1 media#2 media#3
713 // fec#0: 1 0 0 0
714 // fec#1: 1 1 0 0
715 // fec#2: 0 1 1 0
716 // fec#3: 0 0 1 1
717 //
718
brandtrece4aba2016-09-20 23:16:28 -0700719 this->media_packets_ =
720 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000721
brandtrece4aba2016-09-20 23:16:28 -0700722 EXPECT_EQ(
723 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
724 kNumImportantPackets, kUseUnequalProtection,
725 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000726
727 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700728 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000729
730 // 4 consecutive packets lost: media packets 0,1,2,3.
brandtrece4aba2016-09-20 23:16:28 -0700731 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
732 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
733 this->media_loss_mask_[0] = 1;
734 this->media_loss_mask_[1] = 1;
735 this->media_loss_mask_[2] = 1;
736 this->media_loss_mask_[3] = 1;
737 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000738
nissea5f043f2017-09-18 07:58:59 -0700739 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200740 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700741 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000742
743 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700744 EXPECT_TRUE(this->IsRecoveryComplete());
745 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000746
747 // 4 consecutive packets lost: media packets 1,2, 3, and FEC packet 0.
brandtrece4aba2016-09-20 23:16:28 -0700748 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
749 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
750 this->fec_loss_mask_[0] = 1;
751 this->media_loss_mask_[1] = 1;
752 this->media_loss_mask_[2] = 1;
753 this->media_loss_mask_[3] = 1;
754 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000755
nissea5f043f2017-09-18 07:58:59 -0700756 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200757 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700758 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000759
760 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700761 EXPECT_TRUE(this->IsRecoveryComplete());
762 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000763
764 // 4 packets lost (non-consecutive loss): media packets 0, 3, and FEC# 0, 3.
brandtrece4aba2016-09-20 23:16:28 -0700765 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
766 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
767 this->fec_loss_mask_[0] = 1;
768 this->fec_loss_mask_[3] = 1;
769 this->media_loss_mask_[0] = 1;
770 this->media_loss_mask_[3] = 1;
771 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000772
nissea5f043f2017-09-18 07:58:59 -0700773 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200774 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700775 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000776
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000777 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700778 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000779}
780
brandtrece4aba2016-09-20 23:16:28 -0700781TYPED_TEST(RtpFecTest, FecRecoveryNoLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700782 constexpr int kNumImportantPackets = 2;
783 constexpr bool kUseUnequalProtection = true;
784 constexpr int kNumMediaPackets = 4;
785 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000786
brandtrece4aba2016-09-20 23:16:28 -0700787 this->media_packets_ =
788 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000789
brandtrece4aba2016-09-20 23:16:28 -0700790 EXPECT_EQ(
791 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
792 kNumImportantPackets, kUseUnequalProtection,
793 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000794
795 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700796 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000797
798 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700799 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
800 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
801 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000802
nissea5f043f2017-09-18 07:58:59 -0700803 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200804 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700805 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000806
807 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700808 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000809}
810
brandtrece4aba2016-09-20 23:16:28 -0700811TYPED_TEST(RtpFecTest, FecRecoveryWithLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700812 constexpr int kNumImportantPackets = 2;
813 constexpr bool kUseUnequalProtection = true;
814 constexpr int kNumMediaPackets = 4;
815 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000816
brandtrece4aba2016-09-20 23:16:28 -0700817 this->media_packets_ =
818 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000819
brandtrece4aba2016-09-20 23:16:28 -0700820 EXPECT_EQ(
821 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
822 kNumImportantPackets, kUseUnequalProtection,
823 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000824
825 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700826 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000827
828 // 1 media packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700829 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
830 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
831 this->media_loss_mask_[3] = 1;
832 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000833
nissea5f043f2017-09-18 07:58:59 -0700834 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200835 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700836 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000837
838 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700839 EXPECT_TRUE(this->IsRecoveryComplete());
840 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000841
842 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700843 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
844 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
845 this->media_loss_mask_[1] = 1;
846 this->media_loss_mask_[3] = 1;
847 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000848
nissea5f043f2017-09-18 07:58:59 -0700849 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200850 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700851 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000852
853 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700854 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000855}
856
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000857// Test 50% protection with random mask type for UEP on.
brandtrece4aba2016-09-20 23:16:28 -0700858TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percUepRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700859 constexpr int kNumImportantPackets = 1;
860 constexpr bool kUseUnequalProtection = true;
861 constexpr int kNumMediaPackets = 4;
862 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000863
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000864 // Packet Mask for (4,4,1) code, from random mask table.
865 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 1)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000866
867 // media#0 media#1 media#2 media#3
868 // fec#0: 1 0 0 0
869 // fec#1: 1 1 0 0
870 // fec#2: 1 0 1 1
871 // fec#3: 0 1 1 0
872 //
873
brandtrece4aba2016-09-20 23:16:28 -0700874 this->media_packets_ =
875 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000876
brandtrece4aba2016-09-20 23:16:28 -0700877 EXPECT_EQ(
878 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
879 kNumImportantPackets, kUseUnequalProtection,
880 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000881
882 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700883 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000884
885 // 4 packets lost: 3 media packets and FEC packet#1 lost.
brandtrece4aba2016-09-20 23:16:28 -0700886 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
887 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
888 this->fec_loss_mask_[1] = 1;
889 this->media_loss_mask_[0] = 1;
890 this->media_loss_mask_[2] = 1;
891 this->media_loss_mask_[3] = 1;
892 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000893
nissea5f043f2017-09-18 07:58:59 -0700894 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200895 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700896 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000897
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000898 // With media packet#3 and FEC packets #0, #1, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700899 EXPECT_TRUE(this->IsRecoveryComplete());
900 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000901
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000902 // 5 packets lost: 4 media packets and one FEC packet#2 lost.
brandtrece4aba2016-09-20 23:16:28 -0700903 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
904 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
905 this->fec_loss_mask_[2] = 1;
906 this->media_loss_mask_[0] = 1;
907 this->media_loss_mask_[1] = 1;
908 this->media_loss_mask_[2] = 1;
909 this->media_loss_mask_[3] = 1;
910 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000911
nissea5f043f2017-09-18 07:58:59 -0700912 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200913 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700914 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000915
916 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700917 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000918}
919
brandtrece4aba2016-09-20 23:16:28 -0700920TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePackets) {
brandtrd90fa0b2016-08-09 06:57:14 -0700921 constexpr int kNumImportantPackets = 0;
922 constexpr bool kUseUnequalProtection = false;
923 constexpr int kNumMediaPackets = 5;
924 constexpr uint8_t kProtectionFactor = 60;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000925
brandtrece4aba2016-09-20 23:16:28 -0700926 this->media_packets_ =
927 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000928
929 // Create a new temporary packet list for generating FEC packets.
930 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700931 ForwardErrorCorrection::PacketList protected_media_packets;
932 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000933
brandtrece4aba2016-09-20 23:16:28 -0700934 EXPECT_EQ(
935 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
936 kNumImportantPackets, kUseUnequalProtection,
937 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000938
939 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700940 EXPECT_EQ(1u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000941
942 // 1 protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700943 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
944 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
945 this->media_loss_mask_[2] = 1;
946 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000947
nissea5f043f2017-09-18 07:58:59 -0700948 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200949 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700950 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000951
952 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700953 EXPECT_TRUE(this->IsRecoveryComplete());
954 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000955
956 // Unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700957 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
958 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
959 this->media_loss_mask_[1] = 1;
960 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000961
nissea5f043f2017-09-18 07:58:59 -0700962 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200963 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700964 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000965
966 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -0700967 EXPECT_FALSE(this->IsRecoveryComplete());
968 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000969
970 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700971 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
972 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
973 this->media_loss_mask_[0] = 1;
974 this->media_loss_mask_[2] = 1;
975 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000976
nissea5f043f2017-09-18 07:58:59 -0700977 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200978 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -0700979 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000980
981 // 2 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700982 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000983}
984
brandtrece4aba2016-09-20 23:16:28 -0700985TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsExtension) {
brandtrd90fa0b2016-08-09 06:57:14 -0700986 constexpr int kNumImportantPackets = 0;
987 constexpr bool kUseUnequalProtection = false;
988 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000989 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000990
brandtrece4aba2016-09-20 23:16:28 -0700991 this->media_packets_ =
992 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000993
994 // Create a new temporary packet list for generating FEC packets.
995 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700996 ForwardErrorCorrection::PacketList protected_media_packets;
997 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000998
999 // Zero column insertion will have to extend the size of the packet
1000 // mask since the number of actual packets are 21, while the number
1001 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001002 EXPECT_EQ(
1003 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1004 kNumImportantPackets, kUseUnequalProtection,
1005 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001006
1007 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001008 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001009
1010 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001011 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1012 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1013 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1014 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001015
nissea5f043f2017-09-18 07:58:59 -07001016 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001017 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001018 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001019
1020 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001021 EXPECT_TRUE(this->IsRecoveryComplete());
1022 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001023
1024 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001025 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1026 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1027 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1028 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001029
nissea5f043f2017-09-18 07:58:59 -07001030 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001031 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001032 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001033
1034 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001035 EXPECT_FALSE(this->IsRecoveryComplete());
1036 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001037
1038 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001039 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1040 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1041 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1042 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1043 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1044 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1045 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1046 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1047 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001048
nissea5f043f2017-09-18 07:58:59 -07001049 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001050 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001051 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001052
1053 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001054 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001055}
1056
brandtrece4aba2016-09-20 23:16:28 -07001057TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
brandtrd90fa0b2016-08-09 06:57:14 -07001058 constexpr int kNumImportantPackets = 0;
1059 constexpr bool kUseUnequalProtection = false;
1060 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001061 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001062
brandtrece4aba2016-09-20 23:16:28 -07001063 this->media_packets_ = this->media_packet_generator_.ConstructMediaPackets(
1064 kNumMediaPackets, 0xFFFF - 5);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001065
1066 // Create a new temporary packet list for generating FEC packets.
1067 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001068 ForwardErrorCorrection::PacketList protected_media_packets;
1069 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001070
1071 // Zero column insertion will have to extend the size of the packet
1072 // mask since the number of actual packets are 21, while the number
1073 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001074 EXPECT_EQ(
1075 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1076 kNumImportantPackets, kUseUnequalProtection,
1077 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001078
1079 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001080 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001081
1082 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001083 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1084 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1085 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1086 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001087
nissea5f043f2017-09-18 07:58:59 -07001088 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001089 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001090 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001091
1092 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001093 EXPECT_TRUE(this->IsRecoveryComplete());
1094 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001095
1096 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001097 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1098 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1099 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1100 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001101
nissea5f043f2017-09-18 07:58:59 -07001102 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001103 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001104 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001105
1106 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001107 EXPECT_FALSE(this->IsRecoveryComplete());
1108 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001109
1110 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001111 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1112 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1113 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1114 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1115 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1116 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1117 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1118 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1119 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001120
nissea5f043f2017-09-18 07:58:59 -07001121 for (const auto& received_packet : this->received_packets_) {
Yves Gerey665174f2018-06-19 15:03:05 +02001122 this->fec_.DecodeFec(*received_packet, &this->recovered_packets_);
nissea5f043f2017-09-18 07:58:59 -07001123 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001124
1125 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001126 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001127}
1128
brandtrece4aba2016-09-20 23:16:28 -07001129} // namespace webrtc