mediasoup 3.19.21 → 3.19.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/node/lib/Worker.d.ts +1 -0
  2. package/node/lib/Worker.d.ts.map +1 -1
  3. package/node/lib/Worker.js +14 -0
  4. package/package.json +4 -2
  5. package/worker/fuzzer/src/RTC/FuzzerDtlsTransport.cpp +9 -3
  6. package/worker/fuzzer/src/RTC/RTP/FuzzerRtpStreamSend.cpp +9 -1
  7. package/worker/include/Channel/ChannelMessageRegistrator.hpp +39 -0
  8. package/worker/include/Channel/ChannelMessageRegistratorInterface.hpp +32 -0
  9. package/worker/include/Channel/ChannelSocket.hpp +1 -1
  10. package/worker/include/DepUsrSCTP.hpp +8 -7
  11. package/worker/include/RTC/ActiveSpeakerObserver.hpp +7 -7
  12. package/worker/include/RTC/AudioLevelObserver.hpp +7 -7
  13. package/worker/include/RTC/Consumer.hpp +3 -3
  14. package/worker/include/RTC/DataConsumer.hpp +3 -3
  15. package/worker/include/RTC/DataProducer.hpp +3 -3
  16. package/worker/include/RTC/DirectTransport.hpp +2 -2
  17. package/worker/include/RTC/DtlsTransport.hpp +8 -6
  18. package/worker/include/RTC/ICE/IceServer.hpp +8 -5
  19. package/worker/include/RTC/KeyFrameRequestManager.hpp +15 -12
  20. package/worker/include/RTC/NackGenerator.hpp +7 -6
  21. package/worker/include/RTC/PipeConsumer.hpp +1 -2
  22. package/worker/include/RTC/PipeTransport.hpp +2 -2
  23. package/worker/include/RTC/PlainTransport.hpp +2 -2
  24. package/worker/include/RTC/Producer.hpp +3 -3
  25. package/worker/include/RTC/RTP/RtpStream.hpp +7 -1
  26. package/worker/include/RTC/RTP/RtpStreamRecv.hpp +6 -5
  27. package/worker/include/RTC/RTP/RtpStreamSend.hpp +4 -1
  28. package/worker/include/RTC/Router.hpp +3 -3
  29. package/worker/include/RTC/RtpObserver.hpp +3 -3
  30. package/worker/include/RTC/SCTP/TODO_SCTP.md +18 -6
  31. package/worker/include/RTC/SCTP/association/Association.hpp +11 -8
  32. package/worker/include/RTC/SCTP/association/HeartbeatHandler.hpp +9 -6
  33. package/worker/include/RTC/SCTP/association/StreamResetHandler.hpp +37 -23
  34. package/worker/include/RTC/SCTP/association/TCBContext.hpp +3 -2
  35. package/worker/include/RTC/SCTP/association/TransmissionControlBlock.hpp +81 -8
  36. package/worker/include/RTC/SCTP/packet/UserData.hpp +36 -0
  37. package/worker/include/RTC/SCTP/packet/chunks/ForwardTsnChunk.hpp +1 -1
  38. package/worker/include/RTC/SCTP/packet/chunks/IForwardTsnChunk.hpp +1 -1
  39. package/worker/include/RTC/SCTP/public/SctpOptions.hpp +2 -1
  40. package/worker/include/RTC/SCTP/tx/OutstandingData.hpp +604 -0
  41. package/worker/include/RTC/SCTP/tx/RetransmissionQueue.hpp +336 -0
  42. package/worker/include/RTC/SCTP/tx/RetransmissionTimeout.hpp +5 -4
  43. package/worker/include/RTC/Serializable.hpp +8 -0
  44. package/worker/include/RTC/SimpleConsumer.hpp +1 -2
  45. package/worker/include/RTC/SimulcastConsumer.hpp +1 -2
  46. package/worker/include/RTC/SvcConsumer.hpp +1 -2
  47. package/worker/include/RTC/Transport.hpp +8 -8
  48. package/worker/include/RTC/TransportCongestionControlClient.hpp +8 -5
  49. package/worker/include/RTC/TransportCongestionControlServer.hpp +8 -5
  50. package/worker/include/RTC/WebRtcServer.hpp +3 -3
  51. package/worker/include/RTC/WebRtcTransport.hpp +3 -3
  52. package/worker/include/Shared.hpp +40 -0
  53. package/worker/include/SharedInterface.hpp +44 -0
  54. package/worker/include/Utils.hpp +6 -0
  55. package/worker/include/Worker.hpp +3 -3
  56. package/worker/include/common.hpp +1 -1
  57. package/worker/include/handles/BackoffTimerHandle.hpp +27 -65
  58. package/worker/include/handles/BackoffTimerHandleInterface.hpp +116 -0
  59. package/worker/include/handles/TimerHandle.hpp +36 -20
  60. package/worker/include/handles/TimerHandleInterface.hpp +43 -0
  61. package/worker/meson.build +21 -4
  62. package/worker/meson_options.txt +2 -1
  63. package/worker/mocks/include/Channel/MockChannelMessageRegistrator.hpp +45 -0
  64. package/worker/mocks/include/MockShared.hpp +43 -0
  65. package/worker/mocks/src/Channel/MockChannelMessageRegistrator.cpp +128 -0
  66. package/worker/mocks/src/MockShared.cpp +26 -0
  67. package/worker/scripts/clang-scripts.mjs +4 -1
  68. package/worker/src/Channel/ChannelMessageRegistrator.cpp +125 -0
  69. package/worker/src/Channel/ChannelSocket.cpp +1 -1
  70. package/worker/src/DepUsrSCTP.cpp +10 -4
  71. package/worker/src/RTC/ActiveSpeakerObserver.cpp +7 -7
  72. package/worker/src/RTC/AudioLevelObserver.cpp +12 -10
  73. package/worker/src/RTC/Consumer.cpp +23 -20
  74. package/worker/src/RTC/DataConsumer.cpp +11 -11
  75. package/worker/src/RTC/DataProducer.cpp +3 -3
  76. package/worker/src/RTC/DirectTransport.cpp +16 -16
  77. package/worker/src/RTC/DtlsTransport.cpp +4 -4
  78. package/worker/src/RTC/ICE/IceServer.cpp +4 -3
  79. package/worker/src/RTC/KeyFrameRequestManager.cpp +15 -15
  80. package/worker/src/RTC/NackGenerator.cpp +3 -3
  81. package/worker/src/RTC/PipeConsumer.cpp +5 -4
  82. package/worker/src/RTC/PipeTransport.cpp +3 -3
  83. package/worker/src/RTC/PlainTransport.cpp +10 -9
  84. package/worker/src/RTC/Producer.cpp +30 -28
  85. package/worker/src/RTC/RTCP/FeedbackPsRpsi.cpp +1 -2
  86. package/worker/src/RTC/RTP/RtpStream.cpp +9 -2
  87. package/worker/src/RTC/RTP/RtpStreamRecv.cpp +5 -4
  88. package/worker/src/RTC/RTP/RtpStreamSend.cpp +5 -2
  89. package/worker/src/RTC/Router.cpp +3 -3
  90. package/worker/src/RTC/RtpObserver.cpp +2 -1
  91. package/worker/src/RTC/SCTP/association/Association.cpp +94 -114
  92. package/worker/src/RTC/SCTP/association/HeartbeatHandler.cpp +27 -21
  93. package/worker/src/RTC/SCTP/association/StreamResetHandler.cpp +52 -55
  94. package/worker/src/RTC/SCTP/association/TransmissionControlBlock.cpp +144 -25
  95. package/worker/src/RTC/SCTP/packet/chunks/ForwardTsnChunk.cpp +2 -2
  96. package/worker/src/RTC/SCTP/packet/chunks/IForwardTsnChunk.cpp +2 -2
  97. package/worker/src/RTC/SCTP/tx/OutstandingData.cpp +905 -0
  98. package/worker/src/RTC/SCTP/tx/RetransmissionQueue.cpp +799 -0
  99. package/worker/src/RTC/SCTP/tx/RetransmissionTimeout.cpp +1 -1
  100. package/worker/src/RTC/SctpAssociation.cpp +1 -1
  101. package/worker/src/RTC/SimpleConsumer.cpp +8 -7
  102. package/worker/src/RTC/SimulcastConsumer.cpp +11 -10
  103. package/worker/src/RTC/SvcConsumer.cpp +11 -10
  104. package/worker/src/RTC/Transport.cpp +36 -26
  105. package/worker/src/RTC/TransportCongestionControlClient.cpp +4 -2
  106. package/worker/src/RTC/TransportCongestionControlServer.cpp +4 -3
  107. package/worker/src/RTC/WebRtcServer.cpp +5 -4
  108. package/worker/src/RTC/WebRtcTransport.cpp +39 -26
  109. package/worker/src/Shared.cpp +35 -0
  110. package/worker/src/Worker.cpp +10 -23
  111. package/worker/src/handles/BackoffTimerHandle.cpp +11 -16
  112. package/worker/src/handles/TimerHandle.cpp +5 -4
  113. package/worker/src/lib.cpp +14 -1
  114. package/worker/tasks.py +1 -1
  115. package/worker/test/include/RTC/ICE/iceCommon.hpp +1 -0
  116. package/worker/test/include/RTC/RTP/rtpCommon.hpp +1 -0
  117. package/worker/test/include/RTC/SCTP/sctpCommon.hpp +6 -0
  118. package/worker/test/src/RTC/RTP/TestRtpStreamRecv.cpp +12 -5
  119. package/worker/test/src/RTC/RTP/TestRtpStreamSend.cpp +34 -23
  120. package/worker/test/src/RTC/SCTP/tx/TestOutstandingData.cpp +1196 -0
  121. package/worker/test/src/RTC/SCTP/tx/TestRetransmissionTimeout.cpp +33 -33
  122. package/worker/test/src/RTC/TestKeyFrameRequestManager.cpp +14 -6
  123. package/worker/test/src/RTC/TestNackGenerator.cpp +6 -2
  124. package/worker/test/src/RTC/TestSimpleConsumer.cpp +6 -10
  125. package/worker/test/src/RTC/TestTransportCongestionControlServer.cpp +9 -2
  126. package/worker/test/src/Utils/TestByte.cpp +98 -0
  127. package/worker/include/ChannelMessageRegistrator.hpp +0 -30
  128. package/worker/include/RTC/Shared.hpp +0 -23
  129. package/worker/src/ChannelMessageRegistrator.cpp +0 -119
  130. package/worker/src/RTC/Shared.cpp +0 -23
