提交 8e27e382 authored 作者: José Luis Millán's avatar José Luis Millán

NackGenerator: use proper 16 bit size sequence number comparison

上级 1f3a7a62
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "common.hpp" #include "common.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "RTC/SeqManager.hpp"
#include "handles/Timer.hpp" #include "handles/Timer.hpp"
#include <map> #include <map>
#include <vector> #include <vector>
...@@ -23,10 +24,10 @@ namespace RTC ...@@ -23,10 +24,10 @@ namespace RTC
struct NackInfo struct NackInfo
{ {
NackInfo(){}; NackInfo(){};
explicit NackInfo(uint32_t seq32, uint32_t sendAtSeq32); explicit NackInfo(uint16_t seq, uint16_t sendAtSeq);
uint32_t seq32{ 0 }; uint16_t seq{ 0 };
uint32_t sendAtSeq32{ 0 }; uint16_t sendAtSeq{ 0 };
uint64_t sentAtTime{ 0 }; uint64_t sentAtTime{ 0 };
uint8_t retries{ 0 }; uint8_t retries{ 0 };
}; };
...@@ -44,7 +45,7 @@ namespace RTC ...@@ -44,7 +45,7 @@ namespace RTC
bool ReceivePacket(RTC::RtpPacket* packet); bool ReceivePacket(RTC::RtpPacket* packet);
private: private:
void AddPacketsToNackList(uint32_t seq32Start, uint32_t seq32End); void AddPacketsToNackList(uint16_t seqStart, uint16_t seqEnd);
void RemoveFromNackListOlderThan(RTC::RtpPacket* packet); void RemoveFromNackListOlderThan(RTC::RtpPacket* packet);
std::vector<uint16_t> GetNackBatch(NackFilter filter); std::vector<uint16_t> GetNackBatch(NackFilter filter);
void MayRunTimer() const; void MayRunTimer() const;
...@@ -59,16 +60,16 @@ namespace RTC ...@@ -59,16 +60,16 @@ namespace RTC
// Allocated by this. // Allocated by this.
Timer* timer{ nullptr }; Timer* timer{ nullptr };
// Others. // Others.
std::map<uint32_t, NackInfo> nackList; std::map<uint16_t, NackInfo, SeqManager<uint16_t>::SeqLowerThan> nackList;
bool started{ false }; bool started{ false };
uint32_t lastSeq32{ 0 }; // Extended seq number of last valid packet. uint16_t lastSeq{ 0 }; // Seq number of last valid packet.
uint32_t rtt{ 0 }; // Round trip time (ms). uint32_t rtt{ 0 }; // Round trip time (ms).
}; };
// Inline instance methods. // Inline instance methods.
inline NackGenerator::NackInfo::NackInfo(uint32_t seq32, uint32_t sendAtSeq32) inline NackGenerator::NackInfo::NackInfo(uint16_t seq, uint16_t sendAtSeq)
: seq32(seq32), sendAtSeq32(sendAtSeq32) : seq(seq), sendAtSeq(sendAtSeq)
{ {
} }
} // namespace RTC } // namespace RTC
......
...@@ -13,7 +13,7 @@ namespace RTC ...@@ -13,7 +13,7 @@ namespace RTC
public: public:
static constexpr T MaxValue = std::numeric_limits<T>::max(); static constexpr T MaxValue = std::numeric_limits<T>::max();
private: public:
struct SeqLowerThan struct SeqLowerThan
{ {
bool operator()(const T& lhs, const T& rhs) const; bool operator()(const T& lhs, const T& rhs) const;
......
...@@ -263,6 +263,7 @@ ...@@ -263,6 +263,7 @@
# C++ source files # C++ source files
'test/tests.cpp', 'test/tests.cpp',
'test/test-nack.cpp', 'test/test-nack.cpp',
'test/test-nack-generator.cpp',
'test/test-rtp.cpp', 'test/test-rtp.cpp',
'test/test-rtcp.cpp', 'test/test-rtcp.cpp',
'test/test-bitrate.cpp', 'test/test-bitrate.cpp',
......
...@@ -38,11 +38,11 @@ namespace RTC ...@@ -38,11 +38,11 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
uint32_t seq32 = packet->GetExtendedSequenceNumber(); uint16_t seq = packet->GetSequenceNumber();
if (!this->started) if (!this->started)
{ {
this->lastSeq32 = seq32; this->lastSeq = seq;
this->started = true; this->started = true;
return false; return false;
...@@ -53,22 +53,22 @@ namespace RTC ...@@ -53,22 +53,22 @@ namespace RTC
RemoveFromNackListOlderThan(packet); RemoveFromNackListOlderThan(packet);
// Obviously never nacked, so ignore. // Obviously never nacked, so ignore.
if (seq32 == this->lastSeq32) if (seq == this->lastSeq)
{ {
return false; return false;
} }
if (seq32 == this->lastSeq32 + 1) if (seq == this->lastSeq + 1)
{ {
this->lastSeq32++; this->lastSeq++;
return false; return false;
} }
// May be an out of order packet, or already handled retransmitted packet, // May be an out of order packet, or already handled retransmitted packet,
// or a retransmitted packet. // or a retransmitted packet.
if (seq32 < this->lastSeq32) if (SeqManager<uint16_t>::IsSeqLowerThan(seq, this->lastSeq))
{ {
auto it = this->nackList.find(seq32); auto it = this->nackList.find(seq);
// It was a nacked packet. // It was a nacked packet.
if (it != this->nackList.end()) if (it != this->nackList.end())
...@@ -87,7 +87,7 @@ namespace RTC ...@@ -87,7 +87,7 @@ namespace RTC
// Out of order packet or already handled NACKed packet. // Out of order packet or already handled NACKed packet.
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtx, rtx,
"ignoring out of order packet or already handled NACKed packet [ssrc:%" PRIu32 "ignoring old packet not present in the NACK list [ssrc:%" PRIu32
", seq:%" PRIu16 "]", ", seq:%" PRIu16 "]",
packet->GetSsrc(), packet->GetSsrc(),
packet->GetSequenceNumber()); packet->GetSequenceNumber());
...@@ -96,8 +96,8 @@ namespace RTC ...@@ -96,8 +96,8 @@ namespace RTC
} }
// Otherwise we may have lost some packets. // Otherwise we may have lost some packets.
AddPacketsToNackList(this->lastSeq32 + 1, seq32); AddPacketsToNackList(this->lastSeq + 1, seq);
this->lastSeq32 = seq32; this->lastSeq = seq;
// Check if there are any nacks that are waiting for this seq number. // Check if there are any nacks that are waiting for this seq number.
std::vector<uint16_t> nackBatch = GetNackBatch(NackFilter::SEQ); std::vector<uint16_t> nackBatch = GetNackBatch(NackFilter::SEQ);
...@@ -110,16 +110,16 @@ namespace RTC ...@@ -110,16 +110,16 @@ namespace RTC
return false; return false;
} }
void NackGenerator::AddPacketsToNackList(uint32_t seq32Start, uint32_t seq32End) void NackGenerator::AddPacketsToNackList(uint16_t seqStart, uint16_t seqEnd)
{ {
MS_TRACE(); MS_TRACE();
if (seq32End > MaxPacketAge) if (seqEnd > MaxPacketAge)
{ {
uint32_t numItemsBefore = this->nackList.size(); uint32_t numItemsBefore = this->nackList.size();
// Remove old packets. // Remove old packets.
auto it = this->nackList.lower_bound(seq32End - MaxPacketAge); auto it = this->nackList.lower_bound(seqEnd - MaxPacketAge);
this->nackList.erase(this->nackList.begin(), it); this->nackList.erase(this->nackList.begin(), it);
...@@ -129,21 +129,21 @@ namespace RTC ...@@ -129,21 +129,21 @@ namespace RTC
{ {
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtx, rtx,
"removed %" PRIu32 " NACK items due to too old seq number [seq32End:%" PRIu32 "]", "removed %" PRIu32 " NACK items due to too old seq number [seqEnd:%" PRIu16 "]",
numItemsRemoved, numItemsRemoved,
seq32End); seqEnd);
} }
} }
// If the nack list is too large, clear it and request a key frame. // If the nack list is too large, clear it and request a key frame.
uint32_t numNewNacks = seq32End - seq32Start; uint16_t numNewNacks = seqEnd - seqStart;
if (this->nackList.size() + numNewNacks > MaxNackPackets) if (this->nackList.size() + numNewNacks > MaxNackPackets)
{ {
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtx, rtx,
"NACK list too large, clearing it and requesting a key frame [seq32End:%" PRIu32 "]", "NACK list too large, clearing it and requesting a key frame [seqEnd:%" PRIu16 "]",
seq32End); seqEnd);
this->nackList.clear(); this->nackList.clear();
this->listener->OnNackGeneratorKeyFrameRequired(); this->listener->OnNackGeneratorKeyFrameRequired();
...@@ -151,31 +151,31 @@ namespace RTC ...@@ -151,31 +151,31 @@ namespace RTC
return; return;
} }
for (uint32_t seq32 = seq32Start; seq32 != seq32End; ++seq32) for (uint16_t seq = seqStart; seq != seqEnd; ++seq)
{ {
// NOTE: Let the packet become out of order for a while without requesting // NOTE: Let the packet become out of order for a while without requesting
// it into a NACK. // it into a NACK.
// TODO: To be done. // TODO: To be done.
uint32_t sendAtSeq32 = seq32 + 0; uint16_t sendAtSeq = seq + 0;
NackInfo nackInfo(seq32, sendAtSeq32); NackInfo nackInfo(seq, sendAtSeq);
MS_ASSERT( MS_ASSERT(
this->nackList.find(seq32) == this->nackList.end(), "packet already in the NACK list"); this->nackList.find(seq) == this->nackList.end(), "packet already in the NACK list");
this->nackList[seq32] = nackInfo; this->nackList[seq] = nackInfo;
} }
} }
// Delete all the entries in the NACK list whose key (seq32) is older than // Delete all the entries in the NACK list whose key (seq) is older than
// the given one. // the given one.
void NackGenerator::RemoveFromNackListOlderThan(RTC::RtpPacket* packet) void NackGenerator::RemoveFromNackListOlderThan(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
uint32_t seq32 = packet->GetExtendedSequenceNumber(); uint16_t seq = packet->GetSequenceNumber();
uint32_t numItemsBefore = this->nackList.size(); uint32_t numItemsBefore = this->nackList.size();
auto it = this->nackList.lower_bound(seq32); auto it = this->nackList.lower_bound(seq);
this->nackList.erase(this->nackList.begin(), it); this->nackList.erase(this->nackList.begin(), it);
...@@ -202,9 +202,10 @@ namespace RTC ...@@ -202,9 +202,10 @@ namespace RTC
while (it != this->nackList.end()) while (it != this->nackList.end())
{ {
NackInfo& nackInfo = it->second; NackInfo& nackInfo = it->second;
uint16_t seq = nackInfo.seq32 % (1 << 16); uint16_t seq = nackInfo.seq;
if (filter == NackFilter::SEQ && nackInfo.sentAtTime == 0 && this->lastSeq32 >= nackInfo.sendAtSeq32) if (filter == NackFilter::SEQ && nackInfo.sentAtTime == 0 &&
SeqManager<uint16_t>::IsSeqHigherThan(this->lastSeq, nackInfo.sendAtSeq))
{ {
nackInfo.retries++; nackInfo.retries++;
nackInfo.sentAtTime = now; nackInfo.sentAtTime = now;
......
...@@ -107,7 +107,7 @@ namespace RTC ...@@ -107,7 +107,7 @@ namespace RTC
this->cycles + static_cast<uint32_t>(packet->GetSequenceNumber())); this->cycles + static_cast<uint32_t>(packet->GetSequenceNumber()));
// Pass the packet to the NackGenerator and return true just if this was a // Pass the packet to the NackGenerator and return true just if this was a
// NACked packet. // NACKed packet.
if (this->params.useNack) if (this->params.useNack)
return this->nackGenerator->ReceivePacket(packet); return this->nackGenerator->ReceivePacket(packet);
......
#include "include/catch.hpp"
#include "common.hpp"
#include "RTC/NackGenerator.hpp"
#include "RTC/RtpPacket.hpp"
#include <vector>
using namespace RTC;
struct Input
{
Input() = default;
Input(uint16_t seq, uint16_t firstNacked, size_t numNacked)
: seq(seq), firstNacked(firstNacked), numNacked(numNacked)
{}
uint16_t seq{ 0 };
uint16_t firstNacked{ 0 };
size_t numNacked{ 0 };
} currentInput;
class TestNackGeneratorListener : public NackGenerator::Listener
{
void OnNackGeneratorNackRequired(const std::vector<uint16_t>& seqNumbers) override
{
auto it = seqNumbers.begin();
auto firstNacked = *it;
auto numNacked = seqNumbers.size();
REQUIRE(currentInput.firstNacked == firstNacked);
REQUIRE(currentInput.numNacked == numNacked);
};
void OnNackGeneratorKeyFrameRequired() override
{
}
};
uint8_t rtpBuffer[] =
{
0b10000000, 0b01111011, 0b01010010, 0b00001110,
0b01011011, 0b01101011, 0b11001010, 0b10110101,
0, 0, 0, 2
};
// [pt:123, seq:21006, timestamp:1533790901]
RtpPacket* packet = RtpPacket::Parse(rtpBuffer, sizeof(rtpBuffer));
void validate(std::vector<Input>& inputs)
{
TestNackGeneratorListener* listener = new TestNackGeneratorListener();
NackGenerator* nackGenerator = new NackGenerator(listener);
for (auto input : inputs)
{
currentInput = input;
packet->SetSequenceNumber(input.seq);
nackGenerator->ReceivePacket(packet);
}
};
SCENARIO("NACK generator", "[rtp][rtcp]")
{
SECTION("ignore too old packets")
{
std::vector<Input> inputs =
{
{ 2371, 0, 0 },
{ 2372, 0, 0 },
{ 2373, 0, 0 },
{ 2374, 0, 0 },
{ 2375, 0, 0 },
{ 2376, 0, 0 },
{ 2377, 0, 0 },
{ 2378, 0, 0 },
{ 2379, 0, 0 },
{ 2380, 0, 0 },
{ 2254, 0, 0 },
{ 2250, 0, 0 },
};
validate(inputs);
}
SECTION("generate NACK for missing ordered packet")
{
std::vector<Input> inputs =
{
{ 2381, 0, 0 },
{ 2383, 2382, 1 }
};
validate(inputs);
}
SECTION("sequence wrap generates no NACK")
{
std::vector<Input> inputs =
{
{ 65534, 0, 0 },
{ 65535, 0, 0 },
{ 0, 0, 0 }
};
validate(inputs);
}
SECTION("generate NACK after sequence wrap")
{
std::vector<Input> inputs =
{
{ 65534, 0, 0 },
{ 65535, 0, 0 },
{ 1, 0, 1 }
};
validate(inputs);
}
SECTION("generate NACK after sequence wrap, and yet another NACK")
{
std::vector<Input> inputs =
{
{ 65534, 0, 0 },
{ 65535, 0, 0 },
{ 1, 0, 1 },
{ 11, 2, 9 }
};
validate(inputs);
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论