1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <vector>
#include "gtest/gtest.h"
#include "packet.h"
#include "fec.h"
#include "packetfilter.h"
#include "packetfilter_api.h"
using namespace std;
class TestFECRebuilding: public testing::Test
{
protected:
FECFilterBuiltin* fec = nullptr;
vector<SrtPacket> provided;
vector<unique_ptr<CPacket>> source;
int sockid = 54321;
int isn = 123456;
size_t plsize = 1316;
TestFECRebuilding()
{
// Required to make ParseCorrectorConfig work
PacketFilter::globalInit();
}
void SetUp() override
{
int timestamp = 10;
SrtFilterInitializer init = {
sockid,
isn - 1, // It's passed in this form to PacketFilter constructor, it should increase it
isn - 1, // XXX Probably this better be changed.
plsize
};
// Make configuration row-only with size 7
string conf = "fec,rows:1,cols:7";
provided.clear();
fec = new FECFilterBuiltin(init, provided, conf);
int32_t seq = isn;
for (int i = 0; i < 7; ++i)
{
source.emplace_back(new CPacket);
CPacket& p = *source.back();
p.allocate(SRT_LIVE_MAX_PLSIZE);
uint32_t* hdr = p.getHeader();
// Fill in the values
hdr[SRT_PH_SEQNO] = seq;
hdr[SRT_PH_MSGNO] = 1 | MSGNO_PACKET_BOUNDARY::wrap(PB_SOLO);
hdr[SRT_PH_ID] = sockid;
hdr[SRT_PH_TIMESTAMP] = timestamp;
// Fill in the contents.
// Randomly chose the size
int minsize = 732;
int divergence = plsize - minsize - 1;
size_t length = minsize + rand() % divergence;
p.setLength(length);
for (size_t b = 0; b < length; ++b)
{
p.data()[b] = rand() % 255;
}
timestamp += 10;
seq = CSeqNo::incseq(seq);
}
}
void TearDown() override
{
delete fec;
}
};
TEST_F(TestFECRebuilding, Prepare)
{
// Stuff in prepared packets into the source fec.
int32_t seq;
for (int i = 0; i < 7; ++i)
{
CPacket& p = *source[i].get();
// Feed it simultaneously into the sender FEC
fec->feedSource(p);
seq = p.getSeqNo();
}
SrtPacket fec_ctl(SRT_LIVE_MAX_PLSIZE);
// Use the sequence number of the last packet, as usual.
bool have_fec_ctl = fec->packControlPacket(fec_ctl, seq);
EXPECT_EQ(have_fec_ctl, true);
}
TEST_F(TestFECRebuilding, NoRebuild)
{
// Stuff in prepared packets into the source fec.
int32_t seq;
for (int i = 0; i < 7; ++i)
{
CPacket& p = *source[i].get();
// Feed it simultaneously into the sender FEC
fec->feedSource(p);
seq = p.getSeqNo();
}
SrtPacket fec_ctl(SRT_LIVE_MAX_PLSIZE);
// Use the sequence number of the last packet, as usual.
bool have_fec_ctl = fec->packControlPacket(fec_ctl, seq);
ASSERT_EQ(have_fec_ctl, true);
// By having all packets and FEC CTL packet, now stuff in
// these packets into the receiver
FECFilterBuiltin::loss_seqs_t loss; // required as return, ignore
for (int i = 0; i < 7; ++i)
{
// SKIP packet 4 to simulate loss
if (i == 4 || i == 6)
continue;
// Stuff in the packet into the FEC filter
bool want_passthru = fec->receive(*source[i], loss);
EXPECT_EQ(want_passthru, true);
}
// Prepare a real packet basing on the SrtPacket.
// XXX Consider packing this into a callable function as this
// is a code directly copied from PacketFilter::packControlPacket.
unique_ptr<CPacket> fecpkt ( new CPacket );
uint32_t* chdr = fecpkt->getHeader();
memcpy(chdr, fec_ctl.hdr, SRT_PH_E_SIZE * sizeof(*chdr));
// The buffer can be assigned.
fecpkt->m_pcData = fec_ctl.buffer;
fecpkt->setLength(fec_ctl.length);
// This sets only the Packet Boundary flags, while all other things:
// - Order
// - Rexmit
// - Crypto
// - Message Number
// will be set to 0/false
fecpkt->m_iMsgNo = MSGNO_PACKET_BOUNDARY::wrap(PB_SOLO);
// ... and then fix only the Crypto flags
fecpkt->setMsgCryptoFlags(EncryptionKeySpec(0));
// And now receive the FEC control packet
bool want_passthru_fec = fec->receive(*fecpkt, loss);
EXPECT_EQ(want_passthru_fec, false); // Confirm that it's been eaten up
EXPECT_EQ(provided.size(), 0); // Confirm that nothing was rebuilt
/*
// XXX With such a short sequence, losses will not be reported.
// You need at least one packet past the row, even in 1-row config.
// Probably a better way for loss collection should be devised.
ASSERT_EQ(loss.size(), 2);
EXPECT_EQ(loss[0].first, isn + 4);
EXPECT_EQ(loss[1].first, isn + 6);
*/
}
TEST_F(TestFECRebuilding, Rebuild)
{
// Stuff in prepared packets into the source fec->
int32_t seq;
for (int i = 0; i < 7; ++i)
{
CPacket& p = *source[i].get();
// Feed it simultaneously into the sender FEC
fec->feedSource(p);
seq = p.getSeqNo();
}
SrtPacket fec_ctl(SRT_LIVE_MAX_PLSIZE);
// Use the sequence number of the last packet, as usual.
bool have_fec_ctl = fec->packControlPacket(fec_ctl, seq);
ASSERT_EQ(have_fec_ctl, true);
// By having all packets and FEC CTL packet, now stuff in
// these packets into the receiver
FECFilterBuiltin::loss_seqs_t loss; // required as return, ignore
for (int i = 0; i < 7; ++i)
{
// SKIP packet 4 to simulate loss
if (i == 4)
continue;
// Stuff in the packet into the FEC filter
bool want_passthru = fec->receive(*source[i], loss);
EXPECT_EQ(want_passthru, true);
}
// Prepare a real packet basing on the SrtPacket.
// XXX Consider packing this into a callable function as this
// is a code directly copied from PacketFilter::packControlPacket.
unique_ptr<CPacket> fecpkt ( new CPacket );
uint32_t* chdr = fecpkt->getHeader();
memcpy(chdr, fec_ctl.hdr, SRT_PH_E_SIZE * sizeof(*chdr));
// The buffer can be assigned.
fecpkt->m_pcData = fec_ctl.buffer;
fecpkt->setLength(fec_ctl.length);
// This sets only the Packet Boundary flags, while all other things:
// - Order
// - Rexmit
// - Crypto
// - Message Number
// will be set to 0/false
fecpkt->m_iMsgNo = MSGNO_PACKET_BOUNDARY::wrap(PB_SOLO);
// ... and then fix only the Crypto flags
fecpkt->setMsgCryptoFlags(EncryptionKeySpec(0));
// And now receive the FEC control packet
bool want_passthru_fec = fec->receive(*fecpkt, loss);
EXPECT_EQ(want_passthru_fec, false); // Confirm that it's been eaten up
EXPECT_EQ(loss.size(), 0);
ASSERT_EQ(provided.size(), 1);
SrtPacket& rebuilt = provided[0];
CPacket& skipped = *source[4];
// Set artificially the SN_REXMIT flag in the skipped source packet
// because the rebuilt packet shall have REXMIT flag set.
skipped.m_iMsgNo |= MSGNO_REXMIT::wrap(true);
// Compare the header
EXPECT_EQ(skipped.getHeader()[SRT_PH_SEQNO], rebuilt.hdr[SRT_PH_SEQNO]);
EXPECT_EQ(skipped.getHeader()[SRT_PH_MSGNO], rebuilt.hdr[SRT_PH_MSGNO]);
EXPECT_EQ(skipped.getHeader()[SRT_PH_ID], rebuilt.hdr[SRT_PH_ID]);
EXPECT_EQ(skipped.getHeader()[SRT_PH_TIMESTAMP], rebuilt.hdr[SRT_PH_TIMESTAMP]);
// Compare sizes and contents
ASSERT_EQ(skipped.size(), rebuilt.size());
EXPECT_EQ(memcmp(skipped.data(), rebuilt.data(), rebuilt.size()), 0);
}