blob: d01aa559945f93049ef7d22d79259fefdef8af82 [file] [log] [blame]
marpan@webrtc.orgb783a552012-02-04 02:46:35 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Rasmus Brandtae4f7672016-07-07 09:40:51 +020011#include <algorithm>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000012#include <list>
brandtrece4aba2016-09-20 23:16:28 -070013#include <memory>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/fec_test_helper.h"
17#include "modules/rtp_rtcp/source/flexfec_header_reader_writer.h"
18#include "modules/rtp_rtcp/source/forward_error_correction.h"
19#include "modules/rtp_rtcp/source/ulpfec_header_reader_writer.h"
20#include "rtc_base/basictypes.h"
21#include "rtc_base/random.h"
22#include "test/gtest.h"
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000023
brandtrece4aba2016-09-20 23:16:28 -070024namespace webrtc {
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000025
brandtrece4aba2016-09-20 23:16:28 -070026namespace {
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000027
28// Transport header size in bytes. Assume UDP/IPv4 as a reasonable minimum.
brandtrece4aba2016-09-20 23:16:28 -070029constexpr size_t kTransportOverhead = 28;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000030
brandtrece4aba2016-09-20 23:16:28 -070031constexpr uint32_t kMediaSsrc = 83542;
brandtr0496de22016-10-03 00:43:25 -070032constexpr uint32_t kFlexfecSsrc = 43245;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000033
brandtrece4aba2016-09-20 23:16:28 -070034// Deep copies |src| to |dst|, but only keeps every Nth packet.
35void DeepCopyEveryNthPacket(const ForwardErrorCorrection::PacketList& src,
36 int n,
37 ForwardErrorCorrection::PacketList* dst) {
38 RTC_DCHECK_GT(n, 0);
39 int i = 0;
40 for (const auto& packet : src) {
41 if (i % n == 0) {
42 dst->emplace_back(new ForwardErrorCorrection::Packet(*packet));
43 }
44 ++i;
45 }
46}
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +000047
brandtrece4aba2016-09-20 23:16:28 -070048} // namespace
49
50using ::testing::Types;
51
52template <typename ForwardErrorCorrectionType>
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000053class RtpFecTest : public ::testing::Test {
54 protected:
55 RtpFecTest()
brandtrece4aba2016-09-20 23:16:28 -070056 : random_(0xabcdef123456),
57 media_packet_generator_(
58 kRtpHeaderSize, // Minimum packet size.
59 IP_PACKET_SIZE - kRtpHeaderSize - kTransportOverhead -
60 fec_.MaxPacketOverhead(), // Maximum packet size.
61 kMediaSsrc,
62 &random_) {}
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000063
brandtrece4aba2016-09-20 23:16:28 -070064 // Construct |received_packets_|: a subset of the media and FEC packets.
brandtrd90fa0b2016-08-09 06:57:14 -070065 //
66 // Media packet "i" is lost if media_loss_mask_[i] = 1, received if
67 // media_loss_mask_[i] = 0.
68 // FEC packet "i" is lost if fec_loss_mask_[i] = 1, received if
69 // fec_loss_mask_[i] = 0.
70 void NetworkReceivedPackets(int* media_loss_mask, int* fec_loss_mask);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000071
72 // Add packet from |packet_list| to list of received packets, using the
73 // |loss_mask|.
74 // The |packet_list| may be a media packet list (is_fec = false), or a
75 // FEC packet list (is_fec = true).
brandtr35c480c2016-08-09 01:23:23 -070076 template <typename T>
77 void ReceivedPackets(const T& packet_list, int* loss_mask, bool is_fec);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000078
79 // Check for complete recovery after FEC decoding.
80 bool IsRecoveryComplete();
81
brandtrece4aba2016-09-20 23:16:28 -070082 ForwardErrorCorrectionType fec_;
brandtrd90fa0b2016-08-09 06:57:14 -070083
brandtrece4aba2016-09-20 23:16:28 -070084 Random random_;
85 test::fec::MediaPacketGenerator media_packet_generator_;
brandtrd90fa0b2016-08-09 06:57:14 -070086
brandtrece4aba2016-09-20 23:16:28 -070087 ForwardErrorCorrection::PacketList media_packets_;
88 std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_;
nissea5f043f2017-09-18 07:58:59 -070089 std::vector<std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>>
90 received_packets_;
brandtrece4aba2016-09-20 23:16:28 -070091 ForwardErrorCorrection::RecoveredPacketList recovered_packets_;
brandtrd90fa0b2016-08-09 06:57:14 -070092
Rasmus Brandt78db1582016-09-21 09:19:34 +020093 int media_loss_mask_[kUlpfecMaxMediaPackets];
94 int fec_loss_mask_[kUlpfecMaxMediaPackets];
marpan@webrtc.orgb783a552012-02-04 02:46:35 +000095};
96
Rasmus Brandtea7beb92016-09-21 12:01:19 +020097template <typename ForwardErrorCorrectionType>
98void RtpFecTest<ForwardErrorCorrectionType>::NetworkReceivedPackets(
99 int* media_loss_mask,
100 int* fec_loss_mask) {
101 constexpr bool kFecPacket = true;
nissea5f043f2017-09-18 07:58:59 -0700102 this->received_packets_.clear();
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200103 ReceivedPackets(media_packets_, media_loss_mask, !kFecPacket);
104 ReceivedPackets(generated_fec_packets_, fec_loss_mask, kFecPacket);
105}
106
107template <typename ForwardErrorCorrectionType>
108template <typename PacketListType>
109void RtpFecTest<ForwardErrorCorrectionType>::ReceivedPackets(
110 const PacketListType& packet_list,
111 int* loss_mask,
112 bool is_fec) {
brandtrd726a3f2017-06-29 02:45:35 -0700113 uint16_t fec_seq_num = ForwardErrorCorrectionType::GetFirstFecSeqNum(
114 media_packet_generator_.GetNextSeqNum());
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200115 int packet_idx = 0;
116
117 for (const auto& packet : packet_list) {
118 if (loss_mask[packet_idx] == 0) {
119 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet(
120 new ForwardErrorCorrection::ReceivedPacket());
121 received_packet->pkt = new ForwardErrorCorrection::Packet();
122 received_packet->pkt->length = packet->length;
123 memcpy(received_packet->pkt->data, packet->data, packet->length);
124 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() {
150 // We must have equally many recovered packets as original packets.
151 if (recovered_packets_.size() != media_packets_.size()) {
152 return false;
153 }
154
155 // All recovered packets must be identical to the corresponding
156 // original packets.
brandtr0496de22016-10-03 00:43:25 -0700157 auto cmp = [](
158 const std::unique_ptr<ForwardErrorCorrection::Packet>& media_packet,
159 const std::unique_ptr<ForwardErrorCorrection::RecoveredPacket>&
160 recovered_packet) {
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200161 if (media_packet->length != recovered_packet->pkt->length) {
162 return false;
163 }
brandtr0496de22016-10-03 00:43:25 -0700164 if (memcmp(media_packet->data, recovered_packet->pkt->data,
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200165 media_packet->length) != 0) {
166 return false;
167 }
168 return true;
169 };
170 return std::equal(media_packets_.cbegin(), media_packets_.cend(),
171 recovered_packets_.cbegin(), cmp);
172}
173
brandtrece4aba2016-09-20 23:16:28 -0700174// Define gTest typed test to loop over both ULPFEC and FlexFEC.
175// Since the tests now are parameterized, we need to access
176// member variables using |this|, thereby enforcing runtime
177// resolution.
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200178
brandtr0496de22016-10-03 00:43:25 -0700179class FlexfecForwardErrorCorrection : public ForwardErrorCorrection {
180 public:
brandtrd726a3f2017-06-29 02:45:35 -0700181 static const uint32_t kFecSsrc = kFlexfecSsrc;
182
brandtr0496de22016-10-03 00:43:25 -0700183 FlexfecForwardErrorCorrection()
184 : ForwardErrorCorrection(
185 std::unique_ptr<FecHeaderReader>(new FlexfecHeaderReader()),
brandtrd726a3f2017-06-29 02:45:35 -0700186 std::unique_ptr<FecHeaderWriter>(new FlexfecHeaderWriter()),
187 kFecSsrc,
188 kMediaSsrc) {}
189
190 // For FlexFEC we let the FEC packet sequence numbers be independent of
191 // the media packet sequence numbers.
192 static uint16_t GetFirstFecSeqNum(uint16_t next_media_seq_num) {
193 Random random(0xbe110);
194 return random.Rand<uint16_t>();
195 }
brandtr0496de22016-10-03 00:43:25 -0700196};
197
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200198class UlpfecForwardErrorCorrection : public ForwardErrorCorrection {
199 public:
brandtrd726a3f2017-06-29 02:45:35 -0700200 static const uint32_t kFecSsrc = kMediaSsrc;
201
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200202 UlpfecForwardErrorCorrection()
203 : ForwardErrorCorrection(
204 std::unique_ptr<FecHeaderReader>(new UlpfecHeaderReader()),
brandtrd726a3f2017-06-29 02:45:35 -0700205 std::unique_ptr<FecHeaderWriter>(new UlpfecHeaderWriter()),
206 kFecSsrc,
207 kMediaSsrc) {}
208
209 // For ULPFEC we assume that the FEC packets are subsequent to the media
210 // packets in terms of sequence number.
211 static uint16_t GetFirstFecSeqNum(uint16_t next_media_seq_num) {
212 return next_media_seq_num;
213 }
Rasmus Brandtea7beb92016-09-21 12:01:19 +0200214};
215
brandtr0496de22016-10-03 00:43:25 -0700216using FecTypes =
217 Types<FlexfecForwardErrorCorrection, UlpfecForwardErrorCorrection>;
brandtrece4aba2016-09-20 23:16:28 -0700218TYPED_TEST_CASE(RtpFecTest, FecTypes);
219
220TYPED_TEST(RtpFecTest, FecRecoveryNoLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700221 constexpr int kNumImportantPackets = 0;
222 constexpr bool kUseUnequalProtection = false;
223 constexpr int kNumMediaPackets = 4;
224 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000225
brandtrece4aba2016-09-20 23:16:28 -0700226 this->media_packets_ =
227 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000228
brandtrece4aba2016-09-20 23:16:28 -0700229 EXPECT_EQ(
230 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
231 kNumImportantPackets, kUseUnequalProtection,
232 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000233
234 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700235 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000236
237 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700238 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
239 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
240 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000241
nissea5f043f2017-09-18 07:58:59 -0700242 for (const auto& received_packet : this->received_packets_) {
243 this->fec_.DecodeFec(*received_packet,
244 &this->recovered_packets_);
245 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000246
247 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700248 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000249}
250
brandtrece4aba2016-09-20 23:16:28 -0700251TYPED_TEST(RtpFecTest, FecRecoveryWithLoss) {
brandtrd90fa0b2016-08-09 06:57:14 -0700252 constexpr int kNumImportantPackets = 0;
253 constexpr bool kUseUnequalProtection = false;
254 constexpr int kNumMediaPackets = 4;
255 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000256
brandtrece4aba2016-09-20 23:16:28 -0700257 this->media_packets_ =
258 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000259
brandtrece4aba2016-09-20 23:16:28 -0700260 EXPECT_EQ(
261 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
262 kNumImportantPackets, kUseUnequalProtection,
263 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000264
265 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700266 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000267
268 // 1 media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700269 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
270 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
271 this->media_loss_mask_[3] = 1;
272 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000273
nissea5f043f2017-09-18 07:58:59 -0700274 for (const auto& received_packet : this->received_packets_) {
275 this->fec_.DecodeFec(*received_packet,
276 &this->recovered_packets_);
277 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000278
279 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700280 EXPECT_TRUE(this->IsRecoveryComplete());
281 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000282
283 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700284 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
285 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
286 this->media_loss_mask_[1] = 1;
287 this->media_loss_mask_[3] = 1;
288 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000289
nissea5f043f2017-09-18 07:58:59 -0700290 for (const auto& received_packet : this->received_packets_) {
291 this->fec_.DecodeFec(*received_packet,
292 &this->recovered_packets_);
293 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000294
295 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700296 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000297}
298
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000299// Verify that we don't use an old FEC packet for FEC decoding.
brandtrece4aba2016-09-20 23:16:28 -0700300TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapTwoFrames) {
brandtrd90fa0b2016-08-09 06:57:14 -0700301 constexpr int kNumImportantPackets = 0;
302 constexpr bool kUseUnequalProtection = false;
303 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000304
305 // Two frames: first frame (old) with two media packets and 1 FEC packet.
306 // Second frame (new) with 3 media packets, and no FEC packets.
307 // ---Frame 1---- ----Frame 2------
308 // #0(media) #1(media) #2(FEC) #65535(media) #0(media) #1(media).
309 // If we lose either packet 0 or 1 of second frame, FEC decoding should not
310 // try to decode using "old" FEC packet #2.
311
312 // Construct media packets for first frame, starting at sequence number 0.
brandtrece4aba2016-09-20 23:16:28 -0700313 this->media_packets_ =
314 this->media_packet_generator_.ConstructMediaPackets(2, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000315
brandtrece4aba2016-09-20 23:16:28 -0700316 EXPECT_EQ(
317 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
318 kNumImportantPackets, kUseUnequalProtection,
319 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000320 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700321 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000322 // Add FEC packet (seq#2) of this first frame to received list (i.e., assume
323 // the two media packet were lost).
brandtrece4aba2016-09-20 23:16:28 -0700324 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
325 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
326 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000327
328 // Construct media packets for second frame, with sequence number wrap.
brandtrece4aba2016-09-20 23:16:28 -0700329 this->media_packets_ =
330 this->media_packet_generator_.ConstructMediaPackets(3, 65535);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000331
332 // Expect 3 media packets for this frame.
brandtrece4aba2016-09-20 23:16:28 -0700333 EXPECT_EQ(3u, this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000334
335 // Second media packet lost (seq#0).
brandtrece4aba2016-09-20 23:16:28 -0700336 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
337 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000338 // Add packets #65535, and #1 to received list.
brandtrece4aba2016-09-20 23:16:28 -0700339 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000340
nissea5f043f2017-09-18 07:58:59 -0700341 for (const auto& received_packet : this->received_packets_) {
342 this->fec_.DecodeFec(*received_packet,
343 &this->recovered_packets_);
344 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000345
346 // Expect that no decoding is done to get missing packet (seq#0) of second
347 // frame, using old FEC packet (seq#2) from first (old) frame. So number of
348 // recovered packets is 2, and not equal to number of media packets (=3).
brandtrece4aba2016-09-20 23:16:28 -0700349 EXPECT_EQ(2u, this->recovered_packets_.size());
350 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000351}
352
brandtrd90fa0b2016-08-09 06:57:14 -0700353// Verify we can still recover frame if sequence number wrap occurs within
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000354// the frame and FEC packet following wrap is received after media packets.
brandtrece4aba2016-09-20 23:16:28 -0700355TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700356 constexpr int kNumImportantPackets = 0;
357 constexpr bool kUseUnequalProtection = false;
358 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000359
360 // One frame, with sequence number wrap in media packets.
361 // -----Frame 1----
362 // #65534(media) #65535(media) #0(media) #1(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700363 this->media_packets_ =
364 this->media_packet_generator_.ConstructMediaPackets(3, 65534);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000365
brandtrece4aba2016-09-20 23:16:28 -0700366 EXPECT_EQ(
367 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
368 kNumImportantPackets, kUseUnequalProtection,
369 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000370
371 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700372 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000373
374 // Lose one media packet (seq# 65535).
brandtrece4aba2016-09-20 23:16:28 -0700375 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
376 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
377 this->media_loss_mask_[1] = 1;
378 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000379 // Add FEC packet to received list following the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700380 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
381 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000382
nissea5f043f2017-09-18 07:58:59 -0700383 for (const auto& received_packet : this->received_packets_) {
384 this->fec_.DecodeFec(*received_packet,
385 &this->recovered_packets_);
386 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000387
388 // Expect 3 media packets in recovered list, and complete recovery.
389 // Wrap-around won't remove FEC packet, as it follows the wrap.
brandtrece4aba2016-09-20 23:16:28 -0700390 EXPECT_EQ(3u, this->recovered_packets_.size());
391 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000392}
393
brandtrd726a3f2017-06-29 02:45:35 -0700394// Sequence number wrap occurs within the ULPFEC packets for the frame.
brandtrd726a3f2017-06-29 02:45:35 -0700395// Same problem will occur if wrap is within media packets but ULPFEC packet is
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000396// received before the media packets. This may be improved if timing information
brandtrd726a3f2017-06-29 02:45:35 -0700397// is used to detect old ULPFEC packets.
nissea5f043f2017-09-18 07:58:59 -0700398
399// TODO(nisse): There's some logic to discard ULPFEC packets at wrap-around,
400// however, that is not actually exercised by this test: When the first FEC
401// packet is processed, it results in full recovery of one media packet and the
402// FEC packet is forgotten. And then the wraparound isn't noticed when the next
403// FEC packet is received. We should fix wraparound handling, which currently
404// appears broken, and then figure out how to test it properly.
brandtrd726a3f2017-06-29 02:45:35 -0700405using RtpFecTestUlpfecOnly = RtpFecTest<UlpfecForwardErrorCorrection>;
nissea5f043f2017-09-18 07:58:59 -0700406TEST_F(RtpFecTestUlpfecOnly, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700407 constexpr int kNumImportantPackets = 0;
408 constexpr bool kUseUnequalProtection = false;
409 constexpr uint8_t kProtectionFactor = 200;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000410
411 // 1 frame: 3 media packets and 2 FEC packets.
412 // Sequence number wrap in FEC packets.
413 // -----Frame 1----
414 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700415 this->media_packets_ =
416 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000417
brandtrece4aba2016-09-20 23:16:28 -0700418 EXPECT_EQ(
419 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
420 kNumImportantPackets, kUseUnequalProtection,
421 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000422
423 // Expect 2 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700424 EXPECT_EQ(2u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000425
426 // Lose the last two media packets (seq# 65533, 65534).
brandtrece4aba2016-09-20 23:16:28 -0700427 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
428 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
429 this->media_loss_mask_[1] = 1;
430 this->media_loss_mask_[2] = 1;
431 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
432 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
433 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000434
nissea5f043f2017-09-18 07:58:59 -0700435 for (const auto& received_packet : this->received_packets_) {
436 this->fec_.DecodeFec(*received_packet,
437 &this->recovered_packets_);
438 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000439
440 // The two FEC packets are received and should allow for complete recovery,
brandtrd726a3f2017-06-29 02:45:35 -0700441 // but because of the wrap the first FEC packet will be discarded, and only
442 // one media packet is recoverable. So expect 2 media packets on recovered
443 // list and no complete recovery.
nissea5f043f2017-09-18 07:58:59 -0700444 EXPECT_EQ(3u, this->recovered_packets_.size());
445 EXPECT_EQ(this->recovered_packets_.size(), this->media_packets_.size());
446 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd726a3f2017-06-29 02:45:35 -0700447}
448
449// TODO(brandtr): This test mimics the one above, ensuring that the recovery
450// strategy of FlexFEC matches the recovery strategy of ULPFEC. Since FlexFEC
451// does not share the sequence number space with the media, however, having a
452// matching recovery strategy may be suboptimal. Study this further.
nissea5f043f2017-09-18 07:58:59 -0700453// TODO(nisse): In this test, recovery based on the first FEC packet fails with
454// the log message "The recovered packet had a length larger than a typical IP
455// packet, and is thus dropped." This is probably not intended, and needs
456// investigation.
brandtrd726a3f2017-06-29 02:45:35 -0700457using RtpFecTestFlexfecOnly = RtpFecTest<FlexfecForwardErrorCorrection>;
458TEST_F(RtpFecTestFlexfecOnly, FecRecoveryWithSeqNumGapOneFrameNoRecovery) {
459 constexpr int kNumImportantPackets = 0;
460 constexpr bool kUseUnequalProtection = false;
461 constexpr uint8_t kProtectionFactor = 200;
462
463 // 1 frame: 3 media packets and 2 FEC packets.
464 // Sequence number wrap in FEC packets.
465 // -----Frame 1----
466 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
467 this->media_packets_ =
468 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
469
470 EXPECT_EQ(
471 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
472 kNumImportantPackets, kUseUnequalProtection,
473 kFecMaskBursty, &this->generated_fec_packets_));
474
475 // Expect 2 FEC packets.
476 EXPECT_EQ(2u, this->generated_fec_packets_.size());
477
478 // Overwrite the sequence numbers generated by ConstructMediaPackets,
479 // to make sure that we do have a wrap.
480 auto it = this->generated_fec_packets_.begin();
481 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 65535);
482 ++it;
483 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 0);
484
485 // Lose the last two media packets (seq# 65533, 65534).
486 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
487 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
488 this->media_loss_mask_[1] = 1;
489 this->media_loss_mask_[2] = 1;
490 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
491 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
492 true);
493
nissea5f043f2017-09-18 07:58:59 -0700494 for (const auto& received_packet : this->received_packets_) {
495 this->fec_.DecodeFec(*received_packet,
496 &this->recovered_packets_);
497 }
brandtrd726a3f2017-06-29 02:45:35 -0700498
499 // The two FEC packets are received and should allow for complete recovery,
500 // but because of the wrap the first FEC packet will be discarded, and only
501 // one media packet is recoverable. So expect 2 media packets on recovered
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000502 // list and no complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700503 EXPECT_EQ(2u, this->recovered_packets_.size());
504 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
505 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000506}
507
brandtrd90fa0b2016-08-09 06:57:14 -0700508// Verify we can still recover frame if media packets are reordered.
brandtrece4aba2016-09-20 23:16:28 -0700509TYPED_TEST(RtpFecTest, FecRecoveryWithMediaOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700510 constexpr int kNumImportantPackets = 0;
511 constexpr bool kUseUnequalProtection = false;
512 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000513
514 // One frame: 3 media packets, 1 FEC packet.
515 // -----Frame 1----
516 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700517 this->media_packets_ =
518 this->media_packet_generator_.ConstructMediaPackets(3, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000519
brandtrece4aba2016-09-20 23:16:28 -0700520 EXPECT_EQ(
521 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
522 kNumImportantPackets, kUseUnequalProtection,
523 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000524
525 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700526 EXPECT_EQ(1u, this->generated_fec_packets_.size());
brandtrd90fa0b2016-08-09 06:57:14 -0700527
528 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700529 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
530 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
531 this->media_loss_mask_[1] = 1;
532 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
brandtrd90fa0b2016-08-09 06:57:14 -0700533
534 // Reorder received media packets.
brandtrece4aba2016-09-20 23:16:28 -0700535 auto it0 = this->received_packets_.begin();
brandtrd726a3f2017-06-29 02:45:35 -0700536 auto it1 = this->received_packets_.begin();
537 it1++;
538 std::swap(*it0, *it1);
brandtrd90fa0b2016-08-09 06:57:14 -0700539
nissea5f043f2017-09-18 07:58:59 -0700540 for (const auto& received_packet : this->received_packets_) {
541 this->fec_.DecodeFec(*received_packet,
542 &this->recovered_packets_);
543 }
brandtrd90fa0b2016-08-09 06:57:14 -0700544
545 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700546 EXPECT_EQ(3u, this->recovered_packets_.size());
547 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd90fa0b2016-08-09 06:57:14 -0700548}
549
550// Verify we can still recover frame if FEC is received before media packets.
brandtrece4aba2016-09-20 23:16:28 -0700551TYPED_TEST(RtpFecTest, FecRecoveryWithFecOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700552 constexpr int kNumImportantPackets = 0;
553 constexpr bool kUseUnequalProtection = false;
554 constexpr uint8_t kProtectionFactor = 20;
555
556 // One frame: 3 media packets, 1 FEC packet.
557 // -----Frame 1----
558 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700559 this->media_packets_ =
560 this->media_packet_generator_.ConstructMediaPackets(3, 0);
brandtrd90fa0b2016-08-09 06:57:14 -0700561
brandtrece4aba2016-09-20 23:16:28 -0700562 EXPECT_EQ(
563 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
564 kNumImportantPackets, kUseUnequalProtection,
565 kFecMaskBursty, &this->generated_fec_packets_));
brandtrd90fa0b2016-08-09 06:57:14 -0700566
567 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700568 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000569
570 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700571 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
572 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
573 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000574 // Add FEC packet to received list before the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700575 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
576 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000577 // Add media packets to received list.
brandtrece4aba2016-09-20 23:16:28 -0700578 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000579
nissea5f043f2017-09-18 07:58:59 -0700580 for (const auto& received_packet : this->received_packets_) {
581 this->fec_.DecodeFec(*received_packet,
582 &this->recovered_packets_);
583 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000584
585 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700586 EXPECT_EQ(3u, this->recovered_packets_.size());
587 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000588}
589
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000590// Test 50% protection with random mask type: Two cases are considered:
591// a 50% non-consecutive loss which can be fully recovered, and a 50%
592// consecutive loss which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700593TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700594 constexpr int kNumImportantPackets = 0;
595 constexpr bool kUseUnequalProtection = false;
596 constexpr int kNumMediaPackets = 4;
597 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000598
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000599 // Packet Mask for (4,4,0) code, from random mask table.
600 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000601
602 // media#0 media#1 media#2 media#3
603 // fec#0: 1 1 0 0
604 // fec#1: 1 0 1 0
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000605 // fec#2: 0 0 1 1
606 // fec#3: 0 1 0 1
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000607 //
608
brandtrece4aba2016-09-20 23:16:28 -0700609 this->media_packets_ =
610 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000611
brandtrece4aba2016-09-20 23:16:28 -0700612 EXPECT_EQ(
613 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
614 kNumImportantPackets, kUseUnequalProtection,
615 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000616
617 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700618 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000619
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000620 // 4 packets lost: 3 media packets (0, 2, 3), and one FEC packet (0) lost.
brandtrece4aba2016-09-20 23:16:28 -0700621 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
622 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
623 this->fec_loss_mask_[0] = 1;
624 this->media_loss_mask_[0] = 1;
625 this->media_loss_mask_[2] = 1;
626 this->media_loss_mask_[3] = 1;
627 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000628
nissea5f043f2017-09-18 07:58:59 -0700629 for (const auto& received_packet : this->received_packets_) {
630 this->fec_.DecodeFec(*received_packet,
631 &this->recovered_packets_);
632 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000633
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000634 // With media packet#1 and FEC packets #1, #2, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700635 EXPECT_TRUE(this->IsRecoveryComplete());
636 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000637
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000638 // 4 consecutive packets lost: media packets 0, 1, 2, 3.
brandtrece4aba2016-09-20 23:16:28 -0700639 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
640 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
641 this->media_loss_mask_[0] = 1;
642 this->media_loss_mask_[1] = 1;
643 this->media_loss_mask_[2] = 1;
644 this->media_loss_mask_[3] = 1;
645 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000646
nissea5f043f2017-09-18 07:58:59 -0700647 for (const auto& received_packet : this->received_packets_) {
648 this->fec_.DecodeFec(*received_packet,
649 &this->recovered_packets_);
650 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000651
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000652 // Cannot get complete recovery for this loss configuration with random mask.
brandtrece4aba2016-09-20 23:16:28 -0700653 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000654}
655
656// Test 50% protection with bursty type: Three cases are considered:
657// two 50% consecutive losses which can be fully recovered, and one
658// non-consecutive which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700659TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percBurstyMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700660 constexpr int kNumImportantPackets = 0;
661 constexpr bool kUseUnequalProtection = false;
662 constexpr int kNumMediaPackets = 4;
663 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000664
665 // Packet Mask for (4,4,0) code, from bursty mask table.
666 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
667
668 // media#0 media#1 media#2 media#3
669 // fec#0: 1 0 0 0
670 // fec#1: 1 1 0 0
671 // fec#2: 0 1 1 0
672 // fec#3: 0 0 1 1
673 //
674
brandtrece4aba2016-09-20 23:16:28 -0700675 this->media_packets_ =
676 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000677
brandtrece4aba2016-09-20 23:16:28 -0700678 EXPECT_EQ(
679 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
680 kNumImportantPackets, kUseUnequalProtection,
681 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000682
683 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700684 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000685
686 // 4 consecutive packets lost: media packets 0,1,2,3.
brandtrece4aba2016-09-20 23:16:28 -0700687 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
688 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
689 this->media_loss_mask_[0] = 1;
690 this->media_loss_mask_[1] = 1;
691 this->media_loss_mask_[2] = 1;
692 this->media_loss_mask_[3] = 1;
693 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000694
nissea5f043f2017-09-18 07:58:59 -0700695 for (const auto& received_packet : this->received_packets_) {
696 this->fec_.DecodeFec(*received_packet,
697 &this->recovered_packets_);
698 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000699
700 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700701 EXPECT_TRUE(this->IsRecoveryComplete());
702 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000703
704 // 4 consecutive packets lost: media packets 1,2, 3, and FEC packet 0.
brandtrece4aba2016-09-20 23:16:28 -0700705 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
706 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
707 this->fec_loss_mask_[0] = 1;
708 this->media_loss_mask_[1] = 1;
709 this->media_loss_mask_[2] = 1;
710 this->media_loss_mask_[3] = 1;
711 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000712
nissea5f043f2017-09-18 07:58:59 -0700713 for (const auto& received_packet : this->received_packets_) {
714 this->fec_.DecodeFec(*received_packet,
715 &this->recovered_packets_);
716 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000717
718 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700719 EXPECT_TRUE(this->IsRecoveryComplete());
720 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000721
722 // 4 packets lost (non-consecutive loss): media packets 0, 3, and FEC# 0, 3.
brandtrece4aba2016-09-20 23:16:28 -0700723 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
724 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
725 this->fec_loss_mask_[0] = 1;
726 this->fec_loss_mask_[3] = 1;
727 this->media_loss_mask_[0] = 1;
728 this->media_loss_mask_[3] = 1;
729 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000730
nissea5f043f2017-09-18 07:58:59 -0700731 for (const auto& received_packet : this->received_packets_) {
732 this->fec_.DecodeFec(*received_packet,
733 &this->recovered_packets_);
734 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000735
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000736 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700737 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000738}
739
brandtrece4aba2016-09-20 23:16:28 -0700740TYPED_TEST(RtpFecTest, FecRecoveryNoLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700741 constexpr int kNumImportantPackets = 2;
742 constexpr bool kUseUnequalProtection = true;
743 constexpr int kNumMediaPackets = 4;
744 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000745
brandtrece4aba2016-09-20 23:16:28 -0700746 this->media_packets_ =
747 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000748
brandtrece4aba2016-09-20 23:16:28 -0700749 EXPECT_EQ(
750 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
751 kNumImportantPackets, kUseUnequalProtection,
752 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000753
754 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700755 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000756
757 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700758 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
759 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
760 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000761
nissea5f043f2017-09-18 07:58:59 -0700762 for (const auto& received_packet : this->received_packets_) {
763 this->fec_.DecodeFec(*received_packet,
764 &this->recovered_packets_);
765 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000766
767 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700768 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000769}
770
brandtrece4aba2016-09-20 23:16:28 -0700771TYPED_TEST(RtpFecTest, FecRecoveryWithLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700772 constexpr int kNumImportantPackets = 2;
773 constexpr bool kUseUnequalProtection = true;
774 constexpr int kNumMediaPackets = 4;
775 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000776
brandtrece4aba2016-09-20 23:16:28 -0700777 this->media_packets_ =
778 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000779
brandtrece4aba2016-09-20 23:16:28 -0700780 EXPECT_EQ(
781 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
782 kNumImportantPackets, kUseUnequalProtection,
783 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000784
785 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700786 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000787
788 // 1 media packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700789 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
790 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
791 this->media_loss_mask_[3] = 1;
792 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000793
nissea5f043f2017-09-18 07:58:59 -0700794 for (const auto& received_packet : this->received_packets_) {
795 this->fec_.DecodeFec(*received_packet,
796 &this->recovered_packets_);
797 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000798
799 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700800 EXPECT_TRUE(this->IsRecoveryComplete());
801 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000802
803 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700804 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
805 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
806 this->media_loss_mask_[1] = 1;
807 this->media_loss_mask_[3] = 1;
808 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000809
nissea5f043f2017-09-18 07:58:59 -0700810 for (const auto& received_packet : this->received_packets_) {
811 this->fec_.DecodeFec(*received_packet,
812 &this->recovered_packets_);
813 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000814
815 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700816 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000817}
818
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000819// Test 50% protection with random mask type for UEP on.
brandtrece4aba2016-09-20 23:16:28 -0700820TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percUepRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700821 constexpr int kNumImportantPackets = 1;
822 constexpr bool kUseUnequalProtection = true;
823 constexpr int kNumMediaPackets = 4;
824 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000825
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000826 // Packet Mask for (4,4,1) code, from random mask table.
827 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 1)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000828
829 // media#0 media#1 media#2 media#3
830 // fec#0: 1 0 0 0
831 // fec#1: 1 1 0 0
832 // fec#2: 1 0 1 1
833 // fec#3: 0 1 1 0
834 //
835
brandtrece4aba2016-09-20 23:16:28 -0700836 this->media_packets_ =
837 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000838
brandtrece4aba2016-09-20 23:16:28 -0700839 EXPECT_EQ(
840 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
841 kNumImportantPackets, kUseUnequalProtection,
842 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000843
844 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700845 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000846
847 // 4 packets lost: 3 media packets and FEC packet#1 lost.
brandtrece4aba2016-09-20 23:16:28 -0700848 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
849 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
850 this->fec_loss_mask_[1] = 1;
851 this->media_loss_mask_[0] = 1;
852 this->media_loss_mask_[2] = 1;
853 this->media_loss_mask_[3] = 1;
854 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000855
nissea5f043f2017-09-18 07:58:59 -0700856 for (const auto& received_packet : this->received_packets_) {
857 this->fec_.DecodeFec(*received_packet,
858 &this->recovered_packets_);
859 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000860
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000861 // With media packet#3 and FEC packets #0, #1, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700862 EXPECT_TRUE(this->IsRecoveryComplete());
863 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000864
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000865 // 5 packets lost: 4 media packets and one FEC packet#2 lost.
brandtrece4aba2016-09-20 23:16:28 -0700866 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
867 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
868 this->fec_loss_mask_[2] = 1;
869 this->media_loss_mask_[0] = 1;
870 this->media_loss_mask_[1] = 1;
871 this->media_loss_mask_[2] = 1;
872 this->media_loss_mask_[3] = 1;
873 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000874
nissea5f043f2017-09-18 07:58:59 -0700875 for (const auto& received_packet : this->received_packets_) {
876 this->fec_.DecodeFec(*received_packet,
877 &this->recovered_packets_);
878 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000879
880 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700881 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000882}
883
brandtrece4aba2016-09-20 23:16:28 -0700884TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePackets) {
brandtrd90fa0b2016-08-09 06:57:14 -0700885 constexpr int kNumImportantPackets = 0;
886 constexpr bool kUseUnequalProtection = false;
887 constexpr int kNumMediaPackets = 5;
888 constexpr uint8_t kProtectionFactor = 60;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000889
brandtrece4aba2016-09-20 23:16:28 -0700890 this->media_packets_ =
891 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000892
893 // Create a new temporary packet list for generating FEC packets.
894 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700895 ForwardErrorCorrection::PacketList protected_media_packets;
896 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000897
brandtrece4aba2016-09-20 23:16:28 -0700898 EXPECT_EQ(
899 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
900 kNumImportantPackets, kUseUnequalProtection,
901 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000902
903 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700904 EXPECT_EQ(1u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000905
906 // 1 protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700907 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
908 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
909 this->media_loss_mask_[2] = 1;
910 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000911
nissea5f043f2017-09-18 07:58:59 -0700912 for (const auto& received_packet : this->received_packets_) {
913 this->fec_.DecodeFec(*received_packet,
914 &this->recovered_packets_);
915 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000916
917 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700918 EXPECT_TRUE(this->IsRecoveryComplete());
919 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000920
921 // Unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700922 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
923 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
924 this->media_loss_mask_[1] = 1;
925 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000926
nissea5f043f2017-09-18 07:58:59 -0700927 for (const auto& received_packet : this->received_packets_) {
928 this->fec_.DecodeFec(*received_packet,
929 &this->recovered_packets_);
930 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000931
932 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -0700933 EXPECT_FALSE(this->IsRecoveryComplete());
934 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000935
936 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700937 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
938 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
939 this->media_loss_mask_[0] = 1;
940 this->media_loss_mask_[2] = 1;
941 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000942
nissea5f043f2017-09-18 07:58:59 -0700943 for (const auto& received_packet : this->received_packets_) {
944 this->fec_.DecodeFec(*received_packet,
945 &this->recovered_packets_);
946 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000947
948 // 2 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700949 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000950}
951
brandtrece4aba2016-09-20 23:16:28 -0700952TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsExtension) {
brandtrd90fa0b2016-08-09 06:57:14 -0700953 constexpr int kNumImportantPackets = 0;
954 constexpr bool kUseUnequalProtection = false;
955 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000956 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000957
brandtrece4aba2016-09-20 23:16:28 -0700958 this->media_packets_ =
959 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000960
961 // Create a new temporary packet list for generating FEC packets.
962 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700963 ForwardErrorCorrection::PacketList protected_media_packets;
964 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000965
966 // Zero column insertion will have to extend the size of the packet
967 // mask since the number of actual packets are 21, while the number
968 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -0700969 EXPECT_EQ(
970 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
971 kNumImportantPackets, kUseUnequalProtection,
972 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000973
974 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700975 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000976
977 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700978 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
979 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
980 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
981 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000982
nissea5f043f2017-09-18 07:58:59 -0700983 for (const auto& received_packet : this->received_packets_) {
984 this->fec_.DecodeFec(*received_packet,
985 &this->recovered_packets_);
986 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000987
988 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700989 EXPECT_TRUE(this->IsRecoveryComplete());
990 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000991
992 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700993 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
994 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
995 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
996 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000997
nissea5f043f2017-09-18 07:58:59 -0700998 for (const auto& received_packet : this->received_packets_) {
999 this->fec_.DecodeFec(*received_packet,
1000 &this->recovered_packets_);
1001 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001002
1003 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001004 EXPECT_FALSE(this->IsRecoveryComplete());
1005 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001006
1007 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001008 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1009 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1010 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1011 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1012 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1013 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1014 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1015 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1016 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001017
nissea5f043f2017-09-18 07:58:59 -07001018 for (const auto& received_packet : this->received_packets_) {
1019 this->fec_.DecodeFec(*received_packet,
1020 &this->recovered_packets_);
1021 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001022
1023 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001024 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001025}
1026
brandtrece4aba2016-09-20 23:16:28 -07001027TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
brandtrd90fa0b2016-08-09 06:57:14 -07001028 constexpr int kNumImportantPackets = 0;
1029 constexpr bool kUseUnequalProtection = false;
1030 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001031 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001032
brandtrece4aba2016-09-20 23:16:28 -07001033 this->media_packets_ = this->media_packet_generator_.ConstructMediaPackets(
1034 kNumMediaPackets, 0xFFFF - 5);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001035
1036 // Create a new temporary packet list for generating FEC packets.
1037 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001038 ForwardErrorCorrection::PacketList protected_media_packets;
1039 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001040
1041 // Zero column insertion will have to extend the size of the packet
1042 // mask since the number of actual packets are 21, while the number
1043 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001044 EXPECT_EQ(
1045 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1046 kNumImportantPackets, kUseUnequalProtection,
1047 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001048
1049 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001050 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001051
1052 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001053 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1054 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1055 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1056 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001057
nissea5f043f2017-09-18 07:58:59 -07001058 for (const auto& received_packet : this->received_packets_) {
1059 this->fec_.DecodeFec(*received_packet,
1060 &this->recovered_packets_);
1061 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001062
1063 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001064 EXPECT_TRUE(this->IsRecoveryComplete());
1065 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001066
1067 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001068 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1069 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1070 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1071 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001072
nissea5f043f2017-09-18 07:58:59 -07001073 for (const auto& received_packet : this->received_packets_) {
1074 this->fec_.DecodeFec(*received_packet,
1075 &this->recovered_packets_);
1076 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001077
1078 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001079 EXPECT_FALSE(this->IsRecoveryComplete());
1080 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001081
1082 // 6 media packets 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 - 11] = 1;
1086 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1087 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1088 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1089 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1090 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1091 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001092
nissea5f043f2017-09-18 07:58:59 -07001093 for (const auto& received_packet : this->received_packets_) {
1094 this->fec_.DecodeFec(*received_packet,
1095 &this->recovered_packets_);
1096 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001097
1098 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001099 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001100}
1101
brandtrece4aba2016-09-20 23:16:28 -07001102} // namespace webrtc