Unverified 提交 727d1736 authored 作者: José Luis Millán's avatar José Luis Millán 提交者: GitHub

Fix SeqManager (#1032)

上级 6b9a91a8
......@@ -3,7 +3,8 @@
### NEXT
* `RtpRetransmissionBuffer`: Get rid of not necessary `startSeq` private member ([PR #1029](https://github.com/versatica/mediasoup/pull/1029).
* Fix `SeqManager`: Properly consider previous cycle dropped inputs ([PR #1032](https://github.com/versatica/mediasoup/pull/1032)).
* `RtpRetransmissionBuffer`: Get rid of not necessary `startSeq` private member ([PR #1029](https://github.com/versatica/mediasoup/pull/1029)).
* Node: Upgrade TypeScript to 5.0.2.
......@@ -15,7 +16,7 @@
### 3.11.14
* Refactor RTP retransmission buffer in a separate and testable `RTC::RetransmissionBuffer` class ([PR #1023](https://github.com/versatica/mediasoup/pull/1023).
* Refactor RTP retransmission buffer in a separate and testable `RTC::RetransmissionBuffer` class ([PR #1023](https://github.com/versatica/mediasoup/pull/1023)).
* Update NPM deps.
......
......@@ -29,7 +29,6 @@ namespace RTC
private:
static const SeqLowerThan isSeqLowerThan;
static const SeqHigherThan isSeqHigherThan;
static T Delta(const T lhs, const T rhs);
public:
static bool IsSeqLowerThan(const T lhs, const T rhs);
......@@ -46,6 +45,9 @@ namespace RTC
T GetMaxInput() const;
T GetMaxOutput() const;
private:
void ClearDropped();
private:
T base{ 0 };
T maxOutput{ 0 };
......
......@@ -39,14 +39,6 @@ namespace RTC
return isSeqHigherThan(lhs, rhs);
}
template<typename T, uint8_t N>
T SeqManager<T, N>::Delta(const T lhs, const T rhs)
{
T value = (lhs > rhs) ? (lhs - rhs) : (MaxValue - rhs + lhs);
return value & MaxValue;
}
template<typename T, uint8_t N>
void SeqManager<T, N>::Sync(T input)
{
......@@ -84,48 +76,55 @@ namespace RTC
// There are dropped inputs. Synchronize.
if (!this->dropped.empty())
{
// Delete dropped inputs older than input - MaxValue/2.
size_t droppedCount = this->dropped.size();
const size_t threshold = (input - MaxValue / 2) & MaxValue;
auto it = this->dropped.lower_bound(threshold);
this->dropped.erase(this->dropped.begin(), it);
this->base = (this->base - (droppedCount - this->dropped.size())) & MaxValue;
// Count dropped entries before 'input' in order to adapt the base.
droppedCount = this->dropped.size();
it = this->dropped.lower_bound(input);
if (it != this->dropped.end())
// Check whether this input was dropped.
if (this->dropped.find(input) != this->dropped.end())
{
// Check whether this input was dropped.
if (*it == input)
{
MS_DEBUG_DEV("trying to send a dropped input");
MS_DEBUG_DEV("trying to send a dropped input");
return false;
}
return false;
}
droppedCount -= std::distance(it, this->dropped.end());
// Dropped entries count that must be considered for the output.
size_t count{ 0 };
/*
* Consider values lower than input and those higher that input
* which belong to a previous cycle.
*/
for (const auto& value : this->dropped)
{
// clang-format off
if
(
IsSeqLowerThan(value, input) ||
(
(value > input && (value - input > MaxValue / 3)) ||
(value < input && (input - value > MaxValue / 3))
)
)
// clang-format on
{
count++;
}
}
base = (this->base - droppedCount) & MaxValue;
base = (this->base - count) & MaxValue;
}
output = (input + base) & MaxValue;
T idelta = SeqManager<T, N>::Delta(input, this->maxInput);
T odelta = SeqManager<T, N>::Delta(output, this->maxOutput);
// New input is higher than the maximum seen. But less than acceptable units higher.
// Keep it as the maximum seen. See Drop().
if (idelta < MaxValue / 2)
if (IsSeqHigherThan(input, this->maxInput))
this->maxInput = input;
// New output is higher than the maximum seen. But less than acceptable units higher.
// Keep it as the maximum seen. See Sync().
if (odelta < MaxValue / 2)
if (IsSeqHigherThan(output, this->maxOutput))
this->maxOutput = output;
ClearDropped();
return true;
}
......@@ -141,8 +140,48 @@ namespace RTC
return this->maxOutput;
}
/*
* Delete droped inputs greater than maxInput that belong to a previous
* cycle.
*/
template<typename T, uint8_t N>
void SeqManager<T, N>::ClearDropped()
{
// Cleanup dropped values.
if (this->dropped.empty())
{
return;
}
const size_t threshold = (this->maxInput + MaxValue / 3) & MaxValue;
const size_t previousDroppedSize = this->dropped.size();
const auto it1 = this->dropped.upper_bound(this->maxInput);
const auto it2 = this->dropped.lower_bound(threshold);
// There is no dropped value greater than this->maxInput.
if (it1 == this->dropped.end())
{
return;
}
// There is a single value in the range.
if (it1 == it2)
{
this->dropped.erase(it1);
}
// There are many values in the range.
else
{
this->dropped.erase(it1, it2);
}
// Adapt base.
this->base = (this->base - (previousDroppedSize - this->dropped.size())) & MaxValue;
}
// Explicit instantiation to have all SeqManager definitions in this file.
template class SeqManager<uint8_t>;
template class SeqManager<uint8_t, 3>; // For testing.
template class SeqManager<uint16_t>;
template class SeqManager<uint16_t, 15>; // For PictureID (15 bits).
template class SeqManager<uint32_t>;
......
......@@ -56,7 +56,7 @@ void validate(SeqManager<T, N>& seqManager, std::vector<TestSeqManagerInput<T>>&
}
}
SCENARIO("SeqManager", "[rtc]")
SCENARIO("SeqManager", "[rtc][SeqMananger]")
{
SECTION("0 is greater than 65000")
{
......@@ -577,10 +577,10 @@ SCENARIO("SeqManager", "[rtc]")
{
{ 0, 1, true, false, 0, 0 },
};
for (uint16_t j = 0; j < 100; ++j) {
for (uint16_t j = 0; j < 3; ++j) {
for (uint16_t i = 1; i < std::numeric_limits<uint16_t>::max(); ++i) {
uint16_t output = i + 1;
inputs.push_back({ i, output, false, false, 0, i });
const uint16_t output = i + 1;
inputs.emplace_back( i, output, false, false, 0, i );
}
}
// clang-format on
......@@ -596,10 +596,10 @@ SCENARIO("SeqManager", "[rtc]")
{
{ 0, 1, true, false, 0, 0 },
};
for (uint16_t j = 0; j < 100; ++j) {
for (uint16_t j = 0; j < 3; ++j) {
for (uint16_t i = 1; i < kMaxNumberFor15Bits; ++i) {
uint16_t output = i + 1;
inputs.push_back({ i, output, false, false, 0, i });
const uint16_t output = i + 1;
inputs.emplace_back( i, output, false, false, 0, i );
}
}
// clang-format on
......@@ -607,4 +607,46 @@ SCENARIO("SeqManager", "[rtc]")
SeqManager<uint16_t, 15> seqManager;
validate(seqManager, inputs);
}
SECTION("should produce same output for same old input before drop (15 bits range)")
{
// clang-format off
std::vector<TestSeqManagerInput<uint16_t>> inputs =
{
{ 10, 1, true, false }, // sync.
{ 11, 2, false, false },
{ 12, 3, false, false },
{ 13, 4, false, false },
{ 14, 0, false, true }, // drop.
{ 15, 5, false, false },
{ 12, 3, false, false }
};
// clang-format on
SeqManager<uint16_t, 15> seqManager;
validate(seqManager, inputs);
}
SECTION("should properly clean previous cycle drops")
{
// clang-format off
std::vector<TestSeqManagerInput<uint8_t>> inputs =
{
{ 1, 1, false, false, 0 },
{ 2, 0, false, true, 0 }, // Drop.
{ 3, 2, false, false, 0 },
{ 4, 3, false, false, 0 },
{ 5, 4, false, false, 0 },
{ 6, 5, false, false, 0 },
{ 7, 6, false, false, 0 },
{ 0, 7, false, false, 0 },
{ 1, 0, false, false, 0 },
{ 2, 1, false, false, 0 },
{ 3, 2, false, false, 0 }
};
// clang-format on
SeqManager<uint8_t, 3> seqManager;
validate(seqManager, inputs);
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论