blob: 606dafbbf2a659100ab596746d4f7b54994b7ec5 [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.
Niels Möller958288a2017-10-02 13:51:36 +0200300TYPED_TEST(RtpFecTest, NoFecRecoveryWithOldFecPacket) {
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.
Niels Möller958288a2017-10-02 13:51:36 +0200306 // Third frame (new) with 3 media packets, and no FEC packets.
307 //
308 // #0(media) #1(media) #2(FEC) ----Frame 1-----
309 // #32767(media) 32768(media) 32769(media) ----Frame 2-----
310 // #65535(media) #0(media) #1(media). ----Frame 3-----
311 // If we lose either packet 0 or 1 of third frame, FEC decoding should not
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000312 // try to decode using "old" FEC packet #2.
313
314 // Construct media packets for first frame, starting at sequence number 0.
brandtrece4aba2016-09-20 23:16:28 -0700315 this->media_packets_ =
316 this->media_packet_generator_.ConstructMediaPackets(2, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000317
brandtrece4aba2016-09-20 23:16:28 -0700318 EXPECT_EQ(
319 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
320 kNumImportantPackets, kUseUnequalProtection,
321 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000322 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700323 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000324 // Add FEC packet (seq#2) of this first frame to received list (i.e., assume
325 // the two media packet were lost).
brandtrece4aba2016-09-20 23:16:28 -0700326 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
327 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
328 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000329
330 // Construct media packets for second frame, with sequence number wrap.
brandtrece4aba2016-09-20 23:16:28 -0700331 this->media_packets_ =
Niels Möller958288a2017-10-02 13:51:36 +0200332 this->media_packet_generator_.ConstructMediaPackets(3, 32767);
333
334 // Expect 3 media packets for this frame.
335 EXPECT_EQ(3u, this->media_packets_.size());
336
337 // No packets lost
338 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
339 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
340
341 // Construct media packets for third frame, with sequence number wrap.
342 this->media_packets_ =
brandtrece4aba2016-09-20 23:16:28 -0700343 this->media_packet_generator_.ConstructMediaPackets(3, 65535);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000344
345 // Expect 3 media packets for this frame.
brandtrece4aba2016-09-20 23:16:28 -0700346 EXPECT_EQ(3u, this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000347
348 // Second media packet lost (seq#0).
brandtrece4aba2016-09-20 23:16:28 -0700349 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
350 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000351 // Add packets #65535, and #1 to received list.
brandtrece4aba2016-09-20 23:16:28 -0700352 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000353
nissea5f043f2017-09-18 07:58:59 -0700354 for (const auto& received_packet : this->received_packets_) {
355 this->fec_.DecodeFec(*received_packet,
356 &this->recovered_packets_);
357 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000358
Niels Möller958288a2017-10-02 13:51:36 +0200359 // Expect that no decoding is done to get missing packet (seq#0) of third
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000360 // frame, using old FEC packet (seq#2) from first (old) frame. So number of
Niels Möller958288a2017-10-02 13:51:36 +0200361 // recovered packets is 5 (0 from first frame, three from second frame, and 2
362 // for the third frame, with no packets recovered via FEC).
363 EXPECT_EQ(5u, this->recovered_packets_.size());
brandtrece4aba2016-09-20 23:16:28 -0700364 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000365}
366
brandtrd90fa0b2016-08-09 06:57:14 -0700367// Verify we can still recover frame if sequence number wrap occurs within
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000368// the frame and FEC packet following wrap is received after media packets.
brandtrece4aba2016-09-20 23:16:28 -0700369TYPED_TEST(RtpFecTest, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700370 constexpr int kNumImportantPackets = 0;
371 constexpr bool kUseUnequalProtection = false;
372 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000373
374 // One frame, with sequence number wrap in media packets.
375 // -----Frame 1----
376 // #65534(media) #65535(media) #0(media) #1(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700377 this->media_packets_ =
378 this->media_packet_generator_.ConstructMediaPackets(3, 65534);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000379
brandtrece4aba2016-09-20 23:16:28 -0700380 EXPECT_EQ(
381 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
382 kNumImportantPackets, kUseUnequalProtection,
383 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000384
385 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700386 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000387
388 // Lose one media packet (seq# 65535).
brandtrece4aba2016-09-20 23:16:28 -0700389 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
390 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
391 this->media_loss_mask_[1] = 1;
392 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000393 // Add FEC packet to received list following the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700394 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
395 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000396
nissea5f043f2017-09-18 07:58:59 -0700397 for (const auto& received_packet : this->received_packets_) {
398 this->fec_.DecodeFec(*received_packet,
399 &this->recovered_packets_);
400 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000401
402 // Expect 3 media packets in recovered list, and complete recovery.
403 // Wrap-around won't remove FEC packet, as it follows the wrap.
brandtrece4aba2016-09-20 23:16:28 -0700404 EXPECT_EQ(3u, this->recovered_packets_.size());
405 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000406}
407
brandtrd726a3f2017-06-29 02:45:35 -0700408// Sequence number wrap occurs within the ULPFEC packets for the frame.
brandtrd726a3f2017-06-29 02:45:35 -0700409// Same problem will occur if wrap is within media packets but ULPFEC packet is
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000410// received before the media packets. This may be improved if timing information
brandtrd726a3f2017-06-29 02:45:35 -0700411// is used to detect old ULPFEC packets.
nissea5f043f2017-09-18 07:58:59 -0700412
413// TODO(nisse): There's some logic to discard ULPFEC packets at wrap-around,
414// however, that is not actually exercised by this test: When the first FEC
415// packet is processed, it results in full recovery of one media packet and the
416// FEC packet is forgotten. And then the wraparound isn't noticed when the next
417// FEC packet is received. We should fix wraparound handling, which currently
418// appears broken, and then figure out how to test it properly.
brandtrd726a3f2017-06-29 02:45:35 -0700419using RtpFecTestUlpfecOnly = RtpFecTest<UlpfecForwardErrorCorrection>;
nissea5f043f2017-09-18 07:58:59 -0700420TEST_F(RtpFecTestUlpfecOnly, FecRecoveryWithSeqNumGapOneFrameRecovery) {
brandtrd90fa0b2016-08-09 06:57:14 -0700421 constexpr int kNumImportantPackets = 0;
422 constexpr bool kUseUnequalProtection = false;
423 constexpr uint8_t kProtectionFactor = 200;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000424
425 // 1 frame: 3 media packets and 2 FEC packets.
426 // Sequence number wrap in FEC packets.
427 // -----Frame 1----
428 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700429 this->media_packets_ =
430 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000431
brandtrece4aba2016-09-20 23:16:28 -0700432 EXPECT_EQ(
433 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
434 kNumImportantPackets, kUseUnequalProtection,
435 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000436
437 // Expect 2 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700438 EXPECT_EQ(2u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000439
440 // Lose the last two media packets (seq# 65533, 65534).
brandtrece4aba2016-09-20 23:16:28 -0700441 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
442 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
443 this->media_loss_mask_[1] = 1;
444 this->media_loss_mask_[2] = 1;
445 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
446 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
447 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000448
nissea5f043f2017-09-18 07:58:59 -0700449 for (const auto& received_packet : this->received_packets_) {
450 this->fec_.DecodeFec(*received_packet,
451 &this->recovered_packets_);
452 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000453
454 // The two FEC packets are received and should allow for complete recovery,
brandtrd726a3f2017-06-29 02:45:35 -0700455 // but because of the wrap the first FEC packet will be discarded, and only
456 // one media packet is recoverable. So expect 2 media packets on recovered
457 // list and no complete recovery.
nissea5f043f2017-09-18 07:58:59 -0700458 EXPECT_EQ(3u, this->recovered_packets_.size());
459 EXPECT_EQ(this->recovered_packets_.size(), this->media_packets_.size());
460 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd726a3f2017-06-29 02:45:35 -0700461}
462
463// TODO(brandtr): This test mimics the one above, ensuring that the recovery
464// strategy of FlexFEC matches the recovery strategy of ULPFEC. Since FlexFEC
465// does not share the sequence number space with the media, however, having a
466// matching recovery strategy may be suboptimal. Study this further.
nissea5f043f2017-09-18 07:58:59 -0700467// TODO(nisse): In this test, recovery based on the first FEC packet fails with
468// the log message "The recovered packet had a length larger than a typical IP
469// packet, and is thus dropped." This is probably not intended, and needs
470// investigation.
brandtrd726a3f2017-06-29 02:45:35 -0700471using RtpFecTestFlexfecOnly = RtpFecTest<FlexfecForwardErrorCorrection>;
472TEST_F(RtpFecTestFlexfecOnly, FecRecoveryWithSeqNumGapOneFrameNoRecovery) {
473 constexpr int kNumImportantPackets = 0;
474 constexpr bool kUseUnequalProtection = false;
475 constexpr uint8_t kProtectionFactor = 200;
476
477 // 1 frame: 3 media packets and 2 FEC packets.
478 // Sequence number wrap in FEC packets.
479 // -----Frame 1----
480 // #65532(media) #65533(media) #65534(media) #65535(FEC) #0(FEC).
481 this->media_packets_ =
482 this->media_packet_generator_.ConstructMediaPackets(3, 65532);
483
484 EXPECT_EQ(
485 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
486 kNumImportantPackets, kUseUnequalProtection,
487 kFecMaskBursty, &this->generated_fec_packets_));
488
489 // Expect 2 FEC packets.
490 EXPECT_EQ(2u, this->generated_fec_packets_.size());
491
492 // Overwrite the sequence numbers generated by ConstructMediaPackets,
493 // to make sure that we do have a wrap.
494 auto it = this->generated_fec_packets_.begin();
495 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 65535);
496 ++it;
497 ByteWriter<uint16_t>::WriteBigEndian(&(*it)->data[2], 0);
498
499 // Lose the last two media packets (seq# 65533, 65534).
500 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
501 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
502 this->media_loss_mask_[1] = 1;
503 this->media_loss_mask_[2] = 1;
504 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
505 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
506 true);
507
nissea5f043f2017-09-18 07:58:59 -0700508 for (const auto& received_packet : this->received_packets_) {
509 this->fec_.DecodeFec(*received_packet,
510 &this->recovered_packets_);
511 }
brandtrd726a3f2017-06-29 02:45:35 -0700512
513 // The two FEC packets are received and should allow for complete recovery,
514 // but because of the wrap the first FEC packet will be discarded, and only
515 // one media packet is recoverable. So expect 2 media packets on recovered
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000516 // list and no complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700517 EXPECT_EQ(2u, this->recovered_packets_.size());
518 EXPECT_TRUE(this->recovered_packets_.size() != this->media_packets_.size());
519 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000520}
521
brandtrd90fa0b2016-08-09 06:57:14 -0700522// Verify we can still recover frame if media packets are reordered.
brandtrece4aba2016-09-20 23:16:28 -0700523TYPED_TEST(RtpFecTest, FecRecoveryWithMediaOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700524 constexpr int kNumImportantPackets = 0;
525 constexpr bool kUseUnequalProtection = false;
526 constexpr uint8_t kProtectionFactor = 20;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000527
528 // One frame: 3 media packets, 1 FEC packet.
529 // -----Frame 1----
530 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700531 this->media_packets_ =
532 this->media_packet_generator_.ConstructMediaPackets(3, 0);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000533
brandtrece4aba2016-09-20 23:16:28 -0700534 EXPECT_EQ(
535 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
536 kNumImportantPackets, kUseUnequalProtection,
537 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000538
539 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700540 EXPECT_EQ(1u, this->generated_fec_packets_.size());
brandtrd90fa0b2016-08-09 06:57:14 -0700541
542 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700543 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
544 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
545 this->media_loss_mask_[1] = 1;
546 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
brandtrd90fa0b2016-08-09 06:57:14 -0700547
548 // Reorder received media packets.
brandtrece4aba2016-09-20 23:16:28 -0700549 auto it0 = this->received_packets_.begin();
brandtrd726a3f2017-06-29 02:45:35 -0700550 auto it1 = this->received_packets_.begin();
551 it1++;
552 std::swap(*it0, *it1);
brandtrd90fa0b2016-08-09 06:57:14 -0700553
nissea5f043f2017-09-18 07:58:59 -0700554 for (const auto& received_packet : this->received_packets_) {
555 this->fec_.DecodeFec(*received_packet,
556 &this->recovered_packets_);
557 }
brandtrd90fa0b2016-08-09 06:57:14 -0700558
559 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700560 EXPECT_EQ(3u, this->recovered_packets_.size());
561 EXPECT_TRUE(this->IsRecoveryComplete());
brandtrd90fa0b2016-08-09 06:57:14 -0700562}
563
564// Verify we can still recover frame if FEC is received before media packets.
brandtrece4aba2016-09-20 23:16:28 -0700565TYPED_TEST(RtpFecTest, FecRecoveryWithFecOutOfOrder) {
brandtrd90fa0b2016-08-09 06:57:14 -0700566 constexpr int kNumImportantPackets = 0;
567 constexpr bool kUseUnequalProtection = false;
568 constexpr uint8_t kProtectionFactor = 20;
569
570 // One frame: 3 media packets, 1 FEC packet.
571 // -----Frame 1----
572 // #0(media) #1(media) #2(media) #3(FEC).
brandtrece4aba2016-09-20 23:16:28 -0700573 this->media_packets_ =
574 this->media_packet_generator_.ConstructMediaPackets(3, 0);
brandtrd90fa0b2016-08-09 06:57:14 -0700575
brandtrece4aba2016-09-20 23:16:28 -0700576 EXPECT_EQ(
577 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
578 kNumImportantPackets, kUseUnequalProtection,
579 kFecMaskBursty, &this->generated_fec_packets_));
brandtrd90fa0b2016-08-09 06:57:14 -0700580
581 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700582 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000583
584 // Lose one media packet (seq# 1).
brandtrece4aba2016-09-20 23:16:28 -0700585 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
586 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
587 this->media_loss_mask_[1] = 1;
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000588 // Add FEC packet to received list before the media packets.
brandtrece4aba2016-09-20 23:16:28 -0700589 this->ReceivedPackets(this->generated_fec_packets_, this->fec_loss_mask_,
590 true);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000591 // Add media packets to received list.
brandtrece4aba2016-09-20 23:16:28 -0700592 this->ReceivedPackets(this->media_packets_, this->media_loss_mask_, false);
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000593
nissea5f043f2017-09-18 07:58:59 -0700594 for (const auto& received_packet : this->received_packets_) {
595 this->fec_.DecodeFec(*received_packet,
596 &this->recovered_packets_);
597 }
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000598
599 // Expect 3 media packets in recovered list, and complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700600 EXPECT_EQ(3u, this->recovered_packets_.size());
601 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgc8364532014-07-03 16:49:30 +0000602}
603
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000604// Test 50% protection with random mask type: Two cases are considered:
605// a 50% non-consecutive loss which can be fully recovered, and a 50%
606// consecutive loss which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700607TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700608 constexpr int kNumImportantPackets = 0;
609 constexpr bool kUseUnequalProtection = false;
610 constexpr int kNumMediaPackets = 4;
611 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000612
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000613 // Packet Mask for (4,4,0) code, from random mask table.
614 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000615
616 // media#0 media#1 media#2 media#3
617 // fec#0: 1 1 0 0
618 // fec#1: 1 0 1 0
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000619 // fec#2: 0 0 1 1
620 // fec#3: 0 1 0 1
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000621 //
622
brandtrece4aba2016-09-20 23:16:28 -0700623 this->media_packets_ =
624 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000625
brandtrece4aba2016-09-20 23:16:28 -0700626 EXPECT_EQ(
627 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
628 kNumImportantPackets, kUseUnequalProtection,
629 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000630
631 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700632 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000633
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000634 // 4 packets lost: 3 media packets (0, 2, 3), and one FEC packet (0) lost.
brandtrece4aba2016-09-20 23:16:28 -0700635 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
636 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
637 this->fec_loss_mask_[0] = 1;
638 this->media_loss_mask_[0] = 1;
639 this->media_loss_mask_[2] = 1;
640 this->media_loss_mask_[3] = 1;
641 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000642
nissea5f043f2017-09-18 07:58:59 -0700643 for (const auto& received_packet : this->received_packets_) {
644 this->fec_.DecodeFec(*received_packet,
645 &this->recovered_packets_);
646 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000647
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000648 // With media packet#1 and FEC packets #1, #2, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700649 EXPECT_TRUE(this->IsRecoveryComplete());
650 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000651
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000652 // 4 consecutive packets lost: media packets 0, 1, 2, 3.
brandtrece4aba2016-09-20 23:16:28 -0700653 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
654 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
655 this->media_loss_mask_[0] = 1;
656 this->media_loss_mask_[1] = 1;
657 this->media_loss_mask_[2] = 1;
658 this->media_loss_mask_[3] = 1;
659 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000660
nissea5f043f2017-09-18 07:58:59 -0700661 for (const auto& received_packet : this->received_packets_) {
662 this->fec_.DecodeFec(*received_packet,
663 &this->recovered_packets_);
664 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000665
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000666 // Cannot get complete recovery for this loss configuration with random mask.
brandtrece4aba2016-09-20 23:16:28 -0700667 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000668}
669
670// Test 50% protection with bursty type: Three cases are considered:
671// two 50% consecutive losses which can be fully recovered, and one
672// non-consecutive which cannot be fully recovered.
brandtrece4aba2016-09-20 23:16:28 -0700673TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percBurstyMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700674 constexpr int kNumImportantPackets = 0;
675 constexpr bool kUseUnequalProtection = false;
676 constexpr int kNumMediaPackets = 4;
677 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000678
679 // Packet Mask for (4,4,0) code, from bursty mask table.
680 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 0)
681
682 // media#0 media#1 media#2 media#3
683 // fec#0: 1 0 0 0
684 // fec#1: 1 1 0 0
685 // fec#2: 0 1 1 0
686 // fec#3: 0 0 1 1
687 //
688
brandtrece4aba2016-09-20 23:16:28 -0700689 this->media_packets_ =
690 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000691
brandtrece4aba2016-09-20 23:16:28 -0700692 EXPECT_EQ(
693 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
694 kNumImportantPackets, kUseUnequalProtection,
695 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000696
697 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700698 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000699
700 // 4 consecutive packets lost: media packets 0,1,2,3.
brandtrece4aba2016-09-20 23:16:28 -0700701 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
702 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
703 this->media_loss_mask_[0] = 1;
704 this->media_loss_mask_[1] = 1;
705 this->media_loss_mask_[2] = 1;
706 this->media_loss_mask_[3] = 1;
707 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000708
nissea5f043f2017-09-18 07:58:59 -0700709 for (const auto& received_packet : this->received_packets_) {
710 this->fec_.DecodeFec(*received_packet,
711 &this->recovered_packets_);
712 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000713
714 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700715 EXPECT_TRUE(this->IsRecoveryComplete());
716 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000717
718 // 4 consecutive packets lost: media packets 1,2, 3, and FEC packet 0.
brandtrece4aba2016-09-20 23:16:28 -0700719 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
720 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
721 this->fec_loss_mask_[0] = 1;
722 this->media_loss_mask_[1] = 1;
723 this->media_loss_mask_[2] = 1;
724 this->media_loss_mask_[3] = 1;
725 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000726
nissea5f043f2017-09-18 07:58:59 -0700727 for (const auto& received_packet : this->received_packets_) {
728 this->fec_.DecodeFec(*received_packet,
729 &this->recovered_packets_);
730 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000731
732 // Expect complete recovery for consecutive packet loss <= 50%.
brandtrece4aba2016-09-20 23:16:28 -0700733 EXPECT_TRUE(this->IsRecoveryComplete());
734 this->recovered_packets_.clear();
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000735
736 // 4 packets lost (non-consecutive loss): media packets 0, 3, and FEC# 0, 3.
brandtrece4aba2016-09-20 23:16:28 -0700737 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
738 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
739 this->fec_loss_mask_[0] = 1;
740 this->fec_loss_mask_[3] = 1;
741 this->media_loss_mask_[0] = 1;
742 this->media_loss_mask_[3] = 1;
743 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000744
nissea5f043f2017-09-18 07:58:59 -0700745 for (const auto& received_packet : this->received_packets_) {
746 this->fec_.DecodeFec(*received_packet,
747 &this->recovered_packets_);
748 }
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000749
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000750 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700751 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000752}
753
brandtrece4aba2016-09-20 23:16:28 -0700754TYPED_TEST(RtpFecTest, FecRecoveryNoLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700755 constexpr int kNumImportantPackets = 2;
756 constexpr bool kUseUnequalProtection = true;
757 constexpr int kNumMediaPackets = 4;
758 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000759
brandtrece4aba2016-09-20 23:16:28 -0700760 this->media_packets_ =
761 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000762
brandtrece4aba2016-09-20 23:16:28 -0700763 EXPECT_EQ(
764 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
765 kNumImportantPackets, kUseUnequalProtection,
766 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000767
768 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700769 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000770
771 // No packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700772 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
773 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
774 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000775
nissea5f043f2017-09-18 07:58:59 -0700776 for (const auto& received_packet : this->received_packets_) {
777 this->fec_.DecodeFec(*received_packet,
778 &this->recovered_packets_);
779 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000780
781 // No packets lost, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700782 EXPECT_TRUE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000783}
784
brandtrece4aba2016-09-20 23:16:28 -0700785TYPED_TEST(RtpFecTest, FecRecoveryWithLossUep) {
brandtrd90fa0b2016-08-09 06:57:14 -0700786 constexpr int kNumImportantPackets = 2;
787 constexpr bool kUseUnequalProtection = true;
788 constexpr int kNumMediaPackets = 4;
789 constexpr uint8_t kProtectionFactor = 60;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000790
brandtrece4aba2016-09-20 23:16:28 -0700791 this->media_packets_ =
792 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000793
brandtrece4aba2016-09-20 23:16:28 -0700794 EXPECT_EQ(
795 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
796 kNumImportantPackets, kUseUnequalProtection,
797 kFecMaskBursty, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000798
799 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700800 EXPECT_EQ(1u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000801
802 // 1 media packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700803 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
804 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
805 this->media_loss_mask_[3] = 1;
806 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000807
nissea5f043f2017-09-18 07:58:59 -0700808 for (const auto& received_packet : this->received_packets_) {
809 this->fec_.DecodeFec(*received_packet,
810 &this->recovered_packets_);
811 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000812
813 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700814 EXPECT_TRUE(this->IsRecoveryComplete());
815 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000816
817 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700818 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
819 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
820 this->media_loss_mask_[1] = 1;
821 this->media_loss_mask_[3] = 1;
822 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000823
nissea5f043f2017-09-18 07:58:59 -0700824 for (const auto& received_packet : this->received_packets_) {
825 this->fec_.DecodeFec(*received_packet,
826 &this->recovered_packets_);
827 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000828
829 // 2 packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700830 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000831}
832
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000833// Test 50% protection with random mask type for UEP on.
brandtrece4aba2016-09-20 23:16:28 -0700834TYPED_TEST(RtpFecTest, FecRecoveryWithLoss50percUepRandomMask) {
brandtrd90fa0b2016-08-09 06:57:14 -0700835 constexpr int kNumImportantPackets = 1;
836 constexpr bool kUseUnequalProtection = true;
837 constexpr int kNumMediaPackets = 4;
838 constexpr uint8_t kProtectionFactor = 255;
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000839
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000840 // Packet Mask for (4,4,1) code, from random mask table.
841 // (kNumMediaPackets = 4; num_fec_packets = 4, kNumImportantPackets = 1)
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000842
843 // media#0 media#1 media#2 media#3
844 // fec#0: 1 0 0 0
845 // fec#1: 1 1 0 0
846 // fec#2: 1 0 1 1
847 // fec#3: 0 1 1 0
848 //
849
brandtrece4aba2016-09-20 23:16:28 -0700850 this->media_packets_ =
851 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000852
brandtrece4aba2016-09-20 23:16:28 -0700853 EXPECT_EQ(
854 0, this->fec_.EncodeFec(this->media_packets_, kProtectionFactor,
855 kNumImportantPackets, kUseUnequalProtection,
856 kFecMaskRandom, &this->generated_fec_packets_));
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000857
858 // Expect 4 FEC packets.
brandtrece4aba2016-09-20 23:16:28 -0700859 EXPECT_EQ(4u, this->generated_fec_packets_.size());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000860
861 // 4 packets lost: 3 media packets and FEC packet#1 lost.
brandtrece4aba2016-09-20 23:16:28 -0700862 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
863 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
864 this->fec_loss_mask_[1] = 1;
865 this->media_loss_mask_[0] = 1;
866 this->media_loss_mask_[2] = 1;
867 this->media_loss_mask_[3] = 1;
868 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000869
nissea5f043f2017-09-18 07:58:59 -0700870 for (const auto& received_packet : this->received_packets_) {
871 this->fec_.DecodeFec(*received_packet,
872 &this->recovered_packets_);
873 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000874
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000875 // With media packet#3 and FEC packets #0, #1, #3, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700876 EXPECT_TRUE(this->IsRecoveryComplete());
877 this->recovered_packets_.clear();
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000878
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000879 // 5 packets lost: 4 media packets and one FEC packet#2 lost.
brandtrece4aba2016-09-20 23:16:28 -0700880 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
881 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
882 this->fec_loss_mask_[2] = 1;
883 this->media_loss_mask_[0] = 1;
884 this->media_loss_mask_[1] = 1;
885 this->media_loss_mask_[2] = 1;
886 this->media_loss_mask_[3] = 1;
887 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000888
nissea5f043f2017-09-18 07:58:59 -0700889 for (const auto& received_packet : this->received_packets_) {
890 this->fec_.DecodeFec(*received_packet,
891 &this->recovered_packets_);
892 }
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000893
894 // Cannot get complete recovery for this loss configuration.
brandtrece4aba2016-09-20 23:16:28 -0700895 EXPECT_FALSE(this->IsRecoveryComplete());
marpan@webrtc.orgb783a552012-02-04 02:46:35 +0000896}
897
brandtrece4aba2016-09-20 23:16:28 -0700898TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePackets) {
brandtrd90fa0b2016-08-09 06:57:14 -0700899 constexpr int kNumImportantPackets = 0;
900 constexpr bool kUseUnequalProtection = false;
901 constexpr int kNumMediaPackets = 5;
902 constexpr uint8_t kProtectionFactor = 60;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000903
brandtrece4aba2016-09-20 23:16:28 -0700904 this->media_packets_ =
905 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000906
907 // Create a new temporary packet list for generating FEC packets.
908 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700909 ForwardErrorCorrection::PacketList protected_media_packets;
910 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000911
brandtrece4aba2016-09-20 23:16:28 -0700912 EXPECT_EQ(
913 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
914 kNumImportantPackets, kUseUnequalProtection,
915 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000916
917 // Expect 1 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700918 EXPECT_EQ(1u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000919
920 // 1 protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700921 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
922 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
923 this->media_loss_mask_[2] = 1;
924 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000925
nissea5f043f2017-09-18 07:58:59 -0700926 for (const auto& received_packet : this->received_packets_) {
927 this->fec_.DecodeFec(*received_packet,
928 &this->recovered_packets_);
929 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000930
931 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700932 EXPECT_TRUE(this->IsRecoveryComplete());
933 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000934
935 // Unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -0700936 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
937 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
938 this->media_loss_mask_[1] = 1;
939 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000940
nissea5f043f2017-09-18 07:58:59 -0700941 for (const auto& received_packet : this->received_packets_) {
942 this->fec_.DecodeFec(*received_packet,
943 &this->recovered_packets_);
944 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000945
946 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -0700947 EXPECT_FALSE(this->IsRecoveryComplete());
948 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000949
950 // 2 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -0700951 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
952 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
953 this->media_loss_mask_[0] = 1;
954 this->media_loss_mask_[2] = 1;
955 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000956
nissea5f043f2017-09-18 07:58:59 -0700957 for (const auto& received_packet : this->received_packets_) {
958 this->fec_.DecodeFec(*received_packet,
959 &this->recovered_packets_);
960 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000961
962 // 2 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -0700963 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000964}
965
brandtrece4aba2016-09-20 23:16:28 -0700966TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsExtension) {
brandtrd90fa0b2016-08-09 06:57:14 -0700967 constexpr int kNumImportantPackets = 0;
968 constexpr bool kUseUnequalProtection = false;
969 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +0000970 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000971
brandtrece4aba2016-09-20 23:16:28 -0700972 this->media_packets_ =
973 this->media_packet_generator_.ConstructMediaPackets(kNumMediaPackets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000974
975 // Create a new temporary packet list for generating FEC packets.
976 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -0700977 ForwardErrorCorrection::PacketList protected_media_packets;
978 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000979
980 // Zero column insertion will have to extend the size of the packet
981 // mask since the number of actual packets are 21, while the number
982 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -0700983 EXPECT_EQ(
984 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
985 kNumImportantPackets, kUseUnequalProtection,
986 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000987
988 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -0700989 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000990
991 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -0700992 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
993 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
994 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
995 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000996
nissea5f043f2017-09-18 07:58:59 -0700997 for (const auto& received_packet : this->received_packets_) {
998 this->fec_.DecodeFec(*received_packet,
999 &this->recovered_packets_);
1000 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001001
1002 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001003 EXPECT_TRUE(this->IsRecoveryComplete());
1004 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001005
1006 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001007 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1008 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1009 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1010 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001011
nissea5f043f2017-09-18 07:58:59 -07001012 for (const auto& received_packet : this->received_packets_) {
1013 this->fec_.DecodeFec(*received_packet,
1014 &this->recovered_packets_);
1015 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001016
1017 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001018 EXPECT_FALSE(this->IsRecoveryComplete());
1019 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001020
1021 // 6 media packets lost.
brandtrece4aba2016-09-20 23:16:28 -07001022 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1023 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1024 this->media_loss_mask_[kNumMediaPackets - 11] = 1;
1025 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1026 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1027 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1028 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1029 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1030 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001031
nissea5f043f2017-09-18 07:58:59 -07001032 for (const auto& received_packet : this->received_packets_) {
1033 this->fec_.DecodeFec(*received_packet,
1034 &this->recovered_packets_);
1035 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001036
1037 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001038 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001039}
1040
brandtrece4aba2016-09-20 23:16:28 -07001041TYPED_TEST(RtpFecTest, FecRecoveryNonConsecutivePacketsWrap) {
brandtrd90fa0b2016-08-09 06:57:14 -07001042 constexpr int kNumImportantPackets = 0;
1043 constexpr bool kUseUnequalProtection = false;
1044 constexpr int kNumMediaPackets = 21;
marpan@webrtc.org8866bb12012-06-05 16:42:20 +00001045 uint8_t kProtectionFactor = 127;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001046
brandtrece4aba2016-09-20 23:16:28 -07001047 this->media_packets_ = this->media_packet_generator_.ConstructMediaPackets(
1048 kNumMediaPackets, 0xFFFF - 5);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001049
1050 // Create a new temporary packet list for generating FEC packets.
1051 // This list should have every other packet removed.
brandtrece4aba2016-09-20 23:16:28 -07001052 ForwardErrorCorrection::PacketList protected_media_packets;
1053 DeepCopyEveryNthPacket(this->media_packets_, 2, &protected_media_packets);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001054
1055 // Zero column insertion will have to extend the size of the packet
1056 // mask since the number of actual packets are 21, while the number
1057 // of protected packets are 11.
brandtrece4aba2016-09-20 23:16:28 -07001058 EXPECT_EQ(
1059 0, this->fec_.EncodeFec(protected_media_packets, kProtectionFactor,
1060 kNumImportantPackets, kUseUnequalProtection,
1061 kFecMaskBursty, &this->generated_fec_packets_));
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001062
1063 // Expect 5 FEC packet.
brandtrece4aba2016-09-20 23:16:28 -07001064 EXPECT_EQ(5u, this->generated_fec_packets_.size());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001065
1066 // Last protected media packet lost
brandtrece4aba2016-09-20 23:16:28 -07001067 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1068 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1069 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1070 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001071
nissea5f043f2017-09-18 07:58:59 -07001072 for (const auto& received_packet : this->received_packets_) {
1073 this->fec_.DecodeFec(*received_packet,
1074 &this->recovered_packets_);
1075 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001076
1077 // One packet lost, one FEC packet, expect complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001078 EXPECT_TRUE(this->IsRecoveryComplete());
1079 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001080
1081 // Last unprotected packet lost.
brandtrece4aba2016-09-20 23:16:28 -07001082 memset(this->media_loss_mask_, 0, sizeof(this->media_loss_mask_));
1083 memset(this->fec_loss_mask_, 0, sizeof(this->fec_loss_mask_));
1084 this->media_loss_mask_[kNumMediaPackets - 2] = 1;
1085 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001086
nissea5f043f2017-09-18 07:58:59 -07001087 for (const auto& received_packet : this->received_packets_) {
1088 this->fec_.DecodeFec(*received_packet,
1089 &this->recovered_packets_);
1090 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001091
1092 // Unprotected packet lost. Recovery not possible.
brandtrece4aba2016-09-20 23:16:28 -07001093 EXPECT_FALSE(this->IsRecoveryComplete());
1094 this->recovered_packets_.clear();
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001095
1096 // 6 media packets 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 - 11] = 1;
1100 this->media_loss_mask_[kNumMediaPackets - 9] = 1;
1101 this->media_loss_mask_[kNumMediaPackets - 7] = 1;
1102 this->media_loss_mask_[kNumMediaPackets - 5] = 1;
1103 this->media_loss_mask_[kNumMediaPackets - 3] = 1;
1104 this->media_loss_mask_[kNumMediaPackets - 1] = 1;
1105 this->NetworkReceivedPackets(this->media_loss_mask_, this->fec_loss_mask_);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001106
nissea5f043f2017-09-18 07:58:59 -07001107 for (const auto& received_packet : this->received_packets_) {
1108 this->fec_.DecodeFec(*received_packet,
1109 &this->recovered_packets_);
1110 }
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001111
1112 // 5 protected packets lost, one FEC packet, cannot get complete recovery.
brandtrece4aba2016-09-20 23:16:28 -07001113 EXPECT_FALSE(this->IsRecoveryComplete());
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +00001114}
1115
brandtrece4aba2016-09-20 23:16:28 -07001116} // namespace webrtc