@@ -0,0 +1,905 @@
1
+ #define MS_CLASS "RTC::SCTP::OutstandingData"
2
+ // #define MS_LOG_DEV_LEVEL 3
3
+
4
+ #include "RTC/SCTP/tx/OutstandingData.hpp"
5
+ #include "Logger.hpp"
6
+ #include "MediaSoupErrors.hpp"
7
+ #include "Utils.hpp"
8
+ #include "RTC/SCTP/packet/chunks/ForwardTsnChunk.hpp"
9
+ #include "RTC/SCTP/packet/chunks/IForwardTsnChunk.hpp"
10
+ #include <algorithm>
11
+ #include <map>
12
+
13
+ namespace RTC
14
+ {
15
+ namespace SCTP
16
+ {
17
+ /* Static. */
18
+
19
+ // The number of times a packet must be NACKed before it's retransmitted.
20
+ //
21
+ // @see https://datatracker.ietf.org/doc/html/rfc9260#section-7.2.4
22
+ constexpr uint8_t NumberOfNacksForRetransmission{ 3 };
23
+
24
+ /* Instance methods. */
25
+
26
+ OutstandingData::Item::Item(
27
+ uint32_t messageId,
28
+ UserData data,
29
+ uint64_t timeSentMs,
30
+ uint16_t maxRetransmissions,
31
+ uint64_t expiresAtMs,
32
+ uint64_t lifecycleId)
33
+ : messageId(messageId),
34
+ timeSentMs(timeSentMs),
35
+ maxRetransmissions(maxRetransmissions),
36
+ expiresAtMs(expiresAtMs),
37
+ lifecycleId(lifecycleId),
38
+ data(std::move(data))
39
+ {
40
+ MS_TRACE();
41
+ }
42
+
43
+ void OutstandingData::Item::Ack()
44
+ {
45
+ MS_TRACE();
46
+
47
+ if (this->lifecycle != Lifecycle::ABANDONED)
48
+ {
49
+ this->lifecycle = Lifecycle::ACTIVE;
50
+ }
51
+
52
+ this->ackState = AckState::ACKED;
53
+ }
54
+
55
+ OutstandingData::Item::NackAction OutstandingData::Item::Nack(bool retransmitNow)
56
+ {
57
+ MS_TRACE();
58
+
59
+ this->ackState = AckState::NACKED;
60
+ ++this->nackCount;
61
+
62
+ if (!ShouldBeRetransmitted() && !IsAbandoned() && (retransmitNow || this->nackCount >= NumberOfNacksForRetransmission))
63
+ {
64
+ // Nacked enough times, it's considered lost.
65
+ if (this->numRetransmissions < this->maxRetransmissions)
66
+ {
67
+ this->lifecycle = Lifecycle::TO_BE_RETRANSMITTED;
68
+
69
+ return NackAction::RETRANSMIT;
70
+ }
71
+
72
+ Abandon();
73
+
74
+ return NackAction::ABANDON;
75
+ }
76
+
77
+ return NackAction::NOTHING;
78
+ }
79
+
80
+ void OutstandingData::Item::MarkAsRetransmitted()
81
+ {
82
+ MS_TRACE();
83
+
84
+ this->lifecycle = Lifecycle::ACTIVE;
85
+ this->ackState = AckState::UNACKED;
86
+ this->nackCount = 0;
87
+ ++this->numRetransmissions;
88
+ }
89
+
90
+ void OutstandingData::Item::Abandon()
91
+ {
92
+ MS_TRACE();
93
+
94
+ MS_ASSERT(
95
+ this->expiresAtMs != OutstandingData::ExpiresAtMsInfinite ||
96
+ this->maxRetransmissions != OutstandingData::MaxRetransmitsNoLimit,
97
+ "item should not have infinite expiration time or its retransmission times shouldn't be the maximum");
98
+
99
+ this->lifecycle = Lifecycle::ABANDONED;
100
+ }
101
+
102
+ OutstandingData::OutstandingData(
103
+ size_t dataChunkHeaderLength,
104
+ UnwrappedTsn lastCumulativeTsnAck,
105
+ std::function<bool(uint16_t /*streamId*/, uint32_t /*outgoingMessageId*/)> discardFromSendQueue)
106
+ : dataChunkHeaderLength(dataChunkHeaderLength),
107
+ lastCumulativeTsnAck(lastCumulativeTsnAck),
108
+ discardFromSendQueue(std::move(discardFromSendQueue))
109
+ {
110
+ MS_TRACE();
111
+ }
112
+
113
+ OutstandingData::AckInfo OutstandingData::HandleSack(
114
+ UnwrappedTsn cumulativeTsnAck,
115
+ std::span<const SackChunk::GapAckBlock> gapAckBlocks,
116
+ bool isInFastRecovery)
117
+ {
118
+ MS_TRACE();
119
+
120
+ const bool cumulativeTsnAckAdvanced = cumulativeTsnAck > this->lastCumulativeTsnAck;
121
+
122
+ OutstandingData::AckInfo ackInfo(cumulativeTsnAck);
123
+
124
+ // Erase all items up to cumulativeTsnAck.
125
+ RemoveAcked(cumulativeTsnAck, ackInfo);
126
+
127
+ // ACK packets reported in the gap ack blocks.
128
+ AckGapBlocks(cumulativeTsnAck, gapAckBlocks, ackInfo);
129
+
130
+ // NACK and possibly mark for retransmit Chunks that weren't acked.
131
+ NackBetweenAckBlocks(
132
+ cumulativeTsnAck, gapAckBlocks, isInFastRecovery, cumulativeTsnAckAdvanced, ackInfo);
133
+
134
+ AssertIsConsistent();
135
+
136
+ return ackInfo;
137
+ }
138
+
139
+ std::vector<std::pair<uint32_t /*tsn*/, UserData>> OutstandingData::GetChunksToBeFastRetransmitted(
140
+ size_t maxLength)
141
+ {
142
+ MS_TRACE();
143
+
144
+ std::vector<std::pair<uint32_t /*tsn*/, UserData>> result =
145
+ ExtractChunksThatCanFit(this->toBeFastRetransmitted, maxLength);
146
+
147
+ // https://datatracker.ietf.org/doc/html/rfc9260#section-7.2.4
148
+ //
149
+ // "Those TSNs marked for retransmission due to the Fast-Retransmit
150
+ // algorithm that did not fit in the sent datagram carrying K other TSNs
151
+ // are also marked as ineligible for a subsequent Fast Retransmit.
152
+ // However, as they are marked for retransmission they will be
153
+ // retransmitted later on as soon as cwnd allows."
154
+ if (!this->toBeFastRetransmitted.empty())
155
+ {
156
+ this->toBeRetransmitted.insert(
157
+ this->toBeFastRetransmitted.begin(), this->toBeFastRetransmitted.end());
158
+
159
+ this->toBeFastRetransmitted.clear();
160
+ }
161
+
162
+ AssertIsConsistent();
163
+
164
+ return result;
165
+ }
166
+
167
+ std::vector<std::pair<uint32_t /*tsn*/, UserData>> OutstandingData::GetChunksToBeRetransmitted(
168
+ size_t maxLength)
169
+ {
170
+ MS_TRACE();
171
+
172
+ // Chunks scheduled for fast retransmission must be sent first.
173
+ MS_ASSERT(this->toBeFastRetransmitted.empty(), "this->toBeFastRetransmitted is not empty");
174
+
175
+ return ExtractChunksThatCanFit(this->toBeFastRetransmitted, maxLength);
176
+ }
177
+
178
+ void OutstandingData::ExpireOutstandingChunks(uint64_t nowMs)
179
+ {
180
+ MS_TRACE();
181
+
182
+ std::vector<UnwrappedTsn> tsnsToExpire;
183
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
184
+
185
+ for (const Item& item : this->outstandingData)
186
+ {
187
+ tsn.Increment();
188
+
189
+ // Chunks that are nacked can be expired. Care should be taken not to
190
+ // expire unacked (in-flight) Chunks as they might have been received,
191
+ // but the SACK is either delayed or in-flight and may be received
192
+ // later.
193
+ if (item.IsAbandoned())
194
+ {
195
+ // Already abandoned.
196
+ }
197
+ else if (item.IsNacked() && item.HasExpired(nowMs))
198
+ {
199
+ tsnsToExpire.push_back(tsn);
200
+ }
201
+ else
202
+ {
203
+ // A non-expired Chunk. No need to iterate any further.
204
+ break;
205
+ }
206
+ }
207
+
208
+ for (const UnwrappedTsn tsnToExpire : tsnsToExpire)
209
+ {
210
+ // The item is retrieved by TSN, as AbandonAllFor() may have modified
211
+ // `this->outstandingData` and invalidated iterators from the first
212
+ // loop.
213
+ const Item& item = GetItem(tsnToExpire);
214
+
215
+ MS_WARN_TAG(
216
+ sctp,
217
+ "marking nacked Chunk %" PRIu32 " and message %" PRIu32 " as expired",
218
+ tsnToExpire.Wrap(),
219
+ item.GetData().GetMessageId());
220
+
221
+ AbandonAllFor(item);
222
+ }
223
+
224
+ AssertIsConsistent();
225
+ }
226
+
227
+ OutstandingData::UnwrappedTsn OutstandingData::GetHighestOutstandingTsn() const
228
+ {
229
+ MS_TRACE();
230
+
231
+ return UnwrappedTsn::AddTo(this->lastCumulativeTsnAck, this->outstandingData.size());
232
+ }
233
+
234
+ std::optional<OutstandingData::UnwrappedTsn> OutstandingData::Insert(
235
+ uint32_t messageId,
236
+ const UserData& data,
237
+ uint64_t timeSentMs,
238
+ uint16_t maxRetransmissions,
239
+ uint64_t expiresAtMs,
240
+ uint64_t lifecycleId)
241
+ {
242
+ MS_TRACE();
243
+
244
+ // All Chunks are always padded to be even divisible by 4.
245
+ const size_t chunkLength = GetSerializedChunkLength(data);
246
+
247
+ this->unackedPayloadBytes += data.GetPayloadLength();
248
+ this->unackedPacketBytes += chunkLength;
249
+ ++this->unackedItems;
250
+
251
+ const UnwrappedTsn tsn = GetNextTsn();
252
+ const Item& item = this->outstandingData.emplace_back(
253
+ messageId, data.Clone(), timeSentMs, maxRetransmissions, expiresAtMs, lifecycleId);
254
+
255
+ if (item.HasExpired(timeSentMs))
256
+ {
257
+ // No need to send it, it was expired when it was in the send queue.
258
+ MS_WARN_TAG(
259
+ sctp,
260
+ "marking freshly produced Chunk %" PRIu32 " and message %" PRIu32 " as expired",
261
+ tsn.Wrap(),
262
+ item.GetData().GetMessageId());
263
+
264
+ AbandonAllFor(item);
265
+
266
+ AssertIsConsistent();
267
+
268
+ return std::nullopt;
269
+ }
270
+
271
+ AssertIsConsistent();
272
+
273
+ return tsn;
274
+ }
275
+
276
+ void OutstandingData::NackAll()
277
+ {
278
+ MS_TRACE();
279
+
280
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
281
+
282
+ // A two-pass algorithm is needed, as NackItem will invalidate iterators.
283
+ std::vector<UnwrappedTsn> tsnsToNack;
284
+
285
+ for (const Item& item : this->outstandingData)
286
+ {
287
+ tsn.Increment();
288
+
289
+ if (!item.IsAcked())
290
+ {
291
+ tsnsToNack.push_back(tsn);
292
+ }
293
+ }
294
+
295
+ for (const UnwrappedTsn tsnToNack : tsnsToNack)
296
+ {
297
+ NackItem(
298
+ tsnToNack,
299
+ /*retransmitNow*/ true,
300
+ /*doFastRetransmit*/ false);
301
+ }
302
+
303
+ AssertIsConsistent();
304
+ }
305
+
306
+ void OutstandingData::CreateForwardTsn(Packet* packet) const
307
+ {
308
+ MS_TRACE();
309
+
310
+ std::map<uint16_t /*streamId*/, uint16_t /*ssn*/> skippedPerOrderedStream;
311
+ UnwrappedTsn newCumulativeAck = this->lastCumulativeTsnAck;
312
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
313
+
314
+ for (const Item& item : this->outstandingData)
315
+ {
316
+ tsn.Increment();
317
+
318
+ if (
319
+ this->streamResetBreakpointTsns.contains(tsn) ||
320
+ (tsn != newCumulativeAck.GetNextValue()) || !item.IsAbandoned())
321
+ {
322
+ break;
323
+ }
324
+
325
+ newCumulativeAck = tsn;
326
+
327
+ if (
328
+ !item.GetData().IsUnordered() && item.GetData().GetStreamSequenceNumber() >
329
+ skippedPerOrderedStream[item.GetData().GetStreamId()])
330
+ {
331
+ skippedPerOrderedStream[item.GetData().GetStreamId()] =
332
+ item.GetData().GetStreamSequenceNumber();
333
+ }
334
+ }
335
+
336
+ auto* forwardTsnChunk = packet->BuildChunkInPlace<ForwardTsnChunk>();
337
+
338
+ forwardTsnChunk->SetNewCumulativeTsn(newCumulativeAck.Wrap());
339
+
340
+ for (const auto& [streamId, ssn] : skippedPerOrderedStream)
341
+ {
342
+ forwardTsnChunk->AddStream(streamId, ssn);
343
+ }
344
+
345
+ forwardTsnChunk->Consolidate();
346
+ }
347
+
348
+ void OutstandingData::CreateIForwardTsn(Packet* packet) const
349
+ {
350
+ MS_TRACE();
351
+
352
+ std::map<std::pair<uint16_t /*streamId*/, bool /*isUnordered*/>, uint32_t /*mid*/> skippedPerStream;
353
+ UnwrappedTsn newCumulativeAck = this->lastCumulativeTsnAck;
354
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
355
+
356
+ for (const Item& item : this->outstandingData)
357
+ {
358
+ tsn.Increment();
359
+
360
+ if (
361
+ this->streamResetBreakpointTsns.contains(tsn) ||
362
+ (tsn != newCumulativeAck.GetNextValue()) || !item.IsAbandoned())
363
+ {
364
+ break;
365
+ }
366
+
367
+ newCumulativeAck = tsn;
368
+
369
+ const std::pair<uint16_t /*streamId*/, bool /*isUnordered*/> stream =
370
+ std::make_pair(item.GetData().GetStreamId(), item.GetData().IsUnordered());
371
+
372
+ skippedPerStream[stream] = std::max(item.GetData().GetMessageId(), skippedPerStream[stream]);
373
+ }
374
+
375
+ auto* iForwardTsnChunk = packet->BuildChunkInPlace<IForwardTsnChunk>();
376
+
377
+ iForwardTsnChunk->SetNewCumulativeTsn(newCumulativeAck.Wrap());
378
+
379
+ for (const auto& [stream, mid] : skippedPerStream)
380
+ {
381
+ iForwardTsnChunk->AddStream(stream.first, stream.second, mid);
382
+ }
383
+
384
+ iForwardTsnChunk->Consolidate();
385
+ }
386
+
387
+ std::optional<uint64_t> OutstandingData::MeasureRtt(uint64_t nowMs, UnwrappedTsn tsn) const
388
+ {
389
+ MS_TRACE();
390
+
391
+ if (tsn > this->lastCumulativeTsnAck && tsn < GetNextTsn())
392
+ {
393
+ const Item& item = GetItem(tsn);
394
+
395
+ if (!item.HasBeenRetransmitted())
396
+ {
397
+ // https://datatracker.ietf.org/doc/html/rfc9260#section-6.3.1
398
+ //
399
+ // "Karn's algorithm: RTT measurements MUST NOT be made using packets
400
+ // that were retransmitted (and thus for which it is ambiguous
401
+ // whether the reply was for the first instance of the Chunk or for a
402
+ // later instance)"
403
+ return nowMs - item.GetTimeSentMs();
404
+ }
405
+ }
406
+
407
+ return std::nullopt;
408
+ }
409
+
410
+ bool OutstandingData::ShouldSendForwardTsn() const
411
+ {
412
+ MS_TRACE();
413
+
414
+ if (!this->outstandingData.empty())
415
+ {
416
+ return this->outstandingData.front().IsAbandoned();
417
+ }
418
+ else
419
+ {
420
+ return false;
421
+ }
422
+ }
423
+
424
+ void OutstandingData::BeginResetStreams()
425
+ {
426
+ MS_TRACE();
427
+
428
+ this->streamResetBreakpointTsns.insert(GetNextTsn());
429
+ }
430
+
431
+ #ifdef MS_TEST
432
+ std::vector<
433
+ std::pair<uint32_t /*tsn*/, OutstandingData::State>> OutstandingData::GetChunkStatesForTesting() const
434
+ {
435
+ MS_TRACE();
436
+
437
+ std::vector<std::pair<uint32_t /*tsn*/, State>> states;
438
+
439
+ states.emplace_back(this->lastCumulativeTsnAck.Wrap(), State::ACKED);
440
+
441
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
442
+
443
+ for (const Item& item : this->outstandingData)
444
+ {
445
+ tsn.Increment();
446
+
447
+ State state;
448
+
449
+ if (item.IsAbandoned())
450
+ {
451
+ state = State::ABANDONED;
452
+ }
453
+ else if (item.ShouldBeRetransmitted())
454
+ {
455
+ state = State::TO_BE_RETRANSMITTED;
456
+ }
457
+ else if (item.IsAcked())
458
+ {
459
+ state = State::ACKED;
460
+ }
461
+ else if (item.IsNacked())
462
+ {
463
+ state = State::NACKED;
464
+ }
465
+ else if (item.IsOutstanding())
466
+ {
467
+ state = State::IN_FLIGHT;
468
+ }
469
+ else
470
+ {
471
+ MS_THROW_ERROR("should not end here");
472
+ }
473
+
474
+ states.emplace_back(tsn.Wrap(), state);
475
+ }
476
+
477
+ return states;
478
+ }
479
+ #endif
480
+
481
+ size_t OutstandingData::GetSerializedChunkLength(const UserData& data) const
482
+ {
483
+ MS_TRACE();
484
+
485
+ return Utils::Byte::PadTo4Bytes<size_t>(this->dataChunkHeaderLength + data.GetPayloadLength());
486
+ }
487
+
488
+ OutstandingData::Item& OutstandingData::GetItem(UnwrappedTsn tsn)
489
+ {
490
+ MS_TRACE();
491
+
492
+ MS_ASSERT(
493
+ tsn > this->lastCumulativeTsnAck, "tsn must be higher than this->lastCumulativeTsnAck");
494
+ MS_ASSERT(tsn < GetNextTsn(), "tsn must be higher than GetNextTsn()");
495
+
496
+ const size_t index = UnwrappedTsn::Difference(tsn, this->lastCumulativeTsnAck) - 1;
497
+
498
+ MS_ASSERT(index >= 0, "index must be equal or higher than 0");
499
+ MS_ASSERT(
500
+ index < this->outstandingData.size(),
501
+ "index must be lower than this->outstandingData.size()");
502
+
503
+ return this->outstandingData[index];
504
+ }
505
+
506
+ const OutstandingData::Item& OutstandingData::GetItem(UnwrappedTsn tsn) const
507
+ {
508
+ MS_TRACE();
509
+
510
+ MS_ASSERT(
511
+ tsn > this->lastCumulativeTsnAck, "tsn must be higher than this->lastCumulativeTsnAck");
512
+ MS_ASSERT(tsn < GetNextTsn(), "tsn must be higher than GetNextTsn()");
513
+
514
+ const size_t index = UnwrappedTsn::Difference(tsn, this->lastCumulativeTsnAck) - 1;
515
+
516
+ MS_ASSERT(index >= 0, "index must be equal or higher than 0");
517
+ MS_ASSERT(
518
+ index < this->outstandingData.size(),
519
+ "index must be lower than this->outstandingData.size()");
520
+
521
+ return this->outstandingData[index];
522
+ }
523
+
524
+ void OutstandingData::RemoveAcked(UnwrappedTsn cumulativeTsnAck, AckInfo& ackInfo)
525
+ {
526
+ MS_TRACE();
527
+
528
+ while (!this->outstandingData.empty() && this->lastCumulativeTsnAck < cumulativeTsnAck)
529
+ {
530
+ const UnwrappedTsn tsn = this->lastCumulativeTsnAck.GetNextValue();
531
+
532
+ Item& item = this->outstandingData.front();
533
+
534
+ AckChunk(ackInfo, tsn, item);
535
+
536
+ if (item.GetLifecycleId() != 0)
537
+ {
538
+ MS_ASSERT(item.GetData().IsEnd(), "item.GetData().IsEnd() must be true");
539
+
540
+ if (item.IsAbandoned())
541
+ {
542
+ ackInfo.abandonedLifecycleIds.push_back(item.GetLifecycleId());
543
+ }
544
+ else
545
+ {
546
+ ackInfo.ackedLifecycleIds.push_back(item.GetLifecycleId());
547
+ }
548
+ }
549
+
550
+ this->outstandingData.pop_front();
551
+ this->lastCumulativeTsnAck.Increment();
552
+ }
553
+
554
+ this->streamResetBreakpointTsns.erase(
555
+ this->streamResetBreakpointTsns.begin(),
556
+ this->streamResetBreakpointTsns.upper_bound(cumulativeTsnAck.GetNextValue()));
557
+ }
558
+
559
+ void OutstandingData::AckGapBlocks(
560
+ UnwrappedTsn cumulativeTsnAck,
561
+ std::span<const SackChunk::GapAckBlock> gapAckBlocks,
562
+ AckInfo& ackInfo)
563
+ {
564
+ MS_TRACE();
565
+
566
+ // Mark all non-gaps as ACKED (but they can't be removed) as (from RFC)
567
+ // "SCTP considers the information carried in the Gap Ack Blocks in the
568
+ // SACK Chunk as advisory". Note that when NR-SACK is supported, this can
569
+ // be handled differently.
570
+
571
+ for (const auto& block : gapAckBlocks)
572
+ {
573
+ const UnwrappedTsn start = UnwrappedTsn::AddTo(cumulativeTsnAck, block.start);
574
+ const UnwrappedTsn end = UnwrappedTsn::AddTo(cumulativeTsnAck, block.end);
575
+
576
+ for (UnwrappedTsn tsn = start; tsn <= end; tsn = tsn.GetNextValue())
577
+ {
578
+ if (tsn > this->lastCumulativeTsnAck && tsn < GetNextTsn())
579
+ {
580
+ Item& item = GetItem(tsn);
581
+
582
+ AckChunk(ackInfo, tsn, item);
583
+ }
584
+ }
585
+ }
586
+ }
587
+
588
+ void OutstandingData::NackBetweenAckBlocks(
589
+ UnwrappedTsn cumulativeTsnAck,
590
+ std::span<const SackChunk::GapAckBlock> gapAckBlocks,
591
+ bool isInFastRecovery,
592
+ bool cumulativeTsnAckedAdvanced,
593
+ AckInfo& ackInfo)
594
+ {
595
+ MS_TRACE();
596
+
597
+ // Mark everything between the blocks as NACKED/TO_BE_RETRANSMITTED.
598
+ //
599
+ // https://datatracker.ietf.org/doc/html/rfc9260#section-7.2.4
600
+ //
601
+ // "Mark the DATA chunk(s) with three miss indications for retransmission."
602
+ // "For each incoming SACK, miss indications are incremented only for
603
+ // missing TSNs prior to the highest TSN newly acknowledged in the SACK."
604
+ //
605
+ // What this means is that only when there is a increasing stream of data
606
+ // received and there are new packets seen (since last time), packets that
607
+ // are in-flight and between gaps should be nacked. This means that SCTP
608
+ // relies on the T3-RTX-timer to re-send packets otherwise.
609
+ UnwrappedTsn maxTsnToNack = ackInfo.highestTsnAcked;
610
+
611
+ if (isInFastRecovery && cumulativeTsnAckedAdvanced)
612
+ {
613
+ // https://datatracker.ietf.org/doc/html/rfc9260#section-7.2.4
614
+ //
615
+ // "If an endpoint is in Fast Recovery and a SACK arrives that advances
616
+ // the Cumulative TSN Ack Point, the miss indications are incremented
617
+ // for all TSNs reported missing in the SACK."
618
+ maxTsnToNack = UnwrappedTsn::AddTo(
619
+ cumulativeTsnAck, gapAckBlocks.empty() ? 0 : gapAckBlocks.rbegin()->end);
620
+ }
621
+
622
+ UnwrappedTsn prevBlockLastAcked = cumulativeTsnAck;
623
+
624
+ for (const auto& block : gapAckBlocks)
625
+ {
626
+ const UnwrappedTsn curBlockFirstAcked = UnwrappedTsn::AddTo(cumulativeTsnAck, block.start);
627
+
628
+ for (UnwrappedTsn tsn = prevBlockLastAcked.GetNextValue();
629
+ tsn < curBlockFirstAcked && tsn <= maxTsnToNack && tsn < GetNextTsn();
630
+ tsn = tsn.GetNextValue())
631
+ {
632
+ ackInfo.hasPacketLoss |= NackItem(
633
+ tsn,
634
+ /*retransmitNow*/ false,
635
+ /*doFastRetransmit*/ !isInFastRecovery);
636
+ }
637
+
638
+ prevBlockLastAcked = UnwrappedTsn::AddTo(cumulativeTsnAck, block.end);
639
+ }
640
+
641
+ // Note that packets are not NACKED which are above the highest
642
+ // gap-ack-block (or above the cumulative ack TSN if no gap-ack-blocks)
643
+ // as only packets up until the `highestTsnAcked` (see above) should be
644
+ // considered when NACKing.
645
+ }
646
+
647
+ void OutstandingData::AckChunk(AckInfo& ackInfo, UnwrappedTsn tsn, Item& item)
648
+ {
649
+ MS_TRACE();
650
+
651
+ if (!item.IsAcked())
652
+ {
653
+ const size_t serializedLength = GetSerializedChunkLength(item.GetData());
654
+
655
+ ackInfo.bytesAcked += serializedLength;
656
+
657
+ if (item.IsOutstanding())
658
+ {
659
+ this->unackedPayloadBytes -= item.GetData().GetPayloadLength();
660
+ this->unackedPacketBytes -= serializedLength;
661
+ --this->unackedItems;
662
+ }
663
+
664
+ if (item.ShouldBeRetransmitted())
665
+ {
666
+ MS_ASSERT(
667
+ !this->toBeFastRetransmitted.contains(tsn),
668
+ "tsn should not be present in this->toBeFastRetransmitted");
669
+
670
+ this->toBeRetransmitted.erase(tsn);
671
+ }
672
+
673
+ item.Ack();
674
+
675
+ ackInfo.highestTsnAcked = std::max(ackInfo.highestTsnAcked, tsn);
676
+ }
677
+ }
678
+
679
+ bool OutstandingData::NackItem(UnwrappedTsn tsn, bool retransmitNow, bool doFastRetransmit)
680
+ {
681
+ MS_TRACE();
682
+
683
+ Item& item = GetItem(tsn);
684
+
685
+ // Ignore NACKs for chunks that have already been acknowledged.
686
+ if (item.IsAcked())
687
+ {
688
+ return false;
689
+ }
690
+
691
+ const bool wasOutstanding = item.IsOutstanding();
692
+ const Item::NackAction action = item.Nack(retransmitNow);
693
+
694
+ if (wasOutstanding && !item.IsOutstanding())
695
+ {
696
+ this->unackedPayloadBytes -= item.GetData().GetPayloadLength();
697
+ this->unackedPacketBytes -= GetSerializedChunkLength(item.GetData());
698
+ --this->unackedItems;
699
+ }
700
+
701
+ switch (action)
702
+ {
703
+ case Item::NackAction::NOTHING:
704
+ {
705
+ return false;
706
+ }
707
+
708
+ case Item::NackAction::RETRANSMIT:
709
+ {
710
+ if (doFastRetransmit)
711
+ {
712
+ this->toBeFastRetransmitted.insert(tsn);
713
+ }
714
+ else
715
+ {
716
+ this->toBeRetransmitted.insert(tsn);
717
+ }
718
+
719
+ MS_DEBUG_TAG(sctp, "tsn %" PRIu32 " marked for retransmission", tsn.Wrap());
720
+
721
+ break;
722
+ }
723
+
724
+ case Item::NackAction::ABANDON:
725
+ {
726
+ MS_DEBUG_TAG(sctp, "tsn %" PRIu32 " nacked, resulted in abandoning", tsn.Wrap());
727
+
728
+ AbandonAllFor(item);
729
+
730
+ break;
731
+ }
732
+ }
733
+
734
+ return true;
735
+ }
736
+
737
+ void OutstandingData::AbandonAllFor(const OutstandingData::Item& item)
738
+ {
739
+ MS_TRACE();
740
+
741
+ // Erase all remaining chunks from the producer, if any.
742
+ if (this->discardFromSendQueue(item.GetData().GetStreamId(), item.GetMessageId()))
743
+ {
744
+ // There were remaining chunks to be produced for this message. Since the
745
+ // receiver may have already received all chunks (up till now) for this
746
+ // message, we can't just FORWARD-TSN to the last fragment in this
747
+ // (abandoned) message and start sending a new message, as the receiver will
748
+ // then see a new message before the end of the previous one was seen (or
749
+ // skipped over). So create a new fragment, representing the end, that the
750
+ // received will never see as it is abandoned immediately and used as cum
751
+ // TSN in the sent FORWARD-TSN.
752
+ UserData messageEnd(
753
+ item.GetData().GetStreamId(),
754
+ item.GetData().GetStreamSequenceNumber(),
755
+ item.GetData().GetMessageId(),
756
+ item.GetData().GetFragmentSequenceNumber(),
757
+ item.GetData().GetPayloadProtocolId(),
758
+ std::vector<uint8_t>(),
759
+ /*isBeginning*/ false,
760
+ /*isEnd*/ true,
761
+ /*isUnordered*/ item.GetData().IsUnordered());
762
+
763
+ const UnwrappedTsn tsn = GetNextTsn();
764
+
765
+ Item& addedItem = this->outstandingData.emplace_back(
766
+ item.GetMessageId(),
767
+ std::move(messageEnd),
768
+ /*timeSentMs*/ 0,
769
+ /*maxRetransmissions*/ 0,
770
+ /*expiresAtMs*/ OutstandingData::ExpiresAtMsInfinite,
771
+ /*lifecycleId*/ 0);
772
+
773
+ // The added Chunk shouldn't be included in `this->unackedPacketBytes`,
774
+ // so set it as acked.
775
+ addedItem.Ack();
776
+
777
+ MS_DEBUG_TAG(sctp, "adding unsent end placeholder for message at TSN %" PRIu32, tsn.Wrap());
778
+ }
779
+
780
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
781
+
782
+ for (Item& other : this->outstandingData)
783
+ {
784
+ tsn.Increment();
785
+
786
+ if (
787
+ !other.IsAbandoned() && other.GetData().GetStreamId() == item.GetData().GetStreamId() &&
788
+ other.GetMessageId() == item.GetMessageId())
789
+ {
790
+ MS_WARN_TAG(sctp, "marking Chunk %" PRIu32 " as abandoned", tsn.Wrap());
791
+
792
+ if (other.ShouldBeRetransmitted())
793
+ {
794
+ this->toBeFastRetransmitted.erase(tsn);
795
+ this->toBeRetransmitted.erase(tsn);
796
+ }
797
+
798
+ const bool wasOutstanding = other.IsOutstanding();
799
+
800
+ other.Abandon();
801
+
802
+ if (wasOutstanding)
803
+ {
804
+ this->unackedPayloadBytes -= other.GetData().GetPayloadLength();
805
+ this->unackedPacketBytes -= GetSerializedChunkLength(other.GetData());
806
+ --this->unackedItems;
807
+ }
808
+ }
809
+ }
810
+ }
811
+
812
+ std::vector<std::pair<uint32_t /*tsn*/, UserData>> OutstandingData::ExtractChunksThatCanFit(
813
+ std::set<UnwrappedTsn>& chunks, size_t maxLength)
814
+ {
815
+ MS_TRACE();
816
+
817
+ std::vector<std::pair<uint32_t /*tsn*/, UserData>> result;
818
+
819
+ for (auto it = chunks.begin(); it != chunks.end();)
820
+ {
821
+ const UnwrappedTsn tsn = *it;
822
+
823
+ Item& item = GetItem(tsn);
824
+
825
+ MS_ASSERT(item.ShouldBeRetransmitted(), "item should be retransmitted");
826
+ MS_ASSERT(!item.IsOutstanding(), "item should not be outstanding");
827
+ MS_ASSERT(!item.IsAbandoned(), "item should not be abandoned");
828
+ MS_ASSERT(!item.IsAcked(), "item should not be acked");
829
+
830
+ const size_t serializedLength = GetSerializedChunkLength(item.GetData());
831
+
832
+ if (serializedLength <= maxLength)
833
+ {
834
+ item.MarkAsRetransmitted();
835
+ result.emplace_back(tsn.Wrap(), item.GetData().Clone());
836
+ maxLength -= serializedLength;
837
+
838
+ this->unackedPayloadBytes += item.GetData().GetPayloadLength();
839
+ this->unackedPacketBytes += serializedLength;
840
+ ++this->unackedItems;
841
+
842
+ it = chunks.erase(it);
843
+ }
844
+ else
845
+ {
846
+ ++it;
847
+ }
848
+
849
+ // No point in continuing if the packet is full.
850
+ if (maxLength <= this->dataChunkHeaderLength)
851
+ {
852
+ break;
853
+ }
854
+ }
855
+
856
+ return result;
857
+ }
858
+
859
+ void OutstandingData::AssertIsConsistent() const
860
+ {
861
+ MS_TRACE();
862
+
863
+ size_t actualUnackedPayloadBytes{ 0 };
864
+ size_t actualUnackedPacketBytes{ 0 };
865
+ size_t actualUnackedItems{ 0 };
866
+
867
+ std::set<UnwrappedTsn> combinedToBeRetransmitted;
868
+
869
+ combinedToBeRetransmitted.insert(this->toBeRetransmitted.begin(), this->toBeRetransmitted.end());
870
+ combinedToBeRetransmitted.insert(
871
+ this->toBeFastRetransmitted.begin(), this->toBeFastRetransmitted.end());
872
+
873
+ std::set<UnwrappedTsn> actualCombinedToBeRetransmitted;
874
+ UnwrappedTsn tsn = this->lastCumulativeTsnAck;
875
+
876
+ for (const Item& item : this->outstandingData)
877
+ {
878
+ tsn.Increment();
879
+
880
+ if (item.IsOutstanding())
881
+ {
882
+ actualUnackedPayloadBytes += item.GetData().GetPayloadLength();
883
+ actualUnackedPacketBytes += GetSerializedChunkLength(item.GetData());
884
+ ++actualUnackedItems;
885
+ }
886
+
887
+ if (item.ShouldBeRetransmitted())
888
+ {
889
+ actualCombinedToBeRetransmitted.insert(tsn);
890
+ }
891
+ }
892
+
893
+ MS_ASSERT(
894
+ actualUnackedPayloadBytes == this->unackedPayloadBytes,
895
+ "actualUnackedPayloadBytes != this->unackedPayloadBytes");
896
+ MS_ASSERT(
897
+ actualUnackedPacketBytes == this->unackedPacketBytes,
898
+ "actualUnackedPacketBytes != this->unackedPacketBytes");
899
+ MS_ASSERT(actualUnackedItems == this->unackedItems, "actualUnackedItems != this->unackedItems");
900
+ MS_ASSERT(
901
+ actualCombinedToBeRetransmitted == combinedToBeRetransmitted,
902
+ "actualCombinedToBeRetransmitted != combinedToBeRetransmitted");
903
+ }
904
+ } // namespace SCTP
905
+ } // namespace RTC