Unverified 提交 5823632a authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

RtpRetransmissionBuffer: consider the case of packet with newest timestamp but…

RtpRetransmissionBuffer: consider the case of packet with newest timestamp but "old" seq number (#1039)

Fixes #1037

- We must consider the case in which, due for example to huge packet loss, received packet has higher timestamp but "older" seq than the newest packet in the buffer and, if so, use it to clear too old packets rather than the newest packet in the buffer.
- Bonus track: restore buffer size to 2500 since increasing it didn't indeed help (it shouldn't help as proven).
上级 cb039ec4
# Changelog # Changelog
### NEXT
* `RtpRetransmissionBuffer`: Consider the case of packet with newest timestamp but "old" seq number ([PR #1039](https://github.com/versatica/mediasoup/pull/1039)).
### 3.11.17 ### 3.11.17
* Add `transport.setMinOutgoingBitrate()` method ([PR #1038](https://github.com/versatica/mediasoup/pull/1038), credits to @ jcague). * Add `transport.setMinOutgoingBitrate()` method ([PR #1038](https://github.com/versatica/mediasoup/pull/1038), credits to @ jcague).
......
...@@ -46,7 +46,7 @@ namespace RTC ...@@ -46,7 +46,7 @@ namespace RTC
Item* GetNewest() const; Item* GetNewest() const;
void RemoveOldest(); void RemoveOldest();
void RemoveOldest(uint16_t numItems); void RemoveOldest(uint16_t numItems);
void ClearTooOld(); bool ClearTooOld(uint32_t newestTimestamp);
bool IsTooOld(uint32_t timestamp, uint32_t newestTimestamp) const; bool IsTooOld(uint32_t timestamp, uint32_t newestTimestamp) const;
Item* FillItem( Item* FillItem(
Item* item, RTC::RtpPacket* packet, std::shared_ptr<RTC::RtpPacket>& sharedPacket) const; Item* item, RTC::RtpPacket* packet, std::shared_ptr<RTC::RtpPacket>& sharedPacket) const;
......
...@@ -81,11 +81,41 @@ namespace RTC ...@@ -81,11 +81,41 @@ namespace RTC
return; return;
} }
auto* oldestItem = GetOldest();
auto* newestItem = GetNewest();
// Clear too old packets in the buffer. // Clear too old packets in the buffer.
ClearTooOld(); // NOTE: Here we must consider the case in which, due for example to huge
// packet loss, received packet has higher timestamp but "older" seq number
// than the newest packet in the buffer and, if so, use it to clear too old
// packets rather than the newest packet in the buffer.
auto newestTimestamp =
RTC::SeqManager<uint32_t>::IsSeqHigherThan(timestamp, newestItem->timestamp)
? timestamp
: newestItem->timestamp;
// ClearTooOld() returns true if at least one packet has been removed from
// the front.
if (ClearTooOld(newestTimestamp))
{
// Buffer content has been modified so we must check it again.
if (this->buffer.empty())
{
MS_DEBUG_DEV(
"buffer empty after clearing too old packets [seq:%" PRIu16 ", timestamp:%" PRIu32 "]",
seq,
timestamp);
const auto* oldestItem = GetOldest(); auto* item = new Item();
const auto* newestItem = GetNewest();
this->buffer.push_back(FillItem(item, packet, sharedPacket));
return;
}
oldestItem = GetOldest();
newestItem = GetNewest();
}
MS_ASSERT(oldestItem != nullptr, "oldest item doesn't exist"); MS_ASSERT(oldestItem != nullptr, "oldest item doesn't exist");
MS_ASSERT(newestItem != nullptr, "newest item doesn't exist"); MS_ASSERT(newestItem != nullptr, "newest item doesn't exist");
...@@ -456,26 +486,22 @@ namespace RTC ...@@ -456,26 +486,22 @@ namespace RTC
} }
} }
void RtpRetransmissionBuffer::ClearTooOld() bool RtpRetransmissionBuffer::ClearTooOld(uint32_t newestTimestamp)
{ {
MS_TRACE(); MS_TRACE();
const auto* newestItem = GetNewest();
if (!newestItem)
{
return;
}
RtpRetransmissionBuffer::Item* oldestItem{ nullptr }; RtpRetransmissionBuffer::Item* oldestItem{ nullptr };
bool itemsRemoved{ false };
// Go through all buffer items starting with the first and free all items // Go through all buffer items starting with the first and free all items
// that contain too old packets. // that contain too old packets.
while ((oldestItem = GetOldest())) while ((oldestItem = GetOldest()))
{ {
if (IsTooOld(oldestItem->timestamp, newestItem->timestamp)) if (IsTooOld(oldestItem->timestamp, newestTimestamp))
{ {
RemoveOldest(); RemoveOldest();
itemsRemoved = true;
} }
// If current oldest stored packet is not too old, exit the loop since we // If current oldest stored packet is not too old, exit the loop since we
// know that packets stored after it are guaranteed to be newer. // know that packets stored after it are guaranteed to be newer.
...@@ -484,6 +510,8 @@ namespace RTC ...@@ -484,6 +510,8 @@ namespace RTC
break; break;
} }
} }
return itemsRemoved;
} }
bool RtpRetransmissionBuffer::IsTooOld(uint32_t timestamp, uint32_t newestTimestamp) const bool RtpRetransmissionBuffer::IsTooOld(uint32_t timestamp, uint32_t newestTimestamp) const
......
...@@ -12,7 +12,7 @@ namespace RTC ...@@ -12,7 +12,7 @@ namespace RTC
/* Static. */ /* Static. */
// Limit max number of items in the retransmission buffer. // Limit max number of items in the retransmission buffer.
static constexpr size_t RetransmissionBufferMaxItems{ 5000u }; static constexpr size_t RetransmissionBufferMaxItems{ 2500u };
// 17: 16 bit mask + the initial sequence number. // 17: 16 bit mask + the initial sequence number.
static constexpr size_t MaxRequestedPackets{ 17u }; static constexpr size_t MaxRequestedPackets{ 17u };
thread_local static std::vector<RTC::RtpRetransmissionBuffer::Item*> RetransmissionContainer( thread_local static std::vector<RTC::RtpRetransmissionBuffer::Item*> RetransmissionContainer(
......
...@@ -230,6 +230,29 @@ SCENARIO("RtpRetransmissionBuffer", "[rtp][rtx]") ...@@ -230,6 +230,29 @@ SCENARIO("RtpRetransmissionBuffer", "[rtp][rtx]")
// clang-format on // clang-format on
} }
SECTION("packet with very newest timestamp is inserted as newest item despite its seq is old")
{
uint16_t maxItems{ 4 };
uint32_t maxRetransmissionDelayMs{ 2000u };
uint32_t clockRate{ 90000 };
RtpMyRetransmissionBuffer myRetransmissionBuffer(maxItems, maxRetransmissionDelayMs, clockRate);
// Scenario based on https://github.com/versatica/mediasoup/issues/1037.
myRetransmissionBuffer.Insert(24816, 1024930187);
myRetransmissionBuffer.Insert(24980, 1025106407);
myRetransmissionBuffer.Insert(18365, 1026593387);
// clang-format off
myRetransmissionBuffer.AssertBuffer(
{
{ true, 18365, 1026593387 }
}
);
// clang-format on
}
SECTION("fuzzer generated packets") SECTION("fuzzer generated packets")
{ {
uint16_t maxItems{ 2500u }; uint16_t maxItems{ 2500u };
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论