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

worker: Fix NACK timer and avoid negative RTT (#1082)

- Stop NACK timer when NACK list is empty as explained in https://github.com/versatica/mediasoup/pull/1076#issuecomment-1537148706.
- Avoid RTT becoming negative in `RtpStreamSend` and `RtpStreamRecv`. If so, assign it with 0.0f.
- And if 0.0f, use `DefaultRtt` (100 ms) in `NackGenerator`.
- Remove useless `RtpStream::hasRtt` and check `RtpStream::rtt > 0.0f` instead.
- Add some brackets in condition blocks.
上级 078ad641
# Changelog # Changelog
### NEXT
* `Worker`: Fix NACK timer and avoid negative RTT ([PR #1082](https://github.com/versatica/mediasoup/pull/1082), thanks to o-u-p for his work in ([PR #1076](https://github.com/versatica/mediasoup/pull/1076)).
### 3.11.25 ### 3.11.25
* `Worker`: Require C++17, Meson >= 1.1.0 and update subprojects ([PR #1081](https://github.com/versatica/mediasoup/pull/1081)). * `Worker`: Require C++17, Meson >= 1.1.0 and update subprojects ([PR #1081](https://github.com/versatica/mediasoup/pull/1081)).
......
...@@ -214,8 +214,7 @@ namespace RTC ...@@ -214,8 +214,7 @@ namespace RTC
uint64_t lastSenderReportNtpMs{ 0u }; uint64_t lastSenderReportNtpMs{ 0u };
// RTP timestamp in last Sender Report. // RTP timestamp in last Sender Report.
uint32_t lastSenderReportTs{ 0u }; uint32_t lastSenderReportTs{ 0u };
float rtt{ 0 }; float rtt{ 0.0f };
bool hasRtt{ false };
// Instance of RtxStream. // Instance of RtxStream.
RTC::RtxStream* rtxStream{ nullptr }; RTC::RtxStream* rtxStream{ nullptr };
......
...@@ -51,14 +51,18 @@ namespace RTC ...@@ -51,14 +51,18 @@ namespace RTC
this->lastSeq = seq; this->lastSeq = seq;
if (isKeyFrame) if (isKeyFrame)
{
this->keyFrameList.insert(seq); this->keyFrameList.insert(seq);
}
return false; return false;
} }
// Obviously never nacked, so ignore. // Obviously never nacked, so ignore.
if (seq == this->lastSeq) if (seq == 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.
...@@ -80,9 +84,13 @@ namespace RTC ...@@ -80,9 +84,13 @@ namespace RTC
this->nackList.erase(it); this->nackList.erase(it);
if (retries != 0) if (retries != 0)
{
return true; return true;
}
else else
{
return false; return false;
}
} }
// Out of order packet or already handled NACKed packet. // Out of order packet or already handled NACKed packet.
...@@ -101,14 +109,18 @@ namespace RTC ...@@ -101,14 +109,18 @@ namespace RTC
// newer than the latest seq seen. // newer than the latest seq seen.
if (isKeyFrame) if (isKeyFrame)
{
this->keyFrameList.insert(seq); this->keyFrameList.insert(seq);
}
// Remove old keyframes. // Remove old keyframes.
{ {
auto it = this->keyFrameList.lower_bound(seq - MaxPacketAge); auto it = this->keyFrameList.lower_bound(seq - MaxPacketAge);
if (it != this->keyFrameList.begin()) if (it != this->keyFrameList.begin())
{
this->keyFrameList.erase(this->keyFrameList.begin(), it); this->keyFrameList.erase(this->keyFrameList.begin(), it);
}
} }
if (isRecovered) if (isRecovered)
...@@ -119,7 +131,9 @@ namespace RTC ...@@ -119,7 +131,9 @@ namespace RTC
auto it = this->recoveredList.lower_bound(seq - MaxPacketAge); auto it = this->recoveredList.lower_bound(seq - MaxPacketAge);
if (it != this->recoveredList.begin()) if (it != this->recoveredList.begin())
{
this->recoveredList.erase(this->recoveredList.begin(), it); this->recoveredList.erase(this->recoveredList.begin(), it);
}
// Do not let a packet pass if it's newer than last seen seq and came via // Do not let a packet pass if it's newer than last seen seq and came via
// RTX. // RTX.
...@@ -134,12 +148,16 @@ namespace RTC ...@@ -134,12 +148,16 @@ namespace RTC
std::vector<uint16_t> nackBatch = GetNackBatch(NackFilter::SEQ); std::vector<uint16_t> nackBatch = GetNackBatch(NackFilter::SEQ);
if (!nackBatch.empty()) if (!nackBatch.empty())
{
this->listener->OnNackGeneratorNackRequired(nackBatch); this->listener->OnNackGeneratorNackRequired(nackBatch);
}
// This is important. Otherwise the running timer (filter:TIME) would be // This is important. Otherwise the running timer (filter:TIME) would be
// interrupted and NACKs would never been sent more than once for each seq. // interrupted and NACKs would never been sent more than once for each seq.
if (!this->timer->IsActive()) if (!this->timer->IsActive())
{
MayRunTimer(); MayRunTimer();
}
return false; return false;
} }
...@@ -187,7 +205,9 @@ namespace RTC ...@@ -187,7 +205,9 @@ namespace RTC
// Do not send NACK for packets that are already recovered by RTX. // Do not send NACK for packets that are already recovered by RTX.
if (this->recoveredList.find(seq) != this->recoveredList.end()) if (this->recoveredList.find(seq) != this->recoveredList.end())
{
continue; continue;
}
this->nackList.emplace(std::make_pair( this->nackList.emplace(std::make_pair(
seq, seq,
...@@ -277,7 +297,10 @@ namespace RTC ...@@ -277,7 +297,10 @@ namespace RTC
continue; continue;
} }
if (filter == NackFilter::TIME && (nackInfo.sentAtMs == 0 || nowMs - nackInfo.sentAtMs >= this->rtt)) if (
filter == NackFilter::TIME &&
(nackInfo.sentAtMs == 0 ||
nowMs - nackInfo.sentAtMs >= (this->rtt > 0u ? this->rtt : DefaultRtt)))
{ {
nackBatch.emplace_back(seq); nackBatch.emplace_back(seq);
nackInfo.retries++; nackInfo.retries++;
...@@ -313,9 +336,13 @@ namespace RTC ...@@ -313,9 +336,13 @@ namespace RTC
seqsStream << nackBatch.back(); seqsStream << nackBatch.back();
if (filter == NackFilter::SEQ) if (filter == NackFilter::SEQ)
{
MS_DEBUG_DEV("[filter:SEQ, asking seqs:%s]", seqsStream.str().c_str()); MS_DEBUG_DEV("[filter:SEQ, asking seqs:%s]", seqsStream.str().c_str());
}
else else
{
MS_DEBUG_DEV("[filter:TIME, asking seqs:%s]", seqsStream.str().c_str()); MS_DEBUG_DEV("[filter:TIME, asking seqs:%s]", seqsStream.str().c_str());
}
} }
#endif #endif
...@@ -329,15 +356,20 @@ namespace RTC ...@@ -329,15 +356,20 @@ namespace RTC
this->nackList.clear(); this->nackList.clear();
this->keyFrameList.clear(); this->keyFrameList.clear();
this->recoveredList.clear(); this->recoveredList.clear();
this->started = false; this->started = false;
this->lastSeq = 0u; this->lastSeq = 0u;
} }
inline void NackGenerator::MayRunTimer() const inline void NackGenerator::MayRunTimer() const
{ {
if (!this->nackList.empty()) if (this->nackList.empty())
{
this->timer->Stop();
}
else
{
this->timer->Start(TimerInterval); this->timer->Start(TimerInterval);
}
} }
inline void NackGenerator::OnTimer(Timer* /*timer*/) inline void NackGenerator::OnTimer(Timer* /*timer*/)
...@@ -347,7 +379,9 @@ namespace RTC ...@@ -347,7 +379,9 @@ namespace RTC
std::vector<uint16_t> nackBatch = GetNackBatch(NackFilter::TIME); std::vector<uint16_t> nackBatch = GetNackBatch(NackFilter::TIME);
if (!nackBatch.empty()) if (!nackBatch.empty())
{
this->listener->OnNackGeneratorNackRequired(nackBatch); this->listener->OnNackGeneratorNackRequired(nackBatch);
}
MayRunTimer(); MayRunTimer();
} }
......
...@@ -76,7 +76,7 @@ namespace RTC ...@@ -76,7 +76,7 @@ namespace RTC
if (this->rtxStream) if (this->rtxStream)
jsonObject["rtxPacketsDiscarded"] = this->rtxStream->GetPacketsDiscarded(); jsonObject["rtxPacketsDiscarded"] = this->rtxStream->GetPacketsDiscarded();
if (this->hasRtt) if (this->rtt > 0.0f)
jsonObject["roundTripTime"] = this->rtt; jsonObject["roundTripTime"] = this->rtt;
} }
......
...@@ -608,9 +608,10 @@ namespace RTC ...@@ -608,9 +608,10 @@ namespace RTC
this->rtt = static_cast<float>(rtt >> 16) * 1000; this->rtt = static_cast<float>(rtt >> 16) * 1000;
this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000; this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000;
if (this->rtt > 0.0f) // Avoid negative RTT value since it doesn't make sense.
if (this->rtt <= 0.0f)
{ {
this->hasRtt = true; this->rtt = 0.0f;
} }
// Tell it to the NackGenerator. // Tell it to the NackGenerator.
......
...@@ -223,9 +223,10 @@ namespace RTC ...@@ -223,9 +223,10 @@ namespace RTC
this->rtt = static_cast<float>(rtt >> 16) * 1000; this->rtt = static_cast<float>(rtt >> 16) * 1000;
this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000; this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000;
if (this->rtt > 0.0f) // Avoid negative RTT value since it doesn't make sense.
if (this->rtt <= 0.0f)
{ {
this->hasRtt = true; this->rtt = 0.0f;
} }
this->packetsLost = report->GetTotalLost(); this->packetsLost = report->GetTotalLost();
...@@ -399,7 +400,7 @@ namespace RTC ...@@ -399,7 +400,7 @@ namespace RTC
// Look for each requested packet. // Look for each requested packet.
const uint64_t nowMs = DepLibUV::GetTimeMs(); const uint64_t nowMs = DepLibUV::GetTimeMs();
const uint16_t rtt = (this->rtt != 0u ? this->rtt : DefaultRtt); const uint16_t rtt = (this->rtt > 0.0f ? this->rtt : DefaultRtt);
uint16_t currentSeq = seq; uint16_t currentSeq = seq;
bool requested{ true }; bool requested{ true };
size_t containerIdx{ 0 }; size_t containerIdx{ 0 };
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论