Debian Codegraph @

srtcore/fec.h

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
12 #ifndef INC_SRT_FEC_H
13 #define INC_SRT_FEC_H
14
15 #include <string>
16 #include <map>
17 #include <vector>
18 #include <deque>
19
20 #include "packetfilter_api.h"
21
22 namespace srt {
23
24 class FECFilterBuiltin: public SrtPacketFilterBase
25 {
26 SrtFilterConfig cfg;
27 size_t m_number_cols;
28 size_t m_number_rows;
29
30 // Configuration
31 SRT_ARQLevel m_fallback_level;
32 bool m_cols_only;
33 bool m_arrangement_staircase;
34
35 public:
36
37 size_t numberCols() const { return m_number_cols; }
38 size_t numberRows() const { return m_number_rows; }
39
40 size_t sizeCol() const { return m_number_rows; }
41 size_t sizeRow() const { return m_number_cols; }
42
43 struct Group
44 {
45 int32_t base; //< Sequence of the first packet in the group
46 size_t step; //< by how many packets the sequence should increase to get the next packet
47 size_t drop; //< by how much the sequence should increase to get to the next series
48 size_t collected; //< how many packets were taken to collect the clip
49
50 Group(): base(SRT_SEQNO_NONE), step(0), drop(0), collected(0)
51 {
52 }
53
54 uint16_t length_clip;
55 uint8_t flag_clip;
56 uint32_t timestamp_clip;
57 std::vector<char> payload_clip;
58
59 // This is mutable because it's an intermediate buffer for
60 // the purpose of output.
61 //mutable vector<char> output_buffer;
62
63 enum Type
64 {
65 HORIZ, // Horizontal, recursive
66 VERT, // Vertical, recursive
67
68 // NOTE: HORIZ/VERT are defined as 0/1 so that not-inversion
69 // can flip between them.
70 SINGLE // Horizontal-only with no recursion
71 };
72
73 static Type FlipType(Type t)
74 {
75 SRT_ASSERT(t != SINGLE);
76 return (t == HORIZ) ? VERT : HORIZ;
77 }
78
79 };
80
81 struct RcvGroup: Group
82 {
83 bool fec;
84 bool dismissed;
85 RcvGroup(): fec(false), dismissed(false) {}
86
87 #if ENABLE_HEAVY_LOGGING
88 std::string DisplayStats()
89 {
90 if (base == SRT_SEQNO_NONE)
91 return "UNINITIALIZED!!!";
92
93 std::ostringstream os;
94 os << "base=" << base << " step=" << step << " drop=" << drop << " collected=" << collected
95 << " " << (fec ? "+" : "-") << "FEC " << (dismissed ? "DISMISSED" : "active");
96 return os.str();
97 }
98 #endif
99 };
100
101 private:
102
103 // Row Groups: every item represents a single row group and collects clips for one row.
104 // Col Groups: every item represents a signel column group and collect clips for packets represented in one column
105
106 struct Send
107 {
108 // We need only ONE horizontal group. Simply after the group
109 // is closed (last packet supplied), and the FEC packet extracted,
110 // the group is no longer in use.
111 Group row;
112 std::vector<Group> cols;
113 } snd;
114
115 struct Receive
116 {
117 SRTSOCKET id;
118 bool order_required;
119
120 Receive(std::vector<SrtPacket>& provided): id(SRT_INVALID_SOCK), order_required(false), rebuilt(provided)
121 {
122 }
123
124 // In reception we need to keep as many horizontal groups as required
125 // for possible later tracking. A horizontal group should be dismissed
126 // when the size of this container exceeds the `m_number_rows` (size of the column).
127 //
128 // The 'std::deque' type is used here for a trial implementation. A desired solution
129 // would be a kind of a ring buffer where new groups are added and old (exceeding
130 // the size) automatically dismissed.
131 std::deque<RcvGroup> rowq;
132
133 // Base index at the oldest column platform determines
134 // the base index of the queue. Meaning, first you need
135 // to determine the column index, where the index 0 is
136 // the fistmost element of this queue. After determining
137 // the column index, there must be also a second factor
138 // determined - which column series it is. So, this can
139 // start by extracting the base sequence of the element
140 // at the index column. This is the series 0. Now, the
141 // distance between these two sequences, divided by
142 // rowsize*colsize should return %index-in-column,
143 // /number-series. The latter multiplied by the row size
144 // is the offset between the firstmost column and the
145 // searched column.
146 std::deque<RcvGroup> colq;
147
148 // This keeps the value of "packet received or not".
149 // The sequence number of the first cell is rowq[0].base.
150 // When dropping a row,
151 // - the firstmost element of rowq is removed
152 // - the length of one row is removed from this std::vector
153 int32_t cell_base;
154 std::deque<bool> cells;
155
156 // Note this function will automatically extend the container
157 // with empty cells if the index exceeds the size, HOWEVER
158 // the caller must make sure that this index isn't any "crazy",
159 // that is, it fits somehow in reasonable ranges.
160 bool CellAt(size_t index)
161 {
162 if (index >= cells.size())
163 {
164 // Cells not prepared for this sequence yet,
165 // so extend in advance.
166 cells.resize(index+1, false);
167 return false; // It wasn't marked, anyway.
168 }
169
170 return cells[index];
171 }
172
173 typedef SrtPacket PrivPacket;
174 std::vector<PrivPacket>& rebuilt;
175 } rcv;
176
177 void ConfigureGroup(Group& g, int32_t seqno, size_t gstep, size_t drop);
178 template <class Container>
179 void ConfigureColumns(Container& which, int32_t isn);
180
181 void ResetGroup(Group& g);
182
183 // Universal
184 void ClipData(Group& g, uint16_t length_net, uint8_t kflg,
185 uint32_t timestamp_hw, const char* payload, size_t payload_size);
186 void ClipPacket(Group& g, const CPacket& pkt);
187
188 // Sending
189 bool CheckGroupClose(Group& g, size_t pos, size_t size);
190 void PackControl(const Group& g, signed char groupix, SrtPacket& pkt, int32_t seqno);
191
192 // Receiving
193 void CheckLargeDrop(int32_t seqno);
194 size_t ExtendRows(size_t rowx);
195 size_t ExtendColumns(size_t colgx);
196
197 enum ECellReceived
198 {
199 CELL_RECEIVED, //< mark cell for a received packet (no matter current value)
200 CELL_EXTEND, //< just make sure there's a place for a packet, set false if not
201 CELL_REMOVE //< even if a packet was marked true, remove the cell existence confirmation
202 };
203 void MarkCellReceived(int32_t seq, ECellReceived recv = CELL_RECEIVED);
204
205 enum EHangStatus
206 {
207 HANG_NOTDONE,
208 HANG_SUCCESS,
209 HANG_PAST,
210 HANG_CRAZY
211 };
212
213 friend bool operator <(FECFilterBuiltin::EHangStatus a, FECFilterBuiltin::EHangStatus b)
214 {
215 return int(a) < int(b);
216 }
217
218 EHangStatus HangHorizontal(const CPacket& pkt, bool fec_ctl, loss_seqs_t& irrecover);
219 EHangStatus HangVertical(const CPacket& pkt, signed char fec_colx, loss_seqs_t& irrecover);
220 void ClipControlPacket(Group& g, const CPacket& pkt);
221 void ClipRebuiltPacket(Group& g, Receive::PrivPacket& pkt);
222 void RcvRebuild(Group& g, int32_t seqno, Group::Type tp);
223 int32_t RcvGetLossSeqHoriz(Group& g);
224 int32_t RcvGetLossSeqVert(Group& g);
225 bool CheckEmergencyShrink(size_t n_series, size_t size_in_packets);
226
227 static void TranslateLossRecords(const std::set<int32_t>& loss, loss_seqs_t& irrecover);
228 void RcvCheckDismissColumn(int32_t seqno, int colgx, loss_seqs_t& irrecover);
229 int RcvGetRowGroupIndex(int32_t seq, EHangStatus& w_status);
230 int RcvGetColumnGroupIndex(int32_t seqno, EHangStatus& w_status);
231 void CollectIrrecoverRow(RcvGroup& g, loss_seqs_t& irrecover) const;
232 bool IsLost(int32_t seq) const;
233
234 public:
235
236 FECFilterBuiltin(const SrtFilterInitializer& init, std::vector<SrtPacket>& provided, const std::string& confstr);
237
238 // Sender side
239
240 // This function creates and stores the FEC control packet with
241 // a prediction to be immediately sent. This is called in the function
242 // that normally is prepared for extracting a data packet from the sender
243 // buffer and send it over the channel.
244 virtual bool packControlPacket(SrtPacket& r_packet, int32_t seq) ATR_OVERRIDE;
245
246 // This is called at the moment when the sender queue decided to pick up
247 // a new packet from the scheduled packets. This should be then used to
248 // continue filling the group, possibly followed by final calculating the
249 // FEC control packet ready to send.
250 virtual void feedSource(CPacket& r_packet) ATR_OVERRIDE;
251
252 // Receiver side
253
254 // This function is called at the moment when a new data packet has
255 // arrived (no matter if subsequent or recovered). The 'state' value
256 // defines the configured level of loss state required to send the
257 // loss report.
258 virtual bool receive(const CPacket& pkt, loss_seqs_t& loss_seqs) ATR_OVERRIDE;
259
260 // Configuration
261
262 // This is the size that is needed extra by packets operated by this corrector.
263 // It should be subtracted from a current maximum value for SRTO_PAYLOADSIZE
264
265 // The default FEC uses extra space only for FEC/CTL packet.
266 // The timestamp clip is placed in the timestamp field in the header.
267 // The payload contains:
268 // - the length clip
269 // - the flag spec
270 // - the payload clip
271 // The payload clip takes simply the current length of SRTO_PAYLOADSIZE.
272 // So extra 4 bytes are needed, 2 for flags, 2 for length clip.
273 static const size_t EXTRA_SIZE = 4;
274
275 virtual SRT_ARQLevel arqLevel() ATR_OVERRIDE { return m_fallback_level; }
276
277 static const char defaultConfig [];
278 static bool verifyConfig(const SrtFilterConfig& config, std::string& w_errormsg);
279 };
280
281 } // namespace srt
282
283 #endif