Debian Codegraph @

srtcore/fec.cpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2019 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 #include "platform_sys.h"
12
13 #include <string>
14 #include <map>
15 #include <vector>
16 #include <deque>
17 #include <iterator>
18
19 #include "packetfilter.h"
20 #include "core.h"
21 #include "packet.h"
22 #include "logging.h"
23
24 #include "fec.h"
25
26 // Maximum allowed "history" remembered in the receiver groups.
27 // This is calculated in series, that is, this number will be
28 // multiplied by sizeRow() and sizeCol() to get the value being
29 // a maximum distance between the FEC group base sequence and
30 // the sequence to which a request comes in.
31
32 // XXX Might be that this parameter should be configurable
33 #define SRT_FEC_MAX_RCV_HISTORY 10
34
35 using namespace std;
36 using namespace srt_logging;
37
38 namespace srt {
39
40 const char FECFilterBuiltin::defaultConfig [] = "fec,rows:1,layout:staircase,arq:onreq";
41
42 struct StringKeys
43 {
44 string operator()(const pair<const string, const string> item)
45 {
46 return item.first;
47 }
48 };
49
50 bool FECFilterBuiltin::verifyConfig(const SrtFilterConfig& cfg, string& w_error)
51 {
52 string arspec = map_get(cfg.parameters, "layout");
53
54 if (arspec != "" && arspec != "even" && arspec != "staircase")
55 {
56 w_error = "value for 'layout' must be 'even' or 'staircase'";
57 return false;
58 }
59
60 string colspec = map_get(cfg.parameters, "cols"), rowspec = map_get(cfg.parameters, "rows");
61
62 int out_rows = 1;
63
64 if (colspec != "")
65 {
66 int out_cols = atoi(colspec.c_str());
67 if (out_cols < 2)
68 {
69 w_error = "at least 'cols' must be specified and > 1";
70 return false;
71 }
72 }
73
74 if (rowspec != "")
75 {
76 out_rows = atoi(rowspec.c_str());
77 if (out_rows >= -1 && out_rows < 1)
78 {
79 w_error = "'rows' must be >=1 or negative < -1";
80 return false;
81 }
82 }
83
84 // Extra interpret level, if found, default never.
85 // Check only those that are managed.
86 string level = map_get(cfg.parameters, "arq");
87 if (level != "")
88 {
89 static const char* const levelnames [] = {"never", "onreq", "always"};
90 size_t i = 0;
91 for (i = 0; i < Size(levelnames); ++i)
92 {
93 if (strcmp(level.c_str(), levelnames[i]) == 0)
94 break;
95 }
96
97 if (i == Size(levelnames))
98 {
99 w_error = "'arq' value '" + level + "' invalid. Allowed: never, onreq, always";
100 return false;
101 }
102 }
103
104 set<string> keys;
105 transform(cfg.parameters.begin(), cfg.parameters.end(), inserter(keys, keys.begin()), StringKeys());
106
107 // Delete all default parameters
108 SrtFilterConfig defconf;
109 ParseFilterConfig(defaultConfig, (defconf));
110 for (map<string,string>::const_iterator i = defconf.parameters.begin();
111 i != defconf.parameters.end(); ++i)
112 keys.erase(i->first);
113
114 // Delete mandatory parameters
115 keys.erase("cols");
116
117 if (!keys.empty())
118 {
119 w_error = "Extra parameters. Allowed only: cols, rows, layout, arq";
120 return false;
121 }
122
123 return true;
124 }
125
126 FECFilterBuiltin::FECFilterBuiltin(const SrtFilterInitializer &init, std::vector<SrtPacket> &provided, const string &confstr)
127 : SrtPacketFilterBase(init)
128 , m_fallback_level(SRT_ARQ_ONREQ)
129 , m_arrangement_staircase(true)
130 , rcv(provided)
131 {
132 if (!ParseFilterConfig(confstr, cfg))
133 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
134
135 string ermsg;
136 if (!verifyConfig(cfg, (ermsg)))
137 {
138 LOGC(pflog.Error, log << "IPE: Filter config failed: " << ermsg);
139 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
140 }
141
142 // Configuration supported:
143 // - row only (number_rows == 1)
144 // - columns only, no row FEC/CTL (number_rows < -1)
145 // - columns and rows (both > 1)
146
147 // Disallowed configurations:
148 // - number_cols < 1
149 // - number_rows [-1, 0]
150
151 string arspec = map_get(cfg.parameters, "layout");
152
153 string shorter = arspec.size() > 5 ? arspec.substr(0, 5) : arspec;
154 if (shorter == "even")
155 m_arrangement_staircase = false;
156
157 string colspec = map_get(cfg.parameters, "cols"), rowspec = map_get(cfg.parameters, "rows");
158
159 if (colspec == "")
160 {
161 LOGC(pflog.Error, log << "FEC filter config: parameter 'cols' is mandatory");
162 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
163 }
164
165 int out_rows = 1;
166 int out_cols = atoi(colspec.c_str());
167
168 m_number_cols = out_cols;
169
170 if (rowspec != "")
171 {
172 out_rows = atoi(rowspec.c_str());
173 }
174
175 if (out_rows < 0)
176 {
177 m_number_rows = -out_rows;
178 m_cols_only = true;
179 }
180 else
181 {
182 m_number_rows = out_rows;
183 m_cols_only = false;
184 }
185
186 // Extra interpret level, if found, default never.
187 // Check only those that are managed.
188 string level = cfg.parameters["arq"];
189 int lv = -1;
190 if (level != "")
191 {
192 static const char* levelnames [] = { "never", "onreq", "always" };
193
194 for (size_t i = 0; i < Size(levelnames); ++i)
195 {
196 if (level == levelnames[i])
197 {
198 lv = int(i);
199 break;
200 }
201 }
202 }
203
204 if (lv != -1)
205 {
206 m_fallback_level = SRT_ARQLevel(lv);
207 }
208 else
209 {
210 m_fallback_level = SRT_ARQ_ONREQ;
211 }
212
213 // Required to store in the header when rebuilding
214 rcv.id = socketID();
215
216 // Setup the bit matrix, initialize everything with false.
217
218 // Vertical size (y)
219 rcv.cells.resize(sizeCol() * sizeRow(), false);
220
221 // These sequence numbers are both the value of ISN-1 at the moment
222 // when the handshake is done. The sender ISN is generated here, the
223 // receiver ISN by the peer. Both should be known after the handshake.
224 // Later they will be updated as packets are transmitted.
225
226 int32_t snd_isn = CSeqNo::incseq(sndISN());
227 int32_t rcv_isn = CSeqNo::incseq(rcvISN());
228
229 // Alright, now we need to get the ISN from m_parent
230 // to extract the sequence number allowing qualification to the group.
231 // The base values must be prepared so that feedSource can qualify them.
232
233 // SEPARATE FOR SENDING AND RECEIVING!
234
235 // Now, assignment of the groups requires:
236 // For row groups, simply the size of the group suffices.
237 // For column groups, you need a whole matrix of all sequence
238 // numbers that are base sequence numbers for the group.
239 // Sequences that belong to this group are:
240 // 1. First packet has seq+1 towards the base.
241 // 2. Every next packet has this value + the size of the row group.
242 // So: group dispatching is:
243 // - get the column number
244 // - extract the group data for that column
245 // - check if the sequence is later than the group base sequence, if not, report no group for the packet
246 // - sanity check, if the seqdiff divided by row size gets 0 remainder
247 // - The result from the above division can't exceed the column size, otherwise
248 // it's another group. The number of currently collected data should be in 'collected'.
249
250 // Now set up the group starting sequences.
251 // The very first group in both dimensions will have the value of ISN in particular direction.
252
253 // Set up sender part.
254 //
255 // Size: rows
256 // Step: 1 (next packet in group is 1 past the previous one)
257 // Slip: rows (first packet in the next group is distant to first packet in the previous group by 'rows')
258 HLOGC(pflog.Debug, log << "FEC: INIT: ISN { snd=" << snd_isn << " rcv=" << rcv_isn << " }; sender single row");
259 ConfigureGroup(snd.row, snd_isn, 1, sizeRow());
260
261 // In the beginning we need just one reception group. New reception
262 // groups will be created in tact with receiving packets outside this one.
263 // The value of rcv.row[0].base will be used as an absolute base for calculating
264 // the index of the group for a given received packet.
265 rcv.rowq.resize(1);
266 HLOGP(pflog.Debug, "FEC: INIT: receiver first row");
267 ConfigureGroup(rcv.rowq[0], rcv_isn, 1, sizeRow());
268
269 if (sizeCol() > 1)
270 {
271 // Size: cols
272 // Step: rows (the next packet in the group is one row later)
273 // Slip: rows+1 (the first packet in the next group is later by 1 column + one whole row down)
274
275 HLOGP(pflog.Debug, "FEC: INIT: sender first N columns");
276 ConfigureColumns(snd.cols, snd_isn);
277 HLOGP(pflog.Debug, "FEC: INIT: receiver first N columns");
278 ConfigureColumns(rcv.colq, rcv_isn);
279 }
280
281 // The bit markers that mark the received/lost packets will be expanded
282 // as packets come in.
283 rcv.cell_base = rcv_isn;
284 }
285
286 template <class Container>
287 void FECFilterBuiltin::ConfigureColumns(Container& which, int32_t isn)
288 {
289 // This is to initialize the first set of groups.
290
291 // which: group vector.
292 // numberCols(): number of packets in one group
293 // sizeCol(): seqdiff between two packets consecutive in the group
294 // m_column_slip: seqdiff between the first packet in one group and first packet in the next group
295 // isn: sequence number of the first packet in the first group
296
297 size_t zero = which.size();
298
299 // The first series of initialization should embrace:
300 // - if multiplayer == 1, EVERYTHING (also the case of SOLID matrix)
301 // - if more, ONLY THE FIRST SQUARE.
302 which.resize(zero + numberCols());
303
304 if (!m_arrangement_staircase)
305 {
306 HLOGC(pflog.Debug, log << "ConfigureColumns: new "
307 << numberCols() << " columns, START AT: " << zero);
308 // With even arrangement, just use a plain loop.
309 // Initialize straight way all groups in the size.
310 int32_t seqno = isn;
311 for (size_t i = zero; i < which.size(); ++i)
312 {
313 // ARGS:
314 // - seqno: sequence number of the first packet in the group
315 // - step: distance between two consecutive packets in the group
316 // - drop: distance between base sequence numbers in groups in consecutive series
317 // (meaning: with row size 6, group with index 2 and 8 are in the
318 // same column 2, lying in 0 and 1 series respectively).
319 ConfigureGroup(which[i], seqno, sizeRow(), sizeCol() * numberCols());
320 seqno = CSeqNo::incseq(seqno);
321 }
322 return;
323 }
324
325 // With staircase, the next column's base sequence is
326 // shifted by 1 AND the length of the row. When this shift
327 // becomes below the column 0 bottom, reset it to the row 0
328 // and continue.
329
330 // Start here. The 'isn' is still the absolute base sequence value.
331 size_t offset = 0;
332
333 HLOGC(pflog.Debug, log << "ConfigureColumns: " << (which.size() - zero)
334 << " columns, START AT: " << zero);
335
336 for (size_t i = zero; i < which.size(); ++i)
337 {
338 int32_t seq = CSeqNo::incseq(isn, int(offset));
339 size_t col = i - zero;
340
341 HLOGC(pflog.Debug, log << "ConfigureColumns: [" << col << "]: -> ConfigureGroup...");
342 ConfigureGroup(which[i], seq, sizeRow(), sizeCol() * numberCols());
343
344 if (col % numberRows() == numberRows() - 1)
345 {
346 offset = col + 1; // +1 because we want it for the next column
347 HLOGC(pflog.Debug, log << "ConfigureColumns: [" << (int(col)+1) << "]... (resetting to row 0: +"
348 << offset << " %" << CSeqNo::incseq(isn, (int32_t)offset) << ")");
349 }
350 else
351 {
352 offset += 1 + sizeRow();
353 HLOGC(pflog.Debug, log << "ConfigureColumns: [" << (int(col)+1) << "] ... (continue +"
354 << offset << " %" << CSeqNo::incseq(isn, (int32_t)offset) << ")");
355 }
356 }
357 }
358
359 void FECFilterBuiltin::ConfigureGroup(Group& g, int32_t seqno, size_t gstep, size_t drop)
360 {
361 g.base = seqno;
362 g.step = gstep;
363
364 // This actually rewrites the size of the group here, but
365 // by having this value precalculated we simply close the
366 // group by adding this value to the base sequence.
367 g.drop = drop;
368 g.collected = 0;
369
370 // Now the buffer spaces for clips.
371 g.payload_clip.resize(payloadSize());
372 g.length_clip = 0;
373 g.flag_clip = 0;
374 g.timestamp_clip = 0;
375
376 HLOGC(pflog.Debug, log << "FEC: ConfigureGroup: base %" << seqno << " step=" << gstep << " drop=" << drop);
377
378 // Preallocate the buffer that will be used for storing it for
379 // the needs of passing the data through the network.
380 // This will be filled with zeros initially, which is unnecessary,
381 // but it happeens just once after connection.
382 }
383
384
385 void FECFilterBuiltin::ResetGroup(Group& g)
386 {
387 const int32_t new_seq_base = CSeqNo::incseq(g.base, int(g.drop));
388
389 HLOGC(pflog.Debug, log << "FEC: ResetGroup (step=" << g.step << "): base %" << g.base << " -> %" << new_seq_base);
390
391 g.base = new_seq_base;
392 g.collected = 0;
393
394 // This isn't necessary for ConfigureGroup because the
395 // vector after resizing is filled with a given value,
396 // by default the default value of the type, char(), that is 0.
397 g.length_clip = 0;
398 g.flag_clip = 0;
399 g.timestamp_clip = 0;
400 memset(&g.payload_clip[0], 0, g.payload_clip.size());
401 }
402
403 void FECFilterBuiltin::feedSource(CPacket& packet)
404 {
405 // Hang on the matrix. Find by packet->getSeqNo().
406
407 // (The "absolute base" is the cell 0 in vertical groups)
408 int32_t base = snd.row.base;
409
410 // (we are guaranteed that this packet is a data packet, so
411 // we don't have to check if this isn't a control packet)
412 int baseoff = CSeqNo::seqoff(base, packet.getSeqNo());
413
414 int horiz_pos = baseoff;
415
416 if (CheckGroupClose(snd.row, horiz_pos, sizeRow()))
417 {
418 HLOGC(pflog.Debug, log << "FEC:... HORIZ group closed, B=%" << snd.row.base);
419 }
420 ClipPacket(snd.row, packet);
421 snd.row.collected++;
422
423 // Don't do any column feeding if using column size 1
424 if (sizeCol() < 2)
425 {
426 // The above logging instruction in case of no columns
427 HLOGC(pflog.Debug, log << "FEC:feedSource: %" << packet.getSeqNo()
428 << " B:%" << baseoff << " H:*[" << horiz_pos << "]"
429 << " size=" << packet.size()
430 << " TS=" << packet.getMsgTimeStamp()
431 << " !" << BufferStamp(packet.data(), packet.size()));
432 HLOGC(pflog.Debug, log << "FEC collected: H: " << snd.row.collected);
433 return;
434 }
435
436 // 1. Get the number of group in both vertical and horizontal groups:
437 // - Vertical: offset towards base (% row size, but with updated Base seq unnecessary)
438 // (Just for a case).
439 int vert_gx = baseoff % sizeRow();
440
441 // 2. Define the position of this packet in the group
442 // - Horizontal: offset towards base (of the given group, not absolute!)
443 // - Vertical: (seq-base)/column_size
444 int32_t vert_base = snd.cols[vert_gx].base;
445 int vert_off = CSeqNo::seqoff(vert_base, packet.getSeqNo());
446
447 // It MAY HAPPEN that the base is newer than the sequence of the packet.
448 // This may normally happen in the beginning period, where the bases
449 // set up initially for all columns got the shift, so they are kinda from
450 // the future, and "this sequence" is in a group that is already closed.
451 // In this case simply can't clip the packet in the column group.
452
453 HLOGC(pflog.Debug, log << "FEC:feedSource: %" << packet.getSeqNo() << " rowoff=" << baseoff
454 << " column=" << vert_gx << " .base=%" << vert_base << " coloff=" << vert_off);
455
456 // [[assert sizeCol() >= 2]]; // see the condition above.
457
458 if (vert_off >= 0)
459 {
460 // BEWARE! X % Y with different signedness upgrades int to unsigned!
461
462 // SANITY: check if the rule applies on the group
463 if (vert_off % sizeRow())
464 {
465 LOGC(pflog.Fatal, log << "FEC:feedSource: IPE: VGroup #" << vert_gx << " base=%" << vert_base
466 << " WRONG with horiz base=%" << base << "coloff(" << vert_off
467 << ") % sizeRow(" << sizeRow() << ") = " << (vert_off % sizeRow()));
468
469 // Do not place it, it would be wrong.
470 return;
471 }
472
473 // [[assert vert_off >= 0]]; // this condition branch
474 int vert_pos = vert_off / int(sizeRow());
475
476 HLOGC(pflog.Debug, log << "FEC:feedSource: %" << packet.getSeqNo()
477 << " B:%" << baseoff << " H:*[" << horiz_pos << "] V(B=%" << vert_base
478 << ")[col=" << vert_gx << "][" << vert_pos << "/" << sizeCol() << "] "
479 << " size=" << packet.size()
480 << " TS=" << packet.getMsgTimeStamp()
481 << " !" << BufferStamp(packet.data(), packet.size()));
482
483 // 3. The group should be check for the necessity of being closed.
484 // Note that FEC packet extraction doesn't change the state of the
485 // VERTICAL groups (it can be potentially extracted multiple times),
486 // only the horizontal in order to mark that the vertical FEC is
487 // extracted already. So, anyway, check if the group limit was reached
488 // and it wasn't closed.
489 // 4. Apply the clip
490 // 5. Increase collected.
491
492 if (CheckGroupClose(snd.cols[vert_gx], vert_pos, sizeCol()))
493 {
494 HLOGC(pflog.Debug, log << "FEC:... VERT group closed, B=%" << snd.cols[vert_gx].base);
495 }
496 ClipPacket(snd.cols[vert_gx], packet);
497 snd.cols[vert_gx].collected++;
498 }
499 else
500 {
501 HLOGC(pflog.Debug, log << "FEC:feedSource: %" << packet.getSeqNo()
502 << " B:%" << baseoff << " H:*[" << horiz_pos << "] V(B=%" << vert_base
503 << ")[col=" << vert_gx << "]<NO-COLUMN>"
504 << " size=" << packet.size()
505 << " TS=" << packet.getMsgTimeStamp()
506 << " !" << BufferStamp(packet.data(), packet.size()));
507 }
508 HLOGC(pflog.Debug, log << "FEC collected: H: " << snd.row.collected << " V[" << vert_gx << "]: " << snd.cols[vert_gx].collected);
509 }
510
511 bool FECFilterBuiltin::CheckGroupClose(Group& g, size_t pos, size_t size)
512 {
513 if (pos < size)
514 return false;
515
516 ResetGroup(g);
517 return true;
518 }
519
520 void FECFilterBuiltin::ClipPacket(Group& g, const CPacket& pkt)
521 {
522 // Both length and timestamp must be taken as NETWORK ORDER
523 // before applying the clip.
524
525 uint16_t length_net = htons(uint16_t(pkt.size()));
526 uint8_t kflg = uint8_t(pkt.getMsgCryptoFlags());
527
528 // NOTE: Unlike length, the TIMESTAMP is NOT endian-reordered
529 // because it will be written into the TIMESTAMP field in the
530 // header, and header is inverted automatically when sending,
531 // unlike the contents of the payload, where the length will be written.
532 uint32_t timestamp_hw = pkt.getMsgTimeStamp();
533
534 ClipData(g, length_net, kflg, timestamp_hw, pkt.data(), pkt.size());
535
536 HLOGC(pflog.Debug, log << "FEC DATA PKT CLIP: " << hex
537 << "FLAGS=" << unsigned(kflg) << " LENGTH[ne]=" << (length_net)
538 << " TS[he]=" << timestamp_hw
539 << " CLIP STATE: FLAGS=" << unsigned(g.flag_clip)
540 << " LENGTH[ne]=" << g.length_clip
541 << " TS[he]=" << g.timestamp_clip
542 << " PL4=" << (*(uint32_t*)&g.payload_clip[0]));
543 }
544
545 // Clipping a control packet does merely the same, just the packet has
546 // different contents, so it must be differetly interpreted.
547 void FECFilterBuiltin::ClipControlPacket(Group& g, const CPacket& pkt)
548 {
549 // Both length and timestamp must be taken as NETWORK ORDER
550 // before applying the clip.
551
552 const char* fec_header = pkt.data();
553 const char* payload = fec_header + 4;
554 size_t payload_clip_len = pkt.size() - 4;
555
556 const uint8_t* flag_clip = (const uint8_t*)(fec_header + 1);
557 const uint16_t* length_clip = (const uint16_t*)(fec_header + 2);
558
559 uint32_t timestamp_hw = pkt.getMsgTimeStamp();
560
561 ClipData(g, *length_clip, *flag_clip, timestamp_hw, payload, payload_clip_len);
562
563 HLOGC(pflog.Debug, log << "FEC/CTL CLIP: " << hex
564 << "FLAGS=" << unsigned(*flag_clip) << " LENGTH[ne]=" << (*length_clip)
565 << " TS[he]=" << timestamp_hw
566 << " CLIP STATE: FLAGS=" << unsigned(g.flag_clip)
567 << " LENGTH[ne]=" << g.length_clip
568 << " TS[he]=" << g.timestamp_clip
569 << " PL4=" << (*(uint32_t*)&g.payload_clip[0]));
570 }
571
572 void FECFilterBuiltin::ClipRebuiltPacket(Group& g, Receive::PrivPacket& pkt)
573 {
574 uint16_t length_net = htons(uint16_t(pkt.length));
575 uint8_t kflg = MSGNO_ENCKEYSPEC::unwrap(pkt.hdr[SRT_PH_MSGNO]);
576
577 // NOTE: Unlike length, the TIMESTAMP is NOT endian-reordered
578 // because it will be written into the TIMESTAMP field in the
579 // header, and header is inverted automatically when sending,
580 // unlike the contents of the payload, where the length will be written.
581 uint32_t timestamp_hw = pkt.hdr[SRT_PH_TIMESTAMP];
582
583 ClipData(g, length_net, kflg, timestamp_hw, pkt.buffer, pkt.length);
584
585 HLOGC(pflog.Debug, log << "FEC REBUILT DATA CLIP: " << hex
586 << "FLAGS=" << unsigned(kflg) << " LENGTH[ne]=" << (length_net)
587 << " TS[he]=" << timestamp_hw
588 << " CLIP STATE: FLAGS=" << unsigned(g.flag_clip)
589 << " LENGTH[ne]=" << g.length_clip
590 << " TS[he]=" << g.timestamp_clip
591 << " PL4=" << (*(uint32_t*)&g.payload_clip[0]));
592 }
593
594 void FECFilterBuiltin::ClipData(Group& g, uint16_t length_net, uint8_t kflg,
595 uint32_t timestamp_hw, const char* payload, size_t payload_size)
596 {
597 g.length_clip = g.length_clip ^ length_net;
598 g.flag_clip = g.flag_clip ^ kflg;
599 g.timestamp_clip = g.timestamp_clip ^ timestamp_hw;
600
601 HLOGC(pflog.Debug, log << "FEC CLIP: data pkt.size=" << payload_size
602 << " to a clip buffer size=" << payloadSize());
603
604 // Payload goes "as is".
605 for (size_t i = 0; i < payload_size; ++i)
606 {
607 g.payload_clip[i] = g.payload_clip[i] ^ payload[i];
608 }
609
610 // Fill the rest with zeros. When this packet is going to be
611 // recovered, the payload extracted from this process will have
612 // the maximum length, but it will be cut to the right length
613 // and these padding 0s taken out.
614 for (size_t i = payload_size; i < payloadSize(); ++i)
615 g.payload_clip[i] = g.payload_clip[i] ^ 0;
616 }
617
618 bool FECFilterBuiltin::packControlPacket(SrtPacket& rpkt, int32_t seq)
619 {
620 // If the FEC packet is not yet ready for extraction, do nothing and return false.
621 // Check if seq is the last sequence of the group.
622
623 // Check VERTICAL group first, then HORIZONTAL.
624 //
625 // This is because when it happens that HORIZONTAL group is to be
626 // FEC-CTL reported, it also shifts the base to the next row, whereas
627 // this base sequence is used to determine the column index that is
628 // needed to reach the right column group and it must stay unupdated
629 // until the last packet in this row is checked for VERTICAL groups.
630
631 // If it's ready for extraction, extract it, and write into the packet.
632 //
633 // NOTE: seq is the sequence number of the LAST PACKET SENT regularly.
634 // This is only about to be shifted forward by 1 to be placed on the
635 // data packet. The packet in `r_packet` doesn't have the sequence number
636 // installed yet
637
638 // For BOTH vertical and horizontal snd groups:
639 // - Check if the "full group" condition is satisfied (all packets from the group are clipped)
640 // - If not, simply return false and do nothing
641 // - If so, store the current clip state into the referenced packet, give it the 'seq' sequence
642
643 // After packing the FEC packet:
644 // - update the base sequence in the group for which it's packed
645 // - make sure that pointers are reset to not suggest the packet is ready
646
647 // Handle the special case of m_number_rows == 1, which
648 // means we don't use columns.
649 if (m_number_rows <= 1)
650 {
651 HLOGC(pflog.Debug, log << "FEC/CTL not checking VERT group - rows only config");
652 // PASS ON to Horizontal group check
653 }
654 else
655 {
656 int offset_to_row_base = CSeqNo::seqoff(snd.row.base, seq);
657 int vert_gx = (offset_to_row_base + int(m_number_cols)) % int(m_number_cols);
658
659 // This can actually happen only for the very first sent packet.
660 // It looks like "following the last packet from the previous group",
661 // however there was no previous group because this is the first packet.
662 if (offset_to_row_base < 0)
663 {
664 HLOGC(pflog.Debug, log << "FEC/CTL not checking VERT group [" << vert_gx << "] - negative offset_to_row_base %"
665 << snd.row.base << " -> %" << seq << " (" << offset_to_row_base
666 << ") (collected " << snd.cols[abs(vert_gx)].collected << "/" << sizeCol() << ")");
667 // PASS ON to Horizontal group check
668 }
669 else
670 {
671 if (snd.cols[vert_gx].collected >= m_number_rows)
672 {
673 HLOGC(pflog.Debug, log << "FEC/CTL ready for VERT group [" << vert_gx << "]: %" << seq
674 << " (base %" << snd.cols[vert_gx].base << ")");
675 // SHIP THE VERTICAL FEC packet.
676 PackControl(snd.cols[vert_gx], vert_gx, rpkt, seq);
677
678 // RESET THE GROUP THAT WAS SENT
679 ResetGroup(snd.cols[vert_gx]);
680 return true;
681 }
682
683 HLOGC(pflog.Debug, log << "FEC/CTL NOT ready for VERT group [" << vert_gx << "]: %" << seq
684 << " (base %" << snd.cols[vert_gx].base << ")"
685 << " - collected " << snd.cols[vert_gx].collected << "/" << m_number_rows);
686 }
687 }
688
689 if (snd.row.collected >= m_number_cols)
690 {
691 if (!m_cols_only)
692 {
693 HLOGC(pflog.Debug, log << "FEC/CTL ready for HORIZ group: %" << seq << " (base %" << snd.row.base << ")");
694 // SHIP THE HORIZONTAL FEC packet.
695 PackControl(snd.row, -1, rpkt, seq);
696
697 HLOGC(pflog.Debug, log << "...PACKET size=" << rpkt.length
698 << " TS=" << rpkt.hdr[SRT_PH_TIMESTAMP]
699 << " !" << BufferStamp(rpkt.buffer, rpkt.length));
700
701 }
702
703 // RESET THE HORIZONTAL GROUP.
704 // ALWAYS, even in columns-only.
705 ResetGroup(snd.row);
706
707 if (!m_cols_only)
708 {
709 // In columns-only you didn't pack anything, so check
710 // for column control.
711 return true;
712 }
713 }
714 else
715 {
716 HLOGC(pflog.Debug, log << "FEC/CTL NOT ready for HORIZ group: %" << seq
717 << " (base %" << snd.row.base << ")"
718 << " - collected " << snd.row.collected << "/" << m_number_cols);
719 }
720
721 return false;
722 }
723
724 void FECFilterBuiltin::PackControl(const Group& g, signed char index, SrtPacket& pkt, int32_t seq)
725 {
726 // Allocate as much space as needed, regardless of the PAYLOADSIZE value.
727
728 static const size_t INDEX_SIZE = 1;
729
730 size_t total_size =
731 INDEX_SIZE
732 + sizeof(g.flag_clip)
733 + sizeof(g.length_clip)
734 + g.payload_clip.size();
735
736 // Sanity
737 #if ENABLE_DEBUG
738 if (g.output_buffer.size() < total_size)
739 {
740 LOGC(pflog.Fatal, log << "OUTPUT BUFFER TOO SMALL!");
741 abort();
742 }
743 #endif
744
745 char* out = pkt.buffer;
746 size_t off = 0;
747 // Spread the index. This is the index of the payload in the vertical group.
748 // For horizontal group this value is always -1.
749 out[off++] = index;
750 // Flags, currently only the encryption flags
751 out[off++] = g.flag_clip;
752
753 // Ok, now the length clip
754 memcpy((out + off), &g.length_clip, sizeof g.length_clip);
755 off += sizeof g.length_clip;
756
757 // And finally the payload clip
758 memcpy((out + off), &g.payload_clip[0], g.payload_clip.size());
759
760 // Ready. Now fill the header and finalize other data.
761 pkt.length = total_size;
762
763 pkt.hdr[SRT_PH_TIMESTAMP] = g.timestamp_clip;
764 pkt.hdr[SRT_PH_SEQNO] = seq;
765
766 HLOGC(pflog.Debug, log << "FEC: PackControl: hdr("
767 << (total_size - g.payload_clip.size()) << "): INDEX="
768 << int(index) << " LENGTH[ne]=" << hex << g.length_clip
769 << " FLAGS=" << int(g.flag_clip) << " TS=" << g.timestamp_clip
770 << " PL(" << dec << g.payload_clip.size() << ")[0-4]=" << hex
771 << (*(uint32_t*)&g.payload_clip[0]));
772
773 }
774
775 bool FECFilterBuiltin::receive(const CPacket& rpkt, loss_seqs_t& loss_seqs)
776 {
777 // Add this packet to the group where it belongs.
778 // Light up the cell of this packet to mark it received.
779 // Check if any of the groups to which the packet belongs
780 // have changed the status into RECOVERABLE.
781 //
782 // The group has RECOVERABLE status when it has FEC
783 // packet received and the number of collected packets counts
784 // exactly group_size - 1.
785
786 bool want_packet = false;
787
788 struct IsFec
789 {
790 bool row;
791 bool col;
792 signed char colx;
793 } isfec = { false, false, -1 };
794
795 // The sequence number must be checked prematurely, or it can otherwise
796 // cause large resource allocation. This might be even survived, provided
797 // that this will make the packet seen as exceeding the series 0 matrix,
798 // so all matrices in previous series should be dismissed thereafter. But
799 // this short living resource spike may be destructive, so let's do
800 // matrix dismissal FIRST before this packet is going to be handled.
801 CheckLargeDrop(rpkt.getSeqNo());
802
803 if (rpkt.getMsgSeq() == SRT_MSGNO_CONTROL)
804 {
805 // Interpret the first byte of the contents.
806 const char* payload = rpkt.data();
807 isfec.colx = payload[0];
808 if (isfec.colx == -1)
809 {
810 isfec.row = true;
811 }
812 else
813 {
814 isfec.col = true;
815 }
816
817 HLOGC(pflog.Debug, log << "FEC: RECEIVED %" << rpkt.getSeqNo() << " msgno=0, FEC/CTL packet. INDEX=" << int(payload[0]));
818
819 // This marks the cell as NOT received, but still does extend the
820 // cell container up to this sequence. The HangHorizontal and HangVertical
821 // functions that would also do cell dismissal, RELY ON IT.
822 MarkCellReceived(rpkt.getSeqNo(), CELL_EXTEND);
823 }
824 else
825 {
826 // Data packet, check if this packet was already received.
827 // If so, ignore it. This may happen if you have configured
828 // FEC and ARQ to cooperate, so a packet once rebuilt might
829 // be simultaneously also retransmitted. This may confuse the tables.
830 int celloff = CSeqNo::seqoff(rcv.cell_base, rpkt.getSeqNo());
831 bool past = celloff < 0;
832 bool exists = celloff < int(rcv.cells.size()) && !past && rcv.cells[celloff];
833
834 if (past || exists)
835 {
836 HLOGC(pflog.Debug, log << "FEC: packet %" << rpkt.getSeqNo() << " "
837 << (past ? "in the PAST" : "already known") << ", IGNORING.");
838
839 return true;
840 }
841
842 want_packet = true;
843
844 HLOGC(pflog.Debug, log << "FEC: RECEIVED %" << rpkt.getSeqNo() << " msgno=" << rpkt.getMsgSeq() << " DATA PACKET.");
845 MarkCellReceived(rpkt.getSeqNo());
846
847 // Remember this simply every time a packet comes in. In live mode usually
848 // this flag is ORD_RELAXED (false), but some earlier versions used ORD_REQUIRED.
849 // Even though this flag is now usually ORD_RELAXED, it's fate in live mode
850 // isn't completely decided yet, so stay flexible. We believe at least that this
851 // flag will stay unchanged during whole connection.
852 rcv.order_required = rpkt.getMsgOrderFlag();
853 }
854
855 loss_seqs_t irrecover_row, irrecover_col;
856
857 #if ENABLE_HEAVY_LOGGING
858 static string hangname [] = {"NOT-DONE", "SUCCESS", "PAST", "CRAZY"};
859 #endif
860
861 // Required for EHangStatus
862 using namespace std::rel_ops;
863
864 EHangStatus okh = HANG_NOTDONE;
865 if (!isfec.col) // == regular packet or FEC/ROW
866 {
867 // Don't manage this packet for horizontal group,
868 // if it was a vertical FEC/CTL packet.
869 okh = HangHorizontal(rpkt, isfec.row, irrecover_row);
870 HLOGC(pflog.Debug, log << "FEC: HangHorizontal %" << rpkt.getSeqNo()
871 << " msgno=" << rpkt.getMsgSeq()
872 << " RESULT=" << hangname[okh] << " IRRECOVERABLE: " << Printable(irrecover_row));
873 }
874
875 if (okh > HANG_SUCCESS)
876 {
877 // Just informative.
878 LOGC(pflog.Warn, log << "FEC/H: rebuilding/hanging FAILED.");
879 }
880
881 EHangStatus okv = HANG_NOTDONE;
882 // Don't do HangVertical in case of row-only configuration
883 if (!isfec.row && m_number_rows > 1) // == regular packet or FEC/COL
884 {
885 // NOTE FOR IPE REPORTING:
886 // It is allowed that
887 // - Both HangVertical and HangHorizontal
888
889 okv = HangVertical(rpkt, isfec.colx, irrecover_col);
890 IF_HEAVY_LOGGING(bool discrep = (okv == HANG_CRAZY) ? int(okh) < HANG_CRAZY : false);
891 HLOGC(pflog.Debug, log << "FEC: HangVertical %" << rpkt.getSeqNo()
892 << " msgno=" << rpkt.getMsgSeq()
893 << " RESULT=" << hangname[okh]
894 << (discrep ? " IPE: H successful and V failed!" : "")
895 << " IRRECOVERABLE: " << Printable(irrecover_col));
896 }
897
898 if (okv > HANG_SUCCESS)
899 {
900 // Just informative.
901 LOGC(pflog.Warn, log << "FEC/V: rebuilding/hanging FAILED.");
902 }
903
904 if (okv == HANG_CRAZY || okh == HANG_CRAZY)
905 {
906 // Mark the cell not received, if it was rejected by the
907 // FEC group facility, otherwise it will deny to try to rebuild an
908 // allegedly existing packet.
909 MarkCellReceived(rpkt.getSeqNo(), CELL_REMOVE);
910 }
911
912 // Pack the following packets as irrecoverable:
913 if (m_fallback_level == SRT_ARQ_ONREQ)
914 {
915 // Use irrecover_row with rows only because there is
916 // never anything collected in irrecover_col.
917 if (m_number_rows == 1)
918 loss_seqs = irrecover_row;
919 else
920 loss_seqs = irrecover_col;
921 }
922
923 return want_packet;
924 // Get the packet from the incoming stream, already recognized
925 // as data packet, and then:
926 //
927 // (Note that the default builtin FEC mechanism uses such rules:
928 // - allows SRT to get the packet, even if it follows the loss
929 // - depending on m_fallback_level, confirms or denies the need that SRT handle the loss
930 // - in loss_seqs we return those that are not recoverable at the current level
931 // - FEC has no extra header provided, so regular data are passed as is
932 //)
933 // So, the needs to implement:
934 //
935 // 1. If this is a FEC packet, close the group, check for lost packets, try to recover.
936 // Check if there is recovery possible, if so, request a new unit and pack the recovered packet there.
937 // Report the loss to be reported by SRT according to m_fallback_level:
938 // - ARQ_ALWAYS: N/A for a FEC packet
939 // - ARQ_EARLY: When Horizontal group is closed and the packet is not recoverable, report this in loss_seqs
940 // - ARQ_LATELY: When Horizontal and Vertical group is closed and the packet is not recoverable, report it.
941 // - ARQ_NEVER: Always return empty loss_seqs
942 //
943 // 2. If this is a regular packet, use it for building the FEC group.
944 // - ARQ_ALWAYS: always return true and leave loss_seqs empty.
945 // - others: return false and return nothing in loss_seqs
946 }
947
948 void FECFilterBuiltin::CheckLargeDrop(int32_t seqno)
949 {
950 // Ok, first try to pick up the column and series
951
952 int offset = CSeqNo::seqoff(rcv.rowq[0].base, seqno);
953 if (offset < 0)
954 {
955 return;
956 }
957
958 // For row-only configuration, check only parts referring
959 // to a row.
960 if (m_number_rows == 1)
961 {
962 // We have no columns. So just check if exceeds 5* the row size.
963 // If so, clear the rows and reconfigure them.
964 if (offset > int(5 * sizeRow()))
965 {
966 // Calculate the new row base, without breaking the current
967 // layout. Make a skip by some number of rows so that the new
968 // first row is prepared to receive this packet.
969
970 int32_t oldbase = rcv.rowq[0].base;
971 size_t rowdist = offset / sizeRow();
972 int32_t newbase = CSeqNo::incseq(oldbase, int(rowdist * sizeRow()));
973
974 LOGC(pflog.Warn, log << "FEC: LARGE DROP detected! Resetting row groups. Base: %" << oldbase
975 << " -> %" << newbase << "(shift by " << CSeqNo::seqoff(oldbase, newbase) << ")");
976
977 rcv.rowq.clear();
978 rcv.cells.clear();
979
980 rcv.rowq.resize(1);
981 HLOGP(pflog.Debug, "FEC: RE-INIT: receiver first row");
982 ConfigureGroup(rcv.rowq[0], newbase, 1, sizeRow());
983 }
984
985 return;
986 }
987
988 bool reset_anyway = false;
989 if (offset != CSeqNo::seqoff(rcv.colq[0].base, seqno))
990 {
991 reset_anyway = true;
992 HLOGC(pflog.Debug, log << "FEC: IPE: row.base %" << rcv.rowq[0].base << " != %" << rcv.colq[0].base << " - resetting");
993 }
994
995 // Number of column - regardless of series.
996 int colx = offset % numberCols();
997
998 // Base sequence from the group series 0 in this column
999
1000 // [[assert rcv.colq.size() >= numberCols()]];
1001 int32_t colbase = rcv.colq[colx].base;
1002
1003 // Offset between this base and seqno
1004 int coloff = CSeqNo::seqoff(colbase, seqno);
1005
1006 // Might be that it's in the row above the column,
1007 // still it's not a large-drop
1008 if (coloff < 0)
1009 {
1010 return;
1011 }
1012
1013 const size_t size_in_packets = colx * numberRows();
1014 const size_t matrix = numberRows() * numberCols();
1015
1016 const int colseries = coloff / int(matrix);
1017
1018 if (size_in_packets > rcvBufferSize()/2 || colseries > SRT_FEC_MAX_RCV_HISTORY || reset_anyway)
1019 {
1020 // Ok, now define the new ABSOLUTE BASE. This is the base of the column 0
1021 // column group from the series previous towards this one.
1022 int32_t oldbase = rcv.colq[0].base;
1023 int32_t newbase = CSeqNo::incseq(oldbase, (colseries-1) * int(matrix));
1024
1025 LOGC(pflog.Warn, log << "FEC: LARGE DROP detected! Resetting all groups. Base: %" << oldbase
1026 << " -> %" << newbase << "(shift by " << CSeqNo::seqoff(oldbase, newbase) << ")");
1027
1028 rcv.rowq.clear();
1029 rcv.colq.clear();
1030 rcv.cells.clear();
1031
1032 rcv.rowq.resize(1);
1033 HLOGP(pflog.Debug, "FEC: RE-INIT: receiver first row");
1034 ConfigureGroup(rcv.rowq[0], newbase, 1, sizeRow());
1035
1036 // Size: cols
1037 // Step: rows (the next packet in the group is one row later)
1038 // Slip: rows+1 (the first packet in the next group is later by 1 column + one whole row down)
1039 HLOGP(pflog.Debug, "FEC: RE-INIT: receiver first N columns");
1040 ConfigureColumns(rcv.colq, newbase);
1041
1042 rcv.cell_base = newbase;
1043 }
1044 }
1045
1046 void FECFilterBuiltin::CollectIrrecoverRow(RcvGroup& g, loss_seqs_t& irrecover) const
1047 {
1048 if (g.dismissed)
1049 return; // already collected
1050
1051 // Obtain the group's packet shift
1052
1053 int32_t base = rcv.cell_base;
1054 int offset = CSeqNo::seqoff(base, g.base);
1055 if (offset < 0)
1056 {
1057 LOGC(pflog.Error, log << "FEC: IPE: row base %" << g.base << " is PAST to cell base %" << base);
1058 return;
1059 }
1060
1061 size_t maxoff = offset + m_number_cols;
1062 // Sanity check, if all cells are really filled.
1063 if (maxoff > rcv.cells.size())
1064 {
1065 LOGC(pflog.Error, log << "FEC: IPE: Collecting loss from row %"
1066 << g.base << "+" << m_number_cols << " while cells <= %"
1067 << CSeqNo::seqoff(rcv.cell_base, int(rcv.cells.size())-1));
1068 return;
1069 }
1070
1071 bool last = true;
1072 loss_seqs_t::value_type val;
1073 for (size_t i = offset; i < maxoff; ++i)
1074 {
1075 bool gone = last;
1076 last = rcv.cells[i];
1077
1078 if (gone && !last)
1079 {
1080 // Switch full -> loss. Store the sequence, as single (for now)
1081 val.first = val.second = CSeqNo::incseq(base, int(i));
1082 }
1083 else if (last && !gone)
1084 {
1085 val.second = CSeqNo::incseq(base, int(i));
1086 irrecover.push_back(val);
1087 }
1088 }
1089
1090 // If it happened that 0 cells were until the end, we are
1091 // sure that we have the val.first set to the first of the loss list
1092 // and we've reached the end. Otherwise 'last' would be true.
1093 if (!last)
1094 {
1095 val.second = CSeqNo::incseq(base, int(maxoff)-1);
1096 irrecover.push_back(val);
1097 }
1098
1099 g.dismissed = true;
1100 }
1101
1102 #if ENABLE_HEAVY_LOGGING
1103 static inline char CellMark(const std::deque<bool>& cells, int index)
1104 {
1105 if (index >= int(cells.size()))
1106 return '/';
1107
1108 return cells[index] ? '#' : '.';
1109 }
1110
1111 static void DebugPrintCells(int32_t base, const std::deque<bool>& cells, size_t row_size)
1112 {
1113 size_t i = 0;
1114 // Shift to the first empty cell
1115 for ( ; i < cells.size(); ++i)
1116 if (cells[i] == false)
1117 break;
1118
1119 if (i == cells.size())
1120 {
1121 LOGC(pflog.Debug, log << "FEC: ... cell[0-" << (cells.size()-1) << "]: ALL CELLS EXIST");
1122 return;
1123 }
1124
1125 // Ok, we have some empty cells, so just adjust to the start of a row.
1126 size_t bstep = i % row_size;
1127 if (i < bstep) // you never know...
1128 i = 0;
1129 else
1130 i -= bstep;
1131
1132 for ( ; i < cells.size(); i += row_size )
1133 {
1134 std::ostringstream os;
1135 os << "cell[" << i << "-" << (i+row_size-1) << "] %" << CSeqNo::incseq(base, (int32_t)i) << ":";
1136 for (size_t y = 0; y < row_size; ++y)
1137 {
1138 os << " " << CellMark(cells, (int)(i+y));
1139 }
1140 LOGP(pflog.Debug, os.str());
1141 }
1142 }
1143 #else
1144 static void DebugPrintCells(int32_t /*base*/, const std::deque<bool>& /*cells*/, size_t /*row_size*/) {}
1145 #endif
1146
1147 FECFilterBuiltin::EHangStatus FECFilterBuiltin::HangHorizontal(const CPacket& rpkt, bool isfec, loss_seqs_t& irrecover)
1148 {
1149 const int32_t seq = rpkt.getSeqNo();
1150
1151 EHangStatus stat;
1152 const int rowx = RcvGetRowGroupIndex(seq, (stat));
1153 if (rowx == -1)
1154 return stat;
1155
1156 RcvGroup& rowg = rcv.rowq[rowx];
1157 // Clip the packet into the horizontal group.
1158
1159 // If this was a regular packet, increase the number of collected.
1160 // If this was a FEC/CTL packet, keep this number, just set the fec flag.
1161 if (isfec)
1162 {
1163 if (!rowg.fec)
1164 {
1165 ClipControlPacket(rowg, rpkt);
1166 rowg.fec = true;
1167 HLOGC(pflog.Debug, log << "FEC/H: FEC/CTL packet clipped, %" << seq << " base=%" << rowg.base);
1168 }
1169 else
1170 {
1171 HLOGC(pflog.Debug, log << "FEC/H: FEC/CTL at %" << seq << " DUPLICATED, skipping.");
1172 }
1173 }
1174 else
1175 {
1176 ClipPacket(rowg, rpkt);
1177 rowg.collected++;
1178 HLOGC(pflog.Debug, log << "FEC/H: DATA packet clipped, %" << seq
1179 << ", received " << rowg.collected << "/" << sizeRow()
1180 << " base=%" << rowg.base);
1181 }
1182
1183 if (rowg.fec && rowg.collected == m_number_cols - 1)
1184 {
1185 HLOGC(pflog.Debug, log << "FEC/H: HAVE " << rowg.collected << " collected & FEC; REBUILDING...");
1186 // The group will provide the information for rebuilding.
1187 // The sequence of the lost packet can be checked in cells.
1188 // With the condition of 'collected == m_number_cols - 1', there
1189 // should be only one lacking packet, so just rely on first found.
1190 RcvRebuild(rowg, RcvGetLossSeqHoriz(rowg),
1191 m_number_rows == 1 ? Group::SINGLE : Group::HORIZ);
1192
1193 #if ENABLE_HEAVY_LOGGING
1194 std::ostringstream os;
1195 for (size_t i = 0; i < rcv.rebuilt.size(); ++i)
1196 {
1197 os << " " << rcv.rebuilt[i].hdr[SRT_PH_SEQNO];
1198 }
1199
1200 LOGC(pflog.Debug, log << "FEC: ... cached rebuilt packets (" << rcv.rebuilt.size() << "):" << os.str());
1201 #endif
1202 }
1203
1204 // When there are only rows, dismiss the oldest row when you have
1205 // collected at least 1 packet in the next group. Do not dismiss
1206 // any groups here otherwise - all will be decided during column
1207 // processing.
1208
1209 bool want_collect_irrecover = false;
1210 bool want_remove_cells = false;
1211
1212 if (rcv.rowq.size() > 1)
1213 {
1214 if (m_number_rows == 1)
1215 {
1216 want_remove_cells = true;
1217 want_collect_irrecover = true;
1218 }
1219 else if (m_fallback_level == SRT_ARQ_ONREQ)
1220 {
1221 want_collect_irrecover = true;
1222 }
1223 }
1224
1225 if (want_collect_irrecover) // AND rcv.rowq.size() > 1
1226 {
1227 int current = int(rcv.rowq.size()) - 2;
1228 // We know we have at least 2 rows.
1229 // This value is then 0 or more.
1230 int past = current - 1;
1231
1232 // To trigger irrecoverable collection, the current sequence
1233 // must be further than 1/3 of the row size to start from
1234 // the previous row. Otherwise, start with the past-previous
1235 // one, as long as it still exists.
1236
1237 bool early SRT_ATR_UNUSED = false;
1238 if (past > 0)
1239 {
1240 // If you already have at least 3 rows, sweep starting from
1241 // the before-previous one (this will become 0 when the number
1242 // of rows is exactly 3).
1243 --past;
1244 }
1245 else
1246 {
1247 // If you have 2 rows, then in the current row (1) there must
1248 // be the sequence passing already the 1/3 of the size. Otherwise
1249 // decrease past to make it -1 and not pass the next test.
1250 if (CSeqNo::seqoff(rcv.rowq[1].base, seq) <= int(m_number_cols/3))
1251 {
1252 --past;
1253 }
1254 else
1255 {
1256 early = true;
1257 }
1258 }
1259
1260 if (past >= 0)
1261 {
1262 // Collect irrecoverable since the 'past' index up to 0.
1263 // If want_remove_cells, also remove these rows and corresponding cells.
1264
1265 int nrowremove = 1 + past;
1266 HLOGC(pflog.Debug, log << "Collecting irrecoverable packets from " << nrowremove << " ROWS per offset "
1267 << CSeqNo::seqoff(rcv.rowq[1].base, seq) << " vs. " << m_number_cols << "/3");
1268
1269 for (int i = 0; i <= past; ++i)
1270 {
1271 CollectIrrecoverRow(rcv.rowq[i], irrecover);
1272 }
1273
1274 // Sanity check condition - rcv.rowq must be of size
1275 // greater than the number of rows to remove so that
1276 // the rcv.rowq[0] exists after the operation.
1277 if (want_remove_cells && rcv.rowq.size() > size_t(nrowremove))
1278 {
1279 // nrowremove >= 1
1280 size_t npktremove = sizeRow() * nrowremove;
1281 size_t ersize = min(npktremove, rcv.cells.size()); // ersize <= rcv.cells.size()
1282
1283 HLOGC(pflog.Debug, log << "FEC/H: Dismissing rows n=" << nrowremove
1284 << ", starting at %" << rcv.rowq[0].base
1285 << " AND " << npktremove << " CELLS, base switch %"
1286 << rcv.cell_base << " -> %" << rcv.rowq[past].base);
1287
1288 rcv.rowq.erase(rcv.rowq.begin(), rcv.rowq.begin() + nrowremove);
1289 rcv.cells.erase(rcv.cells.begin(), rcv.cells.begin() + ersize);
1290
1291 // We state that we have removed as many cells as for the removed
1292 // rows. In case when the number of cells proved to be less than that,
1293 // it will simply remove all cells. So now set the cell base to be
1294 // in sync with the row base.
1295 rcv.cell_base = rcv.rowq[0].base;
1296 DebugPrintCells(rcv.cell_base, rcv.cells, sizeRow());
1297 }
1298 }
1299 else
1300 {
1301 HLOGC(pflog.Debug, log << "FEC: NOT collecting irrecover from rows: distance="
1302 << CSeqNo::seqoff(rcv.rowq[0].base, seq));
1303 }
1304
1305 }
1306
1307 return HANG_SUCCESS;
1308 }
1309
1310 int32_t FECFilterBuiltin::RcvGetLossSeqHoriz(Group& g)
1311 {
1312 int baseoff = CSeqNo::seqoff(rcv.cell_base, g.base);
1313 if (baseoff < 0)
1314 {
1315 LOGC(pflog.Error, log << "FEC: IPE: negative cell offset, cell_base=%" << rcv.cell_base << " Group's base: %" << g.base << " - NOT ATTEMPTING TO REBUILD");
1316 return -1;
1317 }
1318
1319 // This is a row, so start from the first cell for this group
1320 // and search lineraly for the first loss.
1321
1322 int offset = -1;
1323
1324 for (size_t cix = baseoff; cix < baseoff + m_number_cols; ++cix)
1325 {
1326 if (!rcv.CellAt(cix))
1327 {
1328 offset = int(cix);
1329 #if ENABLE_HEAVY_LOGGING
1330 // For heavy logging case, show all cells in the range
1331 LOGC(pflog.Debug, log << "FEC/H: cell %" << CSeqNo::incseq(rcv.cell_base, int(cix))
1332 << " (+" << cix << "): MISSING");
1333
1334 #else
1335
1336 // Find just one. No more that just one shall be found
1337 // because it was checked earlier that we have collected
1338 // all but just one packet.
1339 break;
1340 #endif
1341 }
1342 #if ENABLE_HEAVY_LOGGING
1343 else
1344 {
1345 LOGC(pflog.Debug, log << "FEC/H: cell %" << CSeqNo::incseq(rcv.cell_base, int(cix))
1346 << " (+" << cix << "): exists");
1347 }
1348 #endif
1349 }
1350
1351 if (offset == -1)
1352 {
1353 LOGC(pflog.Fatal, log << "FEC/H: IPE: rebuilding attempt, but no lost packet found");
1354 return -1; // sanity, shouldn't happen
1355 }
1356
1357 // Now that we have an offset towards the first packet in the cells,
1358 // translate it to the sequence number of the lost packet.
1359 return CSeqNo::incseq(rcv.cell_base, offset);
1360 }
1361
1362 int32_t FECFilterBuiltin::RcvGetLossSeqVert(Group& g)
1363 {
1364 int baseoff = CSeqNo::seqoff(rcv.cell_base, g.base);
1365 if (baseoff < 0)
1366 {
1367 LOGC(pflog.Error, log << "FEC: IPE: negative cell offset, cell_base=%" << rcv.cell_base << " Group's base: %" << g.base << " - NOT ATTEMPTING TO REBUILD");
1368 return -1;
1369 }
1370
1371 // This is a row, so start from the first cell for this group
1372 // and search lineraly for the first loss.
1373
1374 int offset = -1;
1375
1376 for (size_t col = 0; col < sizeCol(); ++col)
1377 {
1378 size_t cix = baseoff + (col * sizeRow());
1379 if (!rcv.CellAt(cix))
1380 {
1381 offset = int(cix);
1382 #if ENABLE_HEAVY_LOGGING
1383 // For heavy logging case, show all cells in the range
1384 LOGC(pflog.Debug, log << "FEC/V: cell %" << CSeqNo::incseq(rcv.cell_base, int(cix))
1385 << " (+" << cix << "): MISSING");
1386
1387 #else
1388
1389 // Find just one. No more that just one shall be found
1390 // because it was checked earlier that we have collected
1391 // all but just one packet.
1392 break;
1393 #endif
1394 }
1395 #if ENABLE_HEAVY_LOGGING
1396 else
1397 {
1398 LOGC(pflog.Debug, log << "FEC/V: cell %" << CSeqNo::incseq(rcv.cell_base, int(cix))
1399 << " (+" << cix << "): exists");
1400 }
1401 #endif
1402 }
1403
1404 if (offset == -1)
1405 {
1406 LOGC(pflog.Fatal, log << "FEC/V: IPE: rebuilding attempt, but no lost packet found");
1407 return -1; // sanity, shouldn't happen
1408 }
1409
1410 // Now that we have an offset towards the first packet in the cells,
1411 // translate it to the sequence number of the lost packet.
1412 return CSeqNo::incseq(rcv.cell_base, offset);
1413 }
1414
1415 void FECFilterBuiltin::RcvRebuild(Group& g, int32_t seqno, Group::Type tp)
1416 {
1417 if (seqno == -1)
1418 return;
1419
1420 uint16_t length_hw = ntohs(g.length_clip);
1421 if (length_hw > payloadSize())
1422 {
1423 LOGC(pflog.Warn, log << "FEC: DECLIPPED length '" << length_hw << "' exceeds payload size. NOT REBUILDING.");
1424 return;
1425 }
1426
1427 // Rebuild the packet
1428 // (length_hw is automatically converted through PrivPacket constructor)
1429 rcv.rebuilt.push_back( length_hw );
1430
1431 Receive::PrivPacket& p = rcv.rebuilt.back();
1432
1433 p.hdr[SRT_PH_SEQNO] = seqno;
1434
1435 // This is for live mode only, for now, so the message
1436 // number will be always 1, PB_SOLO, INORDER, and flags from clip.
1437 // The REXMIT flag is set to 1 to fake that the packet was
1438 // retransmitted. It is necessary because this packet will
1439 // come out of sequence order, and if such a packet has
1440 // no rexmit flag set, it's treated as reordered by network,
1441 // which isn't true here.
1442 p.hdr[SRT_PH_MSGNO] = 1
1443 | MSGNO_PACKET_BOUNDARY::wrap(PB_SOLO)
1444 | MSGNO_PACKET_INORDER::wrap(rcv.order_required)
1445 | MSGNO_ENCKEYSPEC::wrap(g.flag_clip)
1446 | MSGNO_REXMIT::wrap(true)
1447 ;
1448
1449 p.hdr[SRT_PH_TIMESTAMP] = g.timestamp_clip;
1450 p.hdr[SRT_PH_ID] = rcv.id;
1451
1452 // Header ready, now we rebuild the contents
1453 // First, rebuild the length.
1454
1455 // Allocate the buffer and assign to a packet.
1456 // This is only temporary, it will be copied to
1457 // the target place when needed, with the buffer coming
1458 // from the unit queue.
1459
1460 // The payload clip may be longer than length_hw, but it
1461 // contains only trailing zeros for completion, which are skipped.
1462 copy(g.payload_clip.begin(), g.payload_clip.end(), p.buffer);
1463
1464 HLOGC(pflog.Debug, log << "FEC: REBUILT: %" << seqno
1465 << " msgno=" << MSGNO_SEQ::unwrap(p.hdr[SRT_PH_MSGNO])
1466 << " flags=" << PacketMessageFlagStr(p.hdr[SRT_PH_MSGNO])
1467 << " TS=" << p.hdr[SRT_PH_TIMESTAMP] << " ID=" << dec << p.hdr[SRT_PH_ID]
1468 << " size=" << length_hw
1469 << " !" << BufferStamp(p.buffer, p.length));
1470
1471 // Mark this packet received
1472 MarkCellReceived(seqno);
1473
1474 // If this is a single request (filled from row and m_number_cols == 1),
1475 // do not attempt recursive rebuilding
1476 if (tp == Group::SINGLE)
1477 return;
1478
1479 Group::Type crosstype = Group::FlipType(tp);
1480 EHangStatus stat;
1481
1482 if (crosstype == Group::HORIZ)
1483 {
1484 // Find this packet in the horizontal group
1485 const int rowx = RcvGetRowGroupIndex(seqno, (stat));
1486 if (rowx == -1)
1487 return; // can't access any group to rebuild
1488 RcvGroup& rowg = rcv.rowq[rowx];
1489
1490 // Sanity check. It's impossible that the packet was already
1491 // rebuilt and any attempt to rebuild a lacking packet was made.
1492 if (rowg.collected > m_number_cols - 1)
1493 {
1494 return;
1495 }
1496
1497 // Same as ClipPacket for the incoming packet, just this
1498 // is extracting the data directly from the rebuilt one.
1499 ClipRebuiltPacket(rowg, p);
1500 rowg.collected++;
1501 HLOGC(pflog.Debug, log << "FEC/H: REBUILT packet clipped, %" << seqno
1502 << ", received " << rowg.collected << "/" << m_number_cols
1503 << " FOR base=%" << rowg.base);
1504
1505 // Similar as by HangHorizontal, just don't collect irrecoverable packets.
1506 // They are already known when the packets were collected.
1507 if (rowg.fec && rowg.collected == m_number_cols - 1)
1508 {
1509 HLOGC(pflog.Debug, log << "FEC/H: with FEC-rebuilt HAVE " << rowg.collected << " collected & FEC; REBUILDING");
1510 // The group will provide the information for rebuilding.
1511 // The sequence of the lost packet can be checked in cells.
1512 // With the condition of 'collected == m_number_cols - 1', there
1513 // should be only one lacking packet, so just rely on first found.
1514
1515 // NOTE: RECURSIVE CALL.
1516 RcvRebuild(rowg, RcvGetLossSeqHoriz(rowg), crosstype);
1517 }
1518 }
1519 else // crosstype == Group::VERT
1520 {
1521 // Find this packet in the vertical group
1522 const int colx = RcvGetColumnGroupIndex(seqno, (stat));
1523 if (colx == -1)
1524 return; // can't access any group to rebuild
1525 RcvGroup& colg = rcv.colq[colx];
1526
1527 // Sanity check. It's impossible that the packet was already
1528 // rebuilt and any attempt to rebuild a lacking packet was made.
1529 if (colg.collected > m_number_rows - 1)
1530 {
1531 return;
1532 }
1533
1534 // Same as ClipPacket for the incoming packet, just this
1535 // is extracting the data directly from the rebuilt one.
1536 ClipRebuiltPacket(colg, p);
1537 colg.collected++;
1538 HLOGC(pflog.Debug, log << "FEC/V: REBUILT packet clipped, %" << seqno
1539 << ", received " << colg.collected << "/" << m_number_rows
1540 << " FOR base=%" << colg.base);
1541
1542 // Similar as by HangVertical, just don't collect irrecoverable packets.
1543 // They are already known when the packets were collected.
1544 if (colg.fec && colg.collected == m_number_rows - 1)
1545 {
1546 HLOGC(pflog.Debug, log << "FEC/V: with FEC-rebuilt HAVE " << colg.collected << " collected & FEC; REBUILDING");
1547 // The group will provide the information for rebuilding.
1548 // The sequence of the lost packet can be checked in cells.
1549 // With the condition of 'collected == m_number_rows - 1', there
1550 // should be only one lacking packet, so just rely on first found.
1551
1552 // NOTE: RECURSIVE CALL.
1553 RcvRebuild(colg, RcvGetLossSeqVert(colg), crosstype);
1554 }
1555 }
1556
1557 }
1558
1559 size_t FECFilterBuiltin::ExtendRows(size_t rowx)
1560 {
1561 // Check if oversize. Oversize is when the
1562 // index is > 2*m_number_cols. If so, shrink
1563 // the container first.
1564
1565 #if ENABLE_HEAVY_LOGGING
1566 LOGC(pflog.Debug, log << "FEC: ROW STATS BEFORE: n=" << rcv.rowq.size());
1567
1568 for (size_t i = 0; i < rcv.rowq.size(); ++i)
1569 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.rowq[i].DisplayStats());
1570 #endif
1571
1572 const size_t size_in_packets = rowx * numberCols();
1573 const int n_series = int(rowx / numberRows());
1574
1575 if (CheckEmergencyShrink(n_series, size_in_packets))
1576 {
1577 HLOGC(pflog.Debug, log << "FEC: DONE Emergency resize, rowx=" << rowx << " series=" << n_series
1578 << "npackets=" << size_in_packets << " exceeds buf=" << rcvBufferSize());
1579 }
1580
1581 // Create and configure next groups.
1582 size_t old = rcv.rowq.size();
1583
1584 // First, add the number of groups.
1585 rcv.rowq.resize(rowx + 1);
1586
1587 // Starting from old size
1588 for (size_t i = old; i < rcv.rowq.size(); ++i)
1589 {
1590 // Initialize the base for the row group
1591 int32_t ibase = CSeqNo::incseq(rcv.rowq[0].base, int(i*m_number_cols));
1592 ConfigureGroup(rcv.rowq[i], ibase, 1, m_number_cols);
1593 }
1594
1595 #if ENABLE_HEAVY_LOGGING
1596 LOGC(pflog.Debug, log << "FEC: ROW STATS AFTER: n=" << rcv.rowq.size());
1597
1598 for (size_t i = 0; i < rcv.rowq.size(); ++i)
1599 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.rowq[i].DisplayStats());
1600 #endif
1601
1602 return rowx;
1603 }
1604
1605 int FECFilterBuiltin::RcvGetRowGroupIndex(int32_t seq, EHangStatus& w_status)
1606 {
1607 RcvGroup& head = rcv.rowq[0];
1608 const int32_t base = head.base;
1609
1610 const int offset = CSeqNo::seqoff(base, seq);
1611
1612 // Discard the packet, if older than base.
1613 if (offset < 0)
1614 {
1615 HLOGC(pflog.Debug, log << "FEC/H: Packet %" << seq << " is in the past, ignoring");
1616 w_status = HANG_PAST;
1617 return -1;
1618 }
1619
1620 // Hang in the receiver group first.
1621 size_t rowx = offset / m_number_cols;
1622
1623 /*
1624 Don't.
1625 Leaving this code for future if needed, but this check should not be done.
1626 The resource management for "crazy" sequence numbers is done in the beginning,
1627 so simply TRUST THIS SEQUENCE, no matter what. After the check it won't do any harm.
1628 if (rowx > numberRows()*2) // past twice the matrix
1629 {
1630 LOGC(pflog.Error, log << "FEC/H: Packet %" << seq << " is in the far future, ignoring");
1631 return -1;
1632 }
1633 */
1634
1635 // The packet might have come completely out of the blue.
1636 // The row group container must be prepared to extend
1637 // itself in order to give place for the packet.
1638
1639 // First, possibly extend the row container
1640 if (rowx >= rcv.rowq.size())
1641 {
1642 // Never returns -1
1643 rowx = ExtendRows(rowx);
1644 }
1645
1646 w_status = HANG_SUCCESS;
1647 return int(rowx);
1648 }
1649
1650 void FECFilterBuiltin::MarkCellReceived(int32_t seq, ECellReceived is_received)
1651 {
1652 // Mark the packet as received. This will allow later to
1653 // determine, which exactly packet is lost and needs rebuilding.
1654 const int cellsize = int(rcv.cells.size());
1655 const int cell_offset = CSeqNo::seqoff(rcv.cell_base, seq);
1656 bool resized SRT_ATR_UNUSED = false;
1657 if (cell_offset >= cellsize)
1658 {
1659 // Expand the cell container with zeros, excluding the 'cell_offset'.
1660 // Resize normally up to the required size, just set the lastmost
1661 // item to true.
1662 resized = true;
1663 rcv.cells.resize(cell_offset+1, false);
1664 }
1665
1666 if (resized || is_received != CELL_EXTEND)
1667 {
1668 // In both RECEIVED and REMOVE cases, forcefully set the value always.
1669 // In EXTEND, only if it was received
1670 // Value set should be true only if RECEIVED, false otherwise
1671 rcv.cells[cell_offset] = (is_received == CELL_RECEIVED);
1672 }
1673
1674 #if ENABLE_HEAVY_LOGGING
1675 static string const cellop [] = { "RECEIVED", "EXTEND", "REMOVE" };
1676 LOGC(pflog.Debug, log << "FEC: MARK CELL " << cellop[is_received]
1677 << "(" << (rcv.cells[cell_offset] ? "SET" : "CLR") << ")"
1678 << ": %" << seq << " - cells base=%"
1679 << rcv.cell_base << "[" << cell_offset << "]+" << rcv.cells.size()
1680 << (resized ? "(resized)":"") << " :");
1681 #endif
1682
1683 DebugPrintCells(rcv.cell_base, rcv.cells, sizeRow());
1684 }
1685
1686 bool FECFilterBuiltin::IsLost(int32_t seq) const
1687 {
1688 const int offset = CSeqNo::seqoff(rcv.cell_base, seq);
1689 if (offset < 0)
1690 {
1691 LOGC(pflog.Error, log << "FEC: IsLost: IPE: %" << seq
1692 << " is earlier than the cell base %" << rcv.cell_base);
1693 return true; // This might be due to emergency shrinking; pretend the packet is lost
1694 }
1695 if (offset >= int(rcv.cells.size()))
1696 {
1697 // XXX IPE!
1698 LOGC(pflog.Error, log << "FEC: IsLost: IPE: %" << seq << " is past the cells %"
1699 << rcv.cell_base << " + " << rcv.cells.size());
1700 return false; // Don't notify it yet
1701 }
1702
1703 return rcv.cells[offset];
1704 }
1705
1706 bool FECFilterBuiltin::CheckEmergencyShrink(size_t n_series, size_t size_in_packets)
1707 {
1708 // The minimum required size of the covered sequence range must be such
1709 // that groups for packets from the previous range must be still reachable.
1710 // It's then "this and previous" series in case of even arrangement.
1711 //
1712 // For staircase arrangement the potential range for a single column series
1713 // (number of columns equal to a row size) spans for 2 matrices (rows * cols)
1714 // minus one row. As dismissal is only allowed to be done by one full series
1715 // of rows and columns, the history must keep as many groups as needed to reach
1716 // out for this very packet of this group and all packets in the same row.
1717 // Hence we need two series of columns to cover a similar range as two row, twice.
1718
1719 const size_t min_series_history = m_arrangement_staircase ? 4 : 2;
1720
1721 if (n_series <= min_series_history)
1722 return false;
1723
1724 if (size_in_packets < rcvBufferSize() && n_series < SRT_FEC_MAX_RCV_HISTORY)
1725 return false;
1726
1727 // Shrink is required in order to prepare place for
1728 // either vertical or horizontal group in series `n_series`.
1729
1730 // The n_series can be calculated as:
1731 // n_series = colgx / numberCols()
1732 // n_series = rowgx / numberRows()
1733 //
1734 // The (Column or Row) Group Index value is calculated as
1735 // the number of column where the desired sequence number
1736 // should be located towards the very first container item
1737 // (row/column 0).
1738
1739 // The task for this function is to leave only one series
1740 // of groups and therefore initialize the containers. Likely
1741 // the part that contains the last series should be already
1742 // there, so in this case just remove some initial items from
1743 // the container so that only those remain that are intended
1744 // to remain. However, by various reasons (like e.g. that all
1745 // packets from the whole series have been lost) particular
1746 // container (colq, rowq, cell) doesn't contain this last
1747 // series at all. In that case clear the container completely
1748 // and just add an initial configuration for the first part
1749 // (which will be then dynamically extended as packets come in).
1750
1751 const int32_t oldbase = rcv.colq[0].base;
1752 const size_t shift_series = n_series - 1;
1753
1754 // This is simply a situation when the size is so excessive
1755 // that it couldn't be withstood by the receiver buffer, so
1756 // even if this isn't an extremely big size for allocation for
1757 // FEC, it doesn't make sense anyway.
1758 //
1759 // Minimum of 2 series must remain in the group container,
1760 // otherwise there's no need to guard the size.
1761
1762 // This requires simply resetting all group containers to
1763 // the very initial state, just take the calculated base seq
1764 // from the value of colgx reset to column 0.
1765
1766 // As colgx is calculated by stating that colgx == 0 represents
1767 // the very first cell in the column groups, take this, shift
1768 // by the number of series.
1769
1770 // SHIFT BY: n_series * matrix size
1771 // n_series is at least 2 (see condition)
1772 const size_t shift = shift_series * numberCols() * numberRows();
1773
1774 // Always positive: colgx, and so n_series, and so shift
1775 const int32_t newbase = CSeqNo::incseq(oldbase, int(shift));
1776
1777 const size_t shift_rows = shift_series * numberRows();
1778
1779 bool need_reset = rcv.rowq.size() < shift_rows;
1780 if (!need_reset)
1781 {
1782 // Sanity check - you should have the exact value
1783 // of `newbase` at the next series beginning position
1784 if (rcv.rowq[numberRows()].base != newbase)
1785 {
1786 LOGC(pflog.Error, log << "FEC: IPE: row start at %" << rcv.rowq[0].base << " next series %" << rcv.rowq[numberRows()].base
1787 << " (expected %" << newbase << "). RESETTING ROWS.");
1788 need_reset = true;
1789 }
1790 }
1791
1792 if (need_reset)
1793 {
1794 rcv.rowq.clear();
1795 // This n_series is the number rounded downwards,
1796 // So you just need to prepare place for ONE series.
1797 // The procedure below will extend them to the required
1798 // size for the received colgx.
1799 rcv.rowq.resize(1);
1800
1801 HLOGC(pflog.Debug, log << "FEC: Reset recv row %" << oldbase << " -> %" << newbase << ", INIT ROWS:");
1802 ConfigureGroup(rcv.rowq[0], newbase, 1, sizeRow());
1803 }
1804 else
1805 {
1806 HLOGC(pflog.Debug, log << "FEC: Shifting rcv row %" << oldbase << " -> %" << newbase);
1807 rcv.rowq.erase(rcv.rowq.begin(), rcv.rowq.begin() + shift_rows);
1808 }
1809
1810 const size_t shift_cols = shift_series * numberCols();
1811 need_reset = rcv.colq.size() < shift_cols;
1812 if (!need_reset)
1813 {
1814 // Sanity check - you should have the exact value
1815 // of `newbase` at the next series beginning position
1816 if (rcv.colq[numberCols()].base != newbase)
1817 {
1818 LOGC(pflog.Error, log << "FEC: IPE: col start at %" << rcv.colq[0].base << " next series %" << rcv.colq[numberCols()].base
1819 << " (expected %" << newbase << "). RESETTING ROWS.");
1820 need_reset = true;
1821 }
1822 }
1823
1824 if (need_reset)
1825 {
1826 rcv.colq.clear();
1827 HLOGC(pflog.Debug, log << "FEC: Reset recv row %" << oldbase << " -> %" << newbase << ", INIT first " << numberCols() << ":");
1828 ConfigureColumns(rcv.colq, newbase);
1829 }
1830
1831 if (rcv.cells.size() > shift)
1832 {
1833 rcv.cells.erase(rcv.cells.begin(), rcv.cells.begin() + shift);
1834 }
1835 else
1836 {
1837 rcv.cells.clear();
1838 rcv.cells.push_back(false);
1839 }
1840 rcv.cell_base = newbase;
1841
1842 return true;
1843 }
1844
1845 FECFilterBuiltin::EHangStatus FECFilterBuiltin::HangVertical(const CPacket& rpkt, signed char fec_col, loss_seqs_t& irrecover)
1846 {
1847 bool fec_ctl = (fec_col != -1);
1848 // Now hang the packet in the vertical group
1849
1850 const int32_t seq = rpkt.getSeqNo();
1851
1852 // Ok, now we have the column index, we know it exists.
1853 // Apply the packet.
1854
1855 EHangStatus stat;
1856 const int colgx = RcvGetColumnGroupIndex(seq, (stat));
1857 if (colgx == -1)
1858 return stat;
1859
1860 RcvGroup& colg = rcv.colq[colgx];
1861
1862 if (fec_ctl)
1863 {
1864 if (!colg.fec)
1865 {
1866 ClipControlPacket(colg, rpkt);
1867 colg.fec = true;
1868 HLOGC(pflog.Debug, log << "FEC/V: FEC/CTL packet clipped, %" << seq << " FOR COLUMN " << int(fec_col)
1869 << " base=%" << colg.base);
1870 }
1871 else
1872 {
1873 HLOGC(pflog.Debug, log << "FEC/V: FEC/CTL at %" << seq << " COLUMN " << int(fec_col) << " DUPLICATED, skipping.");
1874 }
1875 }
1876 else
1877 {
1878 // Data packet, clip it as data
1879 ClipPacket(colg, rpkt);
1880 colg.collected++;
1881 HLOGC(pflog.Debug, log << "FEC/V: DATA packet clipped, %" << seq
1882 << ", received " << colg.collected << "/" << sizeCol()
1883 << " base=%" << colg.base);
1884 }
1885
1886 if (colg.fec && colg.collected == m_number_rows - 1)
1887 {
1888 HLOGC(pflog.Debug, log << "FEC/V: HAVE " << colg.collected << " collected & FEC; REBUILDING");
1889 RcvRebuild(colg, RcvGetLossSeqVert(colg), Group::VERT);
1890 }
1891
1892 // Column dismissal takes place under very strictly specified condition,
1893 // so simply call it in general here. At least it may happen potentially
1894 // at any time of when a packet has been received.
1895 RcvCheckDismissColumn(rpkt.getSeqNo(), colgx, irrecover);
1896
1897 #if ENABLE_HEAVY_LOGGING
1898 LOGC(pflog.Debug, log << "FEC: COL STATS ATM: n=" << rcv.colq.size());
1899
1900 for (size_t i = 0; i < rcv.colq.size(); ++i)
1901 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.colq[i].DisplayStats());
1902 #endif
1903
1904 return HANG_SUCCESS;
1905 }
1906
1907 void FECFilterBuiltin::RcvCheckDismissColumn(int32_t seq, int colgx, loss_seqs_t& irrecover)
1908 {
1909 // The first check we need to do is:
1910 //
1911 // - get the column number
1912 // - get the series for this column
1913 // - if series is 0, just return
1914
1915 const size_t series = colgx / numberCols();
1916 if (series == 0)
1917 return;
1918
1919 // - STARTING from the same column series 0:
1920 // - unless DISMISSED, collect all irrecoverable packets from this group
1921 // - mark this column DISMISSED
1922 // - SAME CHECK for previous group, until index 0
1923
1924 set<int32_t> loss;
1925
1926 size_t colx SRT_ATR_UNUSED = colgx % numberCols();
1927
1928 HLOGC(pflog.Debug, log << "FEC/V: going to DISMISS cols past %" << seq
1929 << " at INDEX=" << colgx << " col=" << colx
1930 << " series=" << series << " - looking up candidates...");
1931
1932 // Walk through all column groups in series 0. Collect irrecov's
1933 // from every group, for which the incoming 'seq' is in future.
1934 for (size_t i = 0; i < numberCols(); ++i)
1935 {
1936 RcvGroup& pg = rcv.colq[i];
1937 if (pg.dismissed)
1938 {
1939 HLOGC(pflog.Debug, log << "FEC/V: ... [" << i << "] base=%"
1940 << pg.base << " ALREADY DISMISSED, skipping.");
1941 continue;
1942 }
1943
1944 // With multi-staircase it may happen that THIS column contains
1945 // sequences that are all in the past, but the PREVIOUS column
1946 // has some in the future, because THIS column is the top of
1947 // the second staircase, and PREVIOUS is the bottom stair of
1948 // the first staircase. When this is confirmed, simply skip
1949 // the columns that have the highest sequence in the future
1950 // because they can't be dismissed yet. Jump them over, so maybe
1951 // they can be dismissed in future.
1952 int this_col_offset = CSeqNo::seqoff(pg.base, seq);
1953 int last_seq_offset = this_col_offset - int((sizeCol()-1)*sizeRow());
1954
1955 if (last_seq_offset < 0)
1956 {
1957 HLOGC(pflog.Debug, log << "FEC/V: ... [" << i << "] base=%"
1958 << pg.base << " TOO EARLY (last=%"
1959 << CSeqNo::incseq(pg.base, (int32_t)((sizeCol()-1)*sizeRow()))
1960 << ")");
1961 continue;
1962 }
1963
1964 // NOTE: If it was standing on the second staircase top, there's
1965 // still a chance that it hits the staircase top of the first
1966 // staircase and will dismiss it as well.
1967
1968 HLOGC(pflog.Debug, log << "FEC/V: ... [" << i << "] base=%"
1969 << pg.base << " - PAST last=%"
1970 << CSeqNo::incseq(pg.base, (int32_t)((sizeCol()-1)*sizeRow()))
1971 << " - collecting losses.");
1972
1973 pg.dismissed = true; // mark irrecover already collected
1974 for (size_t sof = 0; sof < pg.step * sizeCol(); sof += pg.step)
1975 {
1976 int32_t lseq = CSeqNo::incseq(pg.base, int(sof));
1977 if (!IsLost(lseq))
1978 {
1979 loss.insert(lseq);
1980 HLOGC(pflog.Debug, log << "FEC: ... cell +" << sof << " %" << lseq
1981 << " lost");
1982 }
1983 else
1984 {
1985 HLOGC(pflog.Debug, log << "FEC: ... cell +" << sof << " %" << lseq
1986 << " EXISTS");
1987 }
1988 }
1989 }
1990
1991 // COLUMN DISMISAL:
1992
1993 // 1. We can only dismiss ONE SERIES OF COLUMNS - OR NOTHING.
1994 // 2. The triggering 'seq' must be past ANY sequence embraced
1995 // by any group in the first series of columns.
1996
1997 // Useful information:
1998 //
1999 // 1. It's not known from upside, which column contains a sequence
2000 // number that reaches FURTHEST. The safe statement is then:
2001 // - For even arrangement, it must be past BASE0 + matrix size
2002 // - For staircase arrangement - BASE0 + matrix size * 2.
2003
2004 int32_t base0 = rcv.colq[0].base;
2005 int this_off = CSeqNo::seqoff(base0, seq);
2006
2007 int mindist = int(
2008 m_arrangement_staircase ?
2009 (numberCols() * numberRows() * 2)
2010 :
2011 (numberCols() * numberRows()));
2012
2013 bool any_dismiss SRT_ATR_UNUSED = false;
2014
2015 // Here's a change.
2016 // The number of existing column groups is supposed to always cover
2017 // at least one full series, whereas the number of row groups are
2018 // created always one per necessity, so the number of existing row
2019 // groups may be less than required for a full series, whereas here
2020 // it is intended to simply dismiss groups for full series. This may
2021 // cause that it is aiming for removing more row groups than currently
2022 // exist. This is completely ok, as the sequence that triggered removal
2023 // is long past these series anyway, so the groups for packets that will
2024 // never be received makes no sense. Simply accept this state and delete
2025 // all row groups and reinitialize them into the new base, where the base
2026 // is the current base for column 0 group.
2027 //
2028 // Therefore dismissal is triggered whenever you have a cover of one column
2029 // series. If the number of row groups doesn't cover it, simply delete all
2030 // row groups, that's all.
2031
2032 // if (base0 +% mindist) <% seq
2033 if (this_off < mindist) // COND 1: minimum remaining
2034 {
2035 HLOGC(pflog.Debug, log << "FEC/V: NOT dismissing any columns at %" << seq
2036 << ", need to pass %" << CSeqNo::incseq(base0, mindist));
2037 }
2038 else if (rcv.colq.size() - 1 < numberCols()) // COND 2: full matrix in columns
2039 {
2040 #if ENABLE_HEAVY_LOGGING
2041 LOGC(pflog.Debug, log << "FEC/V: IPE: about to dismiss past %" << seq
2042 << " with required %" << CSeqNo::incseq(base0, mindist)
2043 << " but col container size still " << rcv.colq.size() << "; COL STATS:");
2044 for (size_t i = 0; i < rcv.colq.size(); ++i)
2045 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.colq[i].DisplayStats());
2046 #endif
2047 }
2048 else
2049 {
2050 // The condition for dismissal is now. The number of dismissed columns
2051 // is numberCols(), regardless of the required 'mindinst'.
2052 any_dismiss = true;
2053
2054 const int32_t newbase = rcv.colq[numberCols()].base;
2055 int32_t newbase_row SRT_ATR_UNUSED; // For logging only, but including FATAL.
2056 // Sanity check
2057 // If sanity check failed OR if the number of existing row
2058 // groups doesn't enclose those that need to be dismissed,
2059 // clear row groups completely - these packets are lost and
2060 // irrecoverable anyway.
2061 bool insane = false;
2062 bool undercounted = false;
2063
2064 if (rcv.rowq.size() - 1 < numberRows()) // COND 3: full matrix in rows
2065 {
2066 // Do not reach to index=numberRows() because it doesn't exist.
2067 // Take the value from the columns as a good deal - actually
2068 // row base and col base shall be always in sync.
2069 newbase_row = newbase;
2070 undercounted = true;
2071 }
2072 else
2073 {
2074 newbase_row = rcv.rowq[numberRows()].base;
2075 insane = newbase_row != newbase;
2076 }
2077 const size_t matrix_size = numberCols() * numberRows();
2078
2079 HLOGC(pflog.Debug, log << "FEC/V: DISMISSING " << numberCols() << " COLS. Base %"
2080 << rcv.colq[0].base << " -> %" << newbase
2081 << " AND " << numberRows() << " ROWS Base %"
2082 << rcv.rowq[0].base << " -> %" << newbase_row
2083 << " AND " << matrix_size << " cells");
2084
2085 // ensured existence of the removed range: see COND 2 above.
2086 rcv.colq.erase(rcv.colq.begin(), rcv.colq.begin() + numberCols());
2087
2088 #if ENABLE_HEAVY_LOGGING
2089 LOGC(pflog.Debug, log << "FEC: COL STATS BEFORE: n=" << rcv.colq.size());
2090
2091 for (size_t i = 0; i < rcv.colq.size(); ++i)
2092 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.colq[i].DisplayStats());
2093 #endif
2094
2095 // Now erase accordingly one matrix of rows.
2096 if (insane || undercounted)
2097 {
2098 if (insane)
2099 {
2100 LOGC(pflog.Fatal, log << "FEC/V: IPE: DISCREPANCY in new base0 col=%"
2101 << newbase << " row=%" << newbase_row << " - DELETING ALL ROWS");
2102 }
2103 else
2104 {
2105
2106 #if ENABLE_HEAVY_LOGGING
2107 LOGC(pflog.Debug, log << "FEC/V: about to dismiss past %" << seq
2108 << " with required %" << CSeqNo::incseq(base0, mindist)
2109 << " but row container size still " << rcv.rowq.size() << " (will clear to %" << newbase << " instead); ROW STATS:");
2110 for (size_t i = 0; i < rcv.rowq.size(); ++i)
2111 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.rowq[i].DisplayStats());
2112 #endif
2113 }
2114
2115 // Delete all rows and reinitialize them.
2116 rcv.rowq.clear();
2117 rcv.rowq.resize(1);
2118 ConfigureGroup(rcv.rowq[0], newbase, 1, sizeRow());
2119 }
2120 else
2121 {
2122 // Remove "legally" a matrix of rows.
2123 // ensured existence of the removed range: see COND 3 above
2124 rcv.rowq.erase(rcv.rowq.begin(), rcv.rowq.begin() + numberRows());
2125 }
2126
2127 // And now accordingly remove cells. Exactly one matrix of cells.
2128 // Sanity check first.
2129 int32_t newbase_cell = CSeqNo::incseq(rcv.cell_base, int32_t(matrix_size));
2130 if (newbase != newbase_cell)
2131 {
2132 LOGC(pflog.Fatal, log << "FEC/V: IPE: DISCREPANCY in new base0 col=%"
2133 << newbase << " cell_base=%" << newbase_cell << " - DELETING ALL CELLS");
2134
2135 // Try to shift it gently first. Find the cell that matches the base.
2136 int shift = CSeqNo::seqoff(rcv.cell_base, newbase);
2137 if (shift < 0 || size_t(shift) > rcv.cells.size())
2138 rcv.cells.clear();
2139 else
2140 rcv.cells.erase(rcv.cells.begin(), rcv.cells.begin() + shift);
2141 }
2142 else
2143 {
2144 if (rcv.cells.size() <= size_t(matrix_size))
2145 rcv.cells.clear();
2146 else
2147 rcv.cells.erase(rcv.cells.begin(), rcv.cells.begin() + matrix_size);
2148 }
2149 rcv.cell_base = newbase;
2150 DebugPrintCells(rcv.cell_base, rcv.cells, sizeRow());
2151 }
2152
2153 /*
2154 OLD UNUSED CODE, leaving for historical reasons
2155
2156 // - check the last sequence of last column in series 0
2157 // - if passed sequence number is earlier than this, just return
2158 // - now that seq is newer than the last in the last column,
2159 // - dismiss whole series 0 column groups
2160
2161 // First, index of the last column
2162 size_t lastx = numberCols()-1;
2163 if (lastx < rcv.colq.size())
2164 {
2165 int32_t lastbase = rcv.colq[lastx].base;
2166
2167 // Compare the base sequence with the sequence that caused the update
2168 int dist = CSeqNo::seqoff(lastbase, seq);
2169
2170 // Shift this distance by the distance between the first and last
2171 // sequence managed by a singled column. This counts (sizeCol()-1)*step.
2172 dist -= (sizeCol()-1) * rcv.colq[lastx].step;
2173
2174 // Now, if this value is in the past (negative), it means that the
2175 // 'seq' number is covered by this group or any earlier group. If so,
2176 // do nothing. If this value is positive, it means that this
2177 // sequence is in future towards the group that is in the last
2178 // column of series 0. If so, whole series 0 may be now dismissed.
2179
2180 // NOTE: we don't care if lost packets have been collected for
2181 // the groups being dismissed. They *SHOULD* be, just as a fallback
2182 // SRT - if needed - will simply send LOSSREPORT request for all
2183 // packets that are lossreported and all older ones.
2184
2185 if (dist > 0 && rcv.colq.size() > numberCols() )
2186 {
2187 any_dismiss = true;
2188 int32_t newbase = rcv.colq[numberCols()].base;
2189 rcv.colq.erase(rcv.colq.begin(), rcv.colq.begin() + numberCols());
2190
2191 // colgx is INVALIDATED after removal
2192 int newcolgx SRT_ATR_UNUSED = colgx - numberCols();
2193
2194 // After a column series was dismissed, now dismiss also
2195 // the same number of rows.
2196 // Do some sanity checks first.
2197
2198 size_t nrowrem = 0;
2199 int32_t oldrowbase = rcv.rowq[0].base; // before it gets deleted
2200 if (rcv.rowq.size() > numberRows())
2201 {
2202 int32_t newrowbase = rcv.rowq[numberRows()].base;
2203 if (newbase != newrowbase)
2204 {
2205 LOGC(pflog.Error, log << "FEC: IPE: ROW/COL base DISCREPANCY: Looking up lineraly for the right row.");
2206
2207 // Fallback implementation in order not to break everything
2208 for (size_t r = 0; r < rcv.rowq.size(); ++r)
2209 {
2210 if (CSeqNo::seqoff(newbase, rcv.rowq[r].base) >= 0)
2211 {
2212 rcv.rowq.erase(rcv.rowq.begin(), rcv.rowq.begin() + r);
2213 nrowrem = r;
2214 break;
2215 }
2216 }
2217 }
2218 else
2219 {
2220 rcv.rowq.erase(rcv.rowq.begin(), rcv.rowq.begin() + numberRows());
2221 nrowrem = numberRows();
2222 }
2223 }
2224
2225 // If rows were removed, so remove also cells
2226 if (nrowrem > 0)
2227 {
2228 int32_t newbase = rcv.rowq[0].base;
2229
2230 // This value SHOULD be == nrowrem * sizeRow(), but this
2231 // calculation is safe against bugs. Report them, if found, though.
2232 int nrem = CSeqNo::seqoff(rcv.cell_base, newbase);
2233
2234 if (oldrowbase != rcv.cell_base)
2235 {
2236 LOGC(pflog.Error, log << "FEC: CELL/ROW base discrepancy, calculating and resynchronizing");
2237 }
2238 else
2239 {
2240 HLOGC(pflog.Debug, log << "FEC: will remove " << nrem << " cells, SHOULD BE = "
2241 << (nrowrem * sizeRow()));
2242 }
2243
2244 if (nrem > 0)
2245 {
2246 // Now collect losses from all rows about to be dismissed.
2247 for (int sof = 0; sof < nrem; sof++)
2248 {
2249 int32_t lseq = CSeqNo::incseq(rcv.cell_base, sof);
2250 if (!IsLost(lseq))
2251 loss.insert(lseq);
2252 }
2253
2254 HLOGC(pflog.Debug, log << "FEC: ERASING unused cells (" << nrem << "): %"
2255 << rcv.cell_base << " - %" << newbase
2256 << ", losses collected: " << Printable(loss));
2257
2258 rcv.cells.erase(rcv.cells.begin(), rcv.cells.begin() + nrem);
2259 rcv.cell_base = newbase;
2260
2261 DebugPrintCells(rcv.cell_base, rcv.cells, sizeRow());
2262 }
2263 else
2264 {
2265 HLOGC(pflog.Debug, log << "FEC: NOT ERASING cells, base %" << rcv.cell_base
2266 << " vs row base %" << rcv.rowq[0].base);
2267 }
2268 }
2269
2270 HLOGC(pflog.Debug, log << "FEC/V: updated g=" << colgx << " -> " << newcolgx << " %"
2271 << rcv.colq[newcolgx].base << ", DISMISS up to g=" << numberCols()
2272 << " base=%" << lastbase
2273 << " ROW=%" << rcv.rowq[0].base << "+" << nrowrem);
2274
2275 }
2276 }
2277
2278 // */
2279
2280 // Now all collected lost packets translate into the range list format
2281 TranslateLossRecords(loss, irrecover);
2282
2283 HLOGC(pflog.Debug, log << "FEC: ... COLLECTED IRRECOVER: " << Printable(loss) << (any_dismiss ? " CELLS DISMISSED" : " nothing dismissed"));
2284 }
2285
2286 void FECFilterBuiltin::TranslateLossRecords(const set<int32_t>& loss, loss_seqs_t& irrecover)
2287 {
2288 if (loss.empty())
2289 return;
2290
2291 // size() >= 1 granted
2292 set<int32_t>::iterator i = loss.begin();
2293
2294 int32_t fi_start = *i;
2295 int32_t fi_end = fi_start;
2296 ++i;
2297 for (; i != loss.end(); ++i)
2298 {
2299 int dist = CSeqNo::seqoff(fi_end, *i);
2300 if (dist == 1)
2301 ++fi_end;
2302 else
2303 {
2304 // Jumped over some sequences, cut the range.
2305 irrecover.push_back(make_pair(fi_start, fi_end));
2306 fi_start = fi_end = *i;
2307 }
2308 }
2309
2310 // And ship the last one
2311 irrecover.push_back(make_pair(fi_start, fi_end));
2312 }
2313
2314 int FECFilterBuiltin::RcvGetColumnGroupIndex(int32_t seqno, EHangStatus& w_status)
2315 {
2316 // The column is only the column, not yet
2317 // exactly the index of the column group in the container.
2318 // It's like:
2319
2320 // 0 1 2 3 4
2321 // [A] ' ' ' '
2322 // [A] [A] ' ' '
2323 // [A] [A] [A] ' '
2324 // [A] [A] [A] [A] '++
2325 // [A]+[A] [A] [A] [A]+
2326 // [B] [A]+[A] [A] [A]+
2327 // [B] [B] [A]+[A] [A]+
2328 // [B] [B] [B] [A]+[A]+
2329 // [B] [B] [B] [B] [A]++
2330 // [B]+[B] [B] [B] [B]
2331 // [B] [B] [B] [B]
2332 // [B] [B] [B]
2333 // [B] [B]
2334 // [B]
2335 //
2336 // The same groups laid out in the container:
2337 //
2338 // [A]0 [A]1 [A]2 [A]3 [A]4 [B]0 [B]1 [B]2 [B]3 [B]4
2339
2340 // This means, vert_gx returns the number for the
2341 // logical column, but we only know this column,
2342 // not the exact group that is assigned to this
2343 // packet. That is, in the above picture, if the
2344 // vert_gx resulted in 3, we still don't know if
2345 // this is A3 or B3.
2346 //
2347 // To know it, we need to first take the base
2348 // sequence number for the very first column group
2349 // in the container. Every next group in this
2350 // container is shifted by the 'slip' factor,
2351 // which in this case is m_number_cols + 1. The base
2352 // sequence shifted by m_number_cols*m_number_rows
2353 // becomes the sequence number of the next
2354 // group in the same column.
2355 //
2356 // If a single column is dismissed, then the
2357 // column 1 will become column 0, but the column
2358 // 1 in the next series will also become column 0.
2359 // All due to that the very base sequence for
2360 // all groups will be the one in the first series
2361 // column 1, now 0.
2362 //
2363 // Therefore, once we have the column, let's extract
2364 // the column base sequence.
2365 // As we can't count on that packets will surely come to close a group
2366 // or just a particular sequence number will be received, we simply have
2367 // to check all past groups at the moment when this packet is received:
2368 //
2369 // 1. Take the sequence number and determine both the GROUP INDEX and the
2370 // COLUMN INDEX of this group.
2371 //
2372 // Important thing here is that the column group base is the base sequence
2373 // number of the very first group, which NEED NOT GO HAND IN HAND with the
2374 // row group sequence base. The rules are then:
2375 //
2376 // OFFSET = DISTANCE( colq[0].base TO seq )
2377 //
2378 // COLUMN_INDEX = OFFSET % m_number_cols
2379 //
2380 // COLUMN_BASE = colq[COLUMN_INDEX].base
2381 //
2382 // COLUMN_OFFSET = DISTANCE(COLUMN_BASE TO seq)
2383 //
2384 // COLUMN_SERIES = COLUMN_OFFSET / (m_number_cols * m_number_rows)
2385 //
2386 // GROUP_INDEX = COLUMN_INDEX + (COLUMN_SERIES * m_number_cols)
2387
2388 const int offset = CSeqNo::seqoff(rcv.colq[0].base, seqno);
2389 if (offset < 0)
2390 {
2391 HLOGC(pflog.Debug, log << "FEC/V: %" << seqno << " in the past of col ABSOLUTE base %" << rcv.colq[0].base);
2392 w_status = HANG_PAST;
2393 return -1;
2394 }
2395
2396 if (offset > CSeqNo::m_iSeqNoTH/2)
2397 {
2398 LOGC(pflog.Error, log << "FEC/V: IPE/ATTACK: pkt %" << seqno << " has CRAZY OFFSET towards the base %" << rcv.colq[0].base);
2399 w_status = HANG_CRAZY;
2400 return -1;
2401 }
2402
2403 const int colx = offset % m_number_cols;
2404 const int32_t colbase = rcv.colq[colx].base;
2405 const int coloff = CSeqNo::seqoff(colbase, seqno);
2406 if (coloff < 0)
2407 {
2408 HLOGC(pflog.Debug, log << "FEC/V: %" << seqno << " in the past of col #" << colx << " base %" << colbase);
2409 // This means that this sequence number predates the earliest
2410 // sequence number supported by the very first column.
2411 w_status = HANG_PAST;
2412 return -1;
2413 }
2414
2415 const int colseries = coloff / int(m_number_cols * m_number_rows);
2416 size_t colgx = colx + int(colseries * m_number_cols);
2417
2418 HLOGC(pflog.Debug, log << "FEC/V: Lookup group for %" << seqno << ": cg_base=%" << rcv.colq[0].base
2419 << " column=" << colx << " with base %" << colbase << ": SERIES=" << colseries
2420 << " INDEX:" << colgx);
2421
2422 // Check oversize. Dismiss some earlier items if it exceeds the size.
2423 // before you extend the size enormously.
2424 if (colgx > m_number_rows * m_number_cols * SRT_FEC_MAX_RCV_HISTORY)
2425 {
2426 // That's too much
2427 LOGC(pflog.Error, log << "FEC/V: IPE or ATTACK: offset " << colgx << " is too crazy, ABORTING lookup");
2428 w_status = HANG_CRAZY;
2429 return -1;
2430 }
2431
2432 if (colgx >= rcv.colq.size())
2433 {
2434 colgx = ExtendColumns(colgx);
2435 }
2436 w_status = HANG_SUCCESS;
2437 return int(colgx);
2438
2439 //
2440 // Even though column groups are arranged in a "staircase", it only means
2441 // that DISTANCE(colq[0].base TO colq[1].base) == 1 + m_number_cols, not 1.
2442 // But still, this DISTANCE(...) % m_number_cols == 1. So:
2443 //
2444 // COLUMN_INDEX = GROUP_INDEX % m_number_cols
2445 //
2446 // What is special here is that, group base sequence numbers with m_number_cols == 5
2447 // is, for example, these column groups in order of how they are arranged in
2448 // the container have their [INDEX] BASE (stating the very first has base == 10):
2449 //
2450 // [0] 10, [1] 16, [2] 22, [3] 28, [4] 34, [5] 40, [6] 46
2451 //
2452 // Therefore, if we get the sequence number 51, then:
2453 //
2454 // OFFSET = 51 - 10 = 41
2455 //
2456 // COLUMN_INDEX = 41 % 5[m_number_cols] == 1
2457 //
2458 // COLUMN_BASE = colq[1].base == 16
2459 //
2460 // COLUMN_OFFSET = DISTANCE(colq[1].base TO 51) = 51 - 16 = 35
2461 //
2462 // COLUMN_SERIES = COLUMN_OFFSET / (m_number_cols * m_number_rows)
2463 // = 35 / (5*5) = 35/25 = 1
2464 //
2465 // GROUP_INDEX = COLUMN_INDEX + (COLUMN_SERIES * m_number_cols)
2466 // = 1 + 1*5 = 6
2467 //
2468 // --> We have identified column group with index 6, this one
2469 // that is designated above as [6] 46. This column group collects
2470 // the following sequence numbers:
2471 // - 46
2472 // - 51 (the one we were searching)
2473 // - 56
2474 // - 61
2475 // - 66
2476 //
2477 // We have then column group with index 6. Now we go backward in the column
2478 // group container starting from the direct previous column up to either
2479 // the start of container or the size == m_number_cols (5), to see if the
2480 // group "is closed". So:
2481 //
2482 // END = min(m_number_cols, colq.size()) <--- should be 5 or less, if there's less in the container
2483 //
2484 // FOR EACH i = 1 TO END:
2485 // g = colq[GROUP_INDEX - i]
2486 // gs = SHIFT(seq, -i)
2487 // gmax = SHIFT(g.base, m_number_cols * m_number_rows)
2488 // IF ( gs %> gmax )
2489 // DISMISS COLUMNS from 0 to GROUP_INDEX - i; break
2490 }
2491
2492 size_t FECFilterBuiltin::ExtendColumns(size_t colgx)
2493 {
2494 // This isn't safe to allow the group container to get expanded to any
2495 // size, however with some very tolerant settings, such as 10 seconds of
2496 // latency and very large receiver buffer, this might be tolerable.
2497 //
2498 // Therefore put only two conditions here:
2499 //
2500 // 1. The group containers must keep at most place for so many
2501 // packets as it is intended for the receiver buffer. Keeping
2502 // group cells for more packets doesn't make sense anyway.
2503 //
2504 // 2. Existing group containers should contain at least size
2505 // for two series. If they don't contain that much, there's no
2506 // need to do any emergency shrinking. Unknown whether this is
2507 // physically possible, although it may also happen in case when
2508 // you have very large FEC matrix size not coordinated with the
2509 // receiver buffer size.
2510
2511 // colgx is the number of column + NSERIES * numberCols().
2512 // We can state that for every column we should have a number
2513 // of packets as many as the number of rows, so simply multiply this.
2514 const size_t size_in_packets = colgx * numberRows();
2515 const size_t n_series = colgx / numberCols();
2516
2517 if (CheckEmergencyShrink(n_series, size_in_packets))
2518 {
2519 HLOGC(pflog.Debug, log << "FEC: DONE Emergency resize, colgx=" << colgx << " series=" << n_series
2520 << "npackets=" << size_in_packets << " exceeds buf=" << rcvBufferSize());
2521 }
2522 else
2523 {
2524 HLOGC(pflog.Debug, log << "FEC: Will extend up to colgx=" << colgx << " series=" << n_series
2525 << " for npackets=" << size_in_packets);
2526 }
2527
2528
2529 #if ENABLE_HEAVY_LOGGING
2530 LOGC(pflog.Debug, log << "FEC: COL STATS BEFORE: n=" << rcv.colq.size());
2531
2532 for (size_t i = 0; i < rcv.colq.size(); ++i)
2533 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.colq[i].DisplayStats());
2534 #endif
2535
2536 // First, obtain the "series" of columns, possibly fixed.
2537 const int series = int(colgx / numberCols());
2538
2539 // Now, the base of the series is the base increased by one matrix size.
2540
2541 const int32_t base = rcv.colq[0].base;
2542
2543 // This is the base for series 0, but this procedure must be prepared
2544 // for that the series will not necessarily be 1, may be greater.
2545 // Extension requires to be done in order to achieve this very index
2546 // existing in the column, so you need to add whole series in loop
2547 // until the series covering this shift is created.
2548
2549 // Check, up to which series the columns are initialized.
2550 // Start with the series that doesn't exist
2551 const int old_series = int(rcv.colq.size() / numberCols());
2552
2553 // Each iteration of this loop adds one series of columns.
2554 // One series count numberCols() columns.
2555 for (int s = old_series; s <= series; ++s)
2556 {
2557 // We start with the base in series 0, the calculation of the
2558 // sequence number must happen anew for each one anyway, so it
2559 // doesn't matter from which start point.
2560
2561 // Every base sequence for a series of columns is the series 0
2562 // base increased by one matrix size times series number.
2563 // THIS REMAINS TRUE NO MATTER IF WE USE STRAIGNT OR STAIRCASE ARRANGEMENT.
2564 const int32_t sbase = CSeqNo::incseq(base, int(numberCols()*numberRows()) * s);
2565 HLOGC(pflog.Debug, log << "FEC/V: EXTENDING column groups series " << s
2566 << ", size " << rcv.colq.size() << " -> "
2567 << (rcv.colq.size() + numberCols())
2568 << ", base=%" << base << " -> %" << sbase);
2569
2570 // Every call to this function extends the given container
2571 // by 'gsize' number and configures each so added column accordingly.
2572 ConfigureColumns(rcv.colq, sbase);
2573 }
2574
2575 #if ENABLE_HEAVY_LOGGING
2576 LOGC(pflog.Debug, log << "FEC: COL STATS BEFORE: n=" << rcv.colq.size());
2577
2578 for (size_t i = 0; i < rcv.colq.size(); ++i)
2579 LOGC(pflog.Debug, log << "... [" << i << "] " << rcv.colq[i].DisplayStats());
2580 #endif
2581
2582 return colgx;
2583 }
2584
2585 } // namespace srt