提交 860d8a62 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

NackGenerator: manage key frames

上级 c1c18848
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "RTC/SeqManager.hpp" #include "RTC/SeqManager.hpp"
#include "handles/Timer.hpp" #include "handles/Timer.hpp"
#include <map> #include <map>
#include <set>
#include <vector> #include <vector>
namespace RTC namespace RTC
...@@ -46,8 +47,9 @@ namespace RTC ...@@ -46,8 +47,9 @@ namespace RTC
size_t GetNackListLength() const; size_t GetNackListLength() const;
private: private:
void CleanOldNackItems(uint16_t seq);
void AddPacketsToNackList(uint16_t seqStart, uint16_t seqEnd); void AddPacketsToNackList(uint16_t seqStart, uint16_t seqEnd);
void RemoveFromNackListOlderThan(RTC::RtpPacket* packet); void RemoveNackItemsUntilKeyFrame();
std::vector<uint16_t> GetNackBatch(NackFilter filter); std::vector<uint16_t> GetNackBatch(NackFilter filter);
void MayRunTimer() const; void MayRunTimer() const;
...@@ -62,6 +64,8 @@ namespace RTC ...@@ -62,6 +64,8 @@ namespace RTC
Timer* timer{ nullptr }; Timer* timer{ nullptr };
// Others. // Others.
std::map<uint16_t, NackInfo, SeqManager<uint16_t>::SeqLowerThan> nackList; std::map<uint16_t, NackInfo, SeqManager<uint16_t>::SeqLowerThan> nackList;
// This set is just supposed to hold zero or one entries.
std::set<uint16_t, SeqManager<uint16_t>::SeqLowerThan> keyFrameList;
bool started{ false }; bool started{ false };
uint16_t lastSeq{ 0 }; // 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).
......
...@@ -4,11 +4,13 @@ ...@@ -4,11 +4,13 @@
#include "RTC/NackGenerator.hpp" #include "RTC/NackGenerator.hpp"
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include <utility> // std::make_pair()
namespace RTC namespace RTC
{ {
/* Static. */ /* Static. */
constexpr size_t MaxPacketAge{ 5000 };
constexpr size_t MaxNackPackets{ 1000 }; constexpr size_t MaxNackPackets{ 1000 };
constexpr uint32_t DefaultRtt{ 100 }; constexpr uint32_t DefaultRtt{ 100 };
constexpr uint8_t MaxNackRetries{ 8 }; constexpr uint8_t MaxNackRetries{ 8 };
...@@ -38,30 +40,22 @@ namespace RTC ...@@ -38,30 +40,22 @@ namespace RTC
MS_TRACE(); MS_TRACE();
uint16_t seq = packet->GetSequenceNumber(); uint16_t seq = packet->GetSequenceNumber();
bool isKeyFrame = packet->IsKeyFrame();
if (!this->started) if (!this->started)
{ {
this->lastSeq = seq; this->lastSeq = seq;
this->started = true; this->started = true;
if (isKeyFrame)
this->keyFrameList.insert(seq);
return false; return false;
} }
// If a key frame remove all the items in the nack list older than this seq.
if (packet->IsKeyFrame())
RemoveFromNackListOlderThan(packet);
// Obviously never nacked, so ignore. // Obviously never nacked, so ignore.
if (seq == this->lastSeq) if (seq == this->lastSeq)
{
return false;
}
if (seq == this->lastSeq + 1)
{
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.
...@@ -93,7 +87,28 @@ namespace RTC ...@@ -93,7 +87,28 @@ namespace RTC
return false; return false;
} }
// Otherwise we may have lost some packets. // If we are here it means that we may have lost some packets so seq
// is newer than the latest seq seen.
CleanOldNackItems(seq);
// If a key frame remove all the items in the nack list older than our
// previous key frame seq.
if (isKeyFrame)
{
RemoveNackItemsUntilKeyFrame();
this->keyFrameList.insert(seq);
}
// Expected seq number so nothing else to do.
if (seq == this->lastSeq + 1)
{
this->lastSeq++;
return false;
}
AddPacketsToNackList(this->lastSeq + 1, seq); AddPacketsToNackList(this->lastSeq + 1, seq);
this->lastSeq = seq; this->lastSeq = seq;
...@@ -108,6 +123,19 @@ namespace RTC ...@@ -108,6 +123,19 @@ namespace RTC
return false; return false;
} }
void NackGenerator::CleanOldNackItems(uint16_t seq)
{
MS_TRACE();
auto it = this->nackList.lower_bound(seq - MaxPacketAge);
this->nackList.erase(this->nackList.begin(), it);
auto it2 = this->keyFrameList.lower_bound(seq - MaxPacketAge);
this->keyFrameList.erase(this->keyFrameList.begin(), it2);
}
void NackGenerator::AddPacketsToNackList(uint16_t seqStart, uint16_t seqEnd) void NackGenerator::AddPacketsToNackList(uint16_t seqStart, uint16_t seqEnd)
{ {
MS_TRACE(); MS_TRACE();
...@@ -123,6 +151,7 @@ namespace RTC ...@@ -123,6 +151,7 @@ namespace RTC
seqEnd); seqEnd);
this->nackList.clear(); this->nackList.clear();
this->keyFrameList.clear();
this->listener->OnNackGeneratorKeyFrameRequired(); this->listener->OnNackGeneratorKeyFrameRequired();
return; return;
...@@ -130,40 +159,42 @@ namespace RTC ...@@ -130,40 +159,42 @@ namespace RTC
for (uint16_t seq = seqStart; seq != seqEnd; ++seq) for (uint16_t seq = seqStart; seq != seqEnd; ++seq)
{ {
// NOTE: Let the packet become out of order for a while without requesting MS_ASSERT(this->nackList.find(seq) == this->nackList.end(), "packet already in the NACK list");
// it into a NACK.
// NOTE: We may not generate a NACK for this seq right now, but wait a bit
// assuming that this packet may be in its way.
// TODO: To be done. // TODO: To be done.
uint16_t sendAtSeq = seq + 0; uint16_t sendAtSeq = seq + 0;
NackInfo nackInfo(seq, sendAtSeq);
MS_ASSERT(this->nackList.find(seq) == this->nackList.end(), "packet already in the NACK list"); this->nackList.emplace(std::make_pair(seq, NackInfo{ seq, sendAtSeq }));
this->nackList[seq] = nackInfo;
} }
} }
// Delete all the entries in the NACK list whose key (seq) is older than void NackGenerator::RemoveNackItemsUntilKeyFrame()
// the given one.
void NackGenerator::RemoveFromNackListOlderThan(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
uint16_t seq = packet->GetSequenceNumber(); // No previous key frame, so do nothing.
uint32_t numItemsBefore = this->nackList.size(); if (this->keyFrameList.empty())
return;
auto it = this->nackList.lower_bound(seq); auto it = this->keyFrameList.begin();
auto seq = *it;
size_t numItemsBefore = this->nackList.size();
auto it2 = this->nackList.lower_bound(seq);
this->nackList.erase(this->nackList.begin(), it); this->nackList.erase(this->nackList.begin(), it2);
this->keyFrameList.erase(seq);
uint32_t numItemsRemoved = numItemsBefore - this->nackList.size(); size_t numItemsRemoved = numItemsBefore - this->nackList.size();
if (numItemsRemoved > 0) if (numItemsRemoved > 0)
{ {
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtx, rtx,
"removed %" PRIu32 " old NACK items older than received key frame [seq:%" PRIu16 "]", "removed %zu old NACK items older than received key frame [seq:%" PRIu16 "]",
numItemsRemoved, numItemsRemoved,
packet->GetSequenceNumber()); seq);
} }
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论