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

Consumer: Refactor RTP probation

上级 7addb772
...@@ -24,6 +24,10 @@ namespace RTC ...@@ -24,6 +24,10 @@ namespace RTC
{ {
class Consumer : public RTC::RtpMonitor::Listener class Consumer : public RTC::RtpMonitor::Listener
{ {
static constexpr uint16_t RtpPacketsBeforeProbation{ 2000 };
// Must be a power of 2.
static constexpr uint16_t ProbationPacketNumber{ 256 };
public: public:
Consumer( Consumer(
Channel::Notifier* notifier, Channel::Notifier* notifier,
...@@ -69,9 +73,13 @@ namespace RTC ...@@ -69,9 +73,13 @@ namespace RTC
void FillSupportedCodecPayloadTypes(); void FillSupportedCodecPayloadTypes();
void CreateRtpStream(RTC::RtpEncodingParameters& encoding); void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void RetransmitRtpPacket(RTC::RtpPacket* packet); void RetransmitRtpPacket(RTC::RtpPacket* packet);
void RecalculateTargetProfile(); void RecalculateTargetProfile(bool force = false);
void SetEffectiveProfile(RTC::RtpEncodingParameters::Profile profile); void SetEffectiveProfile(RTC::RtpEncodingParameters::Profile profile);
void MayRunProbation(); void MayRunProbation();
bool IsProbing() const;
void StartProbation(RTC::RtpEncodingParameters::Profile profile);
void StopProbation();
void SendProbation(RTC::RtpPacket* packet);
/* Pure virtual methods inherited from RTC::RtpMonitor::Listener. */ /* Pure virtual methods inherited from RTC::RtpMonitor::Listener. */
public: public:
...@@ -118,8 +126,8 @@ namespace RTC ...@@ -118,8 +126,8 @@ namespace RTC
RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile probingProfile{ RTC::RtpEncodingParameters::Profile::NONE }; RTC::RtpEncodingParameters::Profile probingProfile{ RTC::RtpEncodingParameters::Profile::NONE };
// RTP probation. // RTP probation.
bool isProbing{ false }; uint16_t rtpPacketsBeforeProbation{ RtpPacketsBeforeProbation };
uint16_t rtpPacketsBeforeProbation{ 0 }; uint16_t probationPackets{ 0 };
}; };
/* Inline methods. */ /* Inline methods. */
...@@ -174,6 +182,23 @@ namespace RTC ...@@ -174,6 +182,23 @@ namespace RTC
{ {
return this->rtpStream->GetRate(now) + this->retransmittedCounter.GetRate(now); return this->rtpStream->GetRate(now) + this->retransmittedCounter.GetRate(now);
} }
inline bool Consumer::IsProbing() const
{
return this->probationPackets != 0;
}
inline void Consumer::StartProbation(RTC::RtpEncodingParameters::Profile profile)
{
this->probationPackets = ProbationPacketNumber;
this->probingProfile = profile;
}
inline void Consumer::StopProbation()
{
this->probationPackets = 0;
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE;
}
} // namespace RTC } // namespace RTC
#endif #endif
...@@ -15,7 +15,6 @@ namespace RTC ...@@ -15,7 +15,6 @@ namespace RTC
/* Static. */ /* Static. */
static uint8_t RtxPacketBuffer[RtpBufferSize]; static uint8_t RtxPacketBuffer[RtpBufferSize];
static constexpr uint16_t RtpPacketsBeforeProbation{ 2000 };
static std::vector<RTC::RtpPacket*> RtpRetransmissionContainer(18); static std::vector<RTC::RtpPacket*> RtpRetransmissionContainer(18);
...@@ -193,10 +192,9 @@ namespace RTC ...@@ -193,10 +192,9 @@ namespace RTC
this->rtpStream->ClearRetransmissionBuffer(); this->rtpStream->ClearRetransmissionBuffer();
this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation; this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation;
if (this->isProbing) if (IsProbing())
{ {
this->isProbing = false; StopProbation();
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE;
} }
} }
} }
...@@ -241,10 +239,9 @@ namespace RTC ...@@ -241,10 +239,9 @@ namespace RTC
this->rtpStream->ClearRetransmissionBuffer(); this->rtpStream->ClearRetransmissionBuffer();
this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation; this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation;
if (this->isProbing) if (IsProbing())
{ {
this->isProbing = false; StopProbation();
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE;
} }
} }
} }
...@@ -324,11 +321,10 @@ namespace RTC ...@@ -324,11 +321,10 @@ namespace RTC
RTC::RtpEncodingParameters::profile2String[profile].c_str()); RTC::RtpEncodingParameters::profile2String[profile].c_str());
// There is an ongoing probation for this profile, disable it. // There is an ongoing probation for this profile, disable it.
if (this->isProbing && this->probingProfile == profile) if (IsProbing() && this->probingProfile == profile)
{ {
// Disable probation flag. // Stop probation.
this->isProbing = false; StopProbation();
this->probingProfile = RtpEncodingParameters::Profile::NONE;
// Reset the RTP monitor so this probation doesn't affect the current profile. // Reset the RTP monitor so this probation doesn't affect the current profile.
this->rtpMonitor->Reset(); this->rtpMonitor->Reset();
...@@ -358,7 +354,7 @@ namespace RTC ...@@ -358,7 +354,7 @@ namespace RTC
"preferred profile set [profile:%s]", "preferred profile set [profile:%s]",
RTC::RtpEncodingParameters::profile2String[profile].c_str()); RTC::RtpEncodingParameters::profile2String[profile].c_str());
RecalculateTargetProfile(); RecalculateTargetProfile(true /*force*/);
} }
void Consumer::SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile) void Consumer::SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile)
...@@ -418,10 +414,9 @@ namespace RTC ...@@ -418,10 +414,9 @@ namespace RTC
this->lastRtcpSentTime = 0; this->lastRtcpSentTime = 0;
// Reset probation. // Reset probation.
if (this->isProbing) if (IsProbing())
{ {
this->isProbing = false; StopProbation();
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE;
} }
} }
...@@ -566,8 +561,10 @@ namespace RTC ...@@ -566,8 +561,10 @@ namespace RTC
this->transport->SendRtpPacket(packet); this->transport->SendRtpPacket(packet);
// Retransmit the RTP packet if probing. // Retransmit the RTP packet if probing.
if (this->isProbing) if (IsProbing())
RetransmitRtpPacket(packet); {
SendProbation(packet);
}
} }
else else
{ {
...@@ -749,87 +746,14 @@ namespace RTC ...@@ -749,87 +746,14 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (this->isProbing)
{
MS_DEBUG_TAG(
rtp,
"successful probation [ssrc:%" PRIu32 ", profile:%s]",
rtpStream->GetSsrc(),
RTC::RtpEncodingParameters::profile2String[this->probingProfile].c_str());
// Promote probing profile.
this->targetProfile = this->probingProfile;
// Disable probation flag.
this->isProbing = false;
this->probingProfile = RtpEncodingParameters::Profile::NONE;
if (IsEnabled() && !IsPaused())
RequestKeyFrame();
MS_DEBUG_TAG(
rtp,
"target profile set [ssrc:%" PRIu32 ", profile:%s]",
rtpStream->GetSsrc(),
RTC::RtpEncodingParameters::profile2String[this->targetProfile].c_str());
}
else if (this->targetProfile < this->preferredProfile)
{
RecalculateTargetProfile(); RecalculateTargetProfile();
} }
}
void Consumer::OnRtpMonitorUnhealthy() void Consumer::OnRtpMonitorUnhealthy()
{ {
MS_TRACE(); MS_TRACE();
// Probation failed. RecalculateTargetProfile();
if (this->isProbing)
{
MS_DEBUG_TAG(
rtp,
"unsuccessful probation [ssrc:%" PRIu32 ", profile:%s]",
rtpStream->GetSsrc(),
RTC::RtpEncodingParameters::profile2String[this->probingProfile].c_str());
// Disable probation flag.
this->isProbing = false;
this->probingProfile = RtpEncodingParameters::Profile::NONE;
return;
}
MS_DEBUG_TAG(
rtp,
"effective profile unhealthy [ssrc:%" PRIu32 ", profile:%s]",
rtpStream->GetSsrc(),
RTC::RtpEncodingParameters::profile2String[this->effectiveProfile].c_str());
// No simulcast/SVC.
if (this->effectiveProfile == RtpEncodingParameters::Profile::DEFAULT)
return;
auto it = this->mapProfileRtpStream.find(this->effectiveProfile);
// This is already the lowest profile.
if (it == this->mapProfileRtpStream.begin())
return;
// Downgrade the target profile.
this->targetProfile = (std::prev(it))->first;
if (IsEnabled() && !IsPaused())
RequestKeyFrame();
// Reset the RTP monitor to have clear data about the new profile.
this->rtpMonitor->Reset();
MS_DEBUG_TAG(
rtp,
"target profile set [ssrc:%" PRIu32 ", profile:%s]",
rtpStream->GetSsrc(),
RTC::RtpEncodingParameters::profile2String[this->targetProfile].c_str());
} }
void Consumer::MayRunProbation() void Consumer::MayRunProbation()
...@@ -843,7 +767,7 @@ namespace RTC ...@@ -843,7 +767,7 @@ namespace RTC
return; return;
// Ongoing probation. // Ongoing probation.
if (this->isProbing) if (IsProbing())
return; return;
// Current health status is not good. // Current health status is not good.
...@@ -956,13 +880,21 @@ namespace RTC ...@@ -956,13 +880,21 @@ namespace RTC
delete rtxPacket; delete rtxPacket;
} }
void Consumer::RecalculateTargetProfile() void Consumer::RecalculateTargetProfile(bool force)
{ {
MS_TRACE(); MS_TRACE();
RTC::RtpEncodingParameters::Profile newTargetProfile; RTC::RtpEncodingParameters::Profile newTargetProfile;
auto probatedProfile = RTC::RtpEncodingParameters::Profile::NONE;
// If there are no profiles, select none or default, depending on whether this // Probation finished.
if (!IsProbing() && this->probingProfile != RTC::RtpEncodingParameters::Profile::NONE)
{
probatedProfile = this->probingProfile;
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE;
}
// There are no profiles, select none or default, depending on whether this
// is single stream or simulcast/SVC. // is single stream or simulcast/SVC.
if (this->mapProfileRtpStream.empty()) if (this->mapProfileRtpStream.empty())
{ {
...@@ -976,6 +908,31 @@ namespace RTC ...@@ -976,6 +908,31 @@ namespace RTC
else else
newTargetProfile = RtpEncodingParameters::Profile::DEFAULT; newTargetProfile = RtpEncodingParameters::Profile::DEFAULT;
} }
// RTP state is unhealty.
else if (!this->rtpMonitor->IsHealthy())
{
// Ongoing probation, abort.
if (IsProbing())
{
StopProbation();
return;
}
// Probated profile did not succeed.
if (probatedProfile != RtpEncodingParameters::Profile::NONE)
{
return;
}
auto it = this->mapProfileRtpStream.find(this->effectiveProfile);
// This is already the lowest profile.
if (it == this->mapProfileRtpStream.begin())
return;
// Downgrade the target profile.
newTargetProfile = (std::prev(it))->first;
}
// If there is no preferred profile, get the highest one. // If there is no preferred profile, get the highest one.
else if (GetPreferredProfile() == RTC::RtpEncodingParameters::Profile::DEFAULT) else if (GetPreferredProfile() == RTC::RtpEncodingParameters::Profile::DEFAULT)
{ {
...@@ -1012,15 +969,19 @@ namespace RTC ...@@ -1012,15 +969,19 @@ namespace RTC
this->targetProfile = newTargetProfile; this->targetProfile = newTargetProfile;
} }
// Ongoing probation. // Ongoing probation.
else if (this->isProbing) else if (IsProbing())
{ {
// New profile higher or equal than the one being probed. Do not upgrade. // New profile higher or equal than the one being probed. Do not upgrade.
if (newTargetProfile >= this->probingProfile) if (newTargetProfile >= this->probingProfile)
return; return;
// New profile lower than the one begin probed. Stop probation. // New profile lower than the one begin probed. Stop probation.
this->isProbing = false; StopProbation();
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE; this->targetProfile = newTargetProfile;
}
// The profile has just been successfully probated.
else if (probatedProfile == newTargetProfile)
{
this->targetProfile = newTargetProfile; this->targetProfile = newTargetProfile;
} }
// New profile is higher than current target. // New profile is higher than current target.
...@@ -1033,12 +994,15 @@ namespace RTC ...@@ -1033,12 +994,15 @@ namespace RTC
{ {
this->targetProfile = newTargetProfile; this->targetProfile = newTargetProfile;
} }
else if (force)
{
this->targetProfile = newTargetProfile;
}
// Probe it before promotion. // Probe it before promotion.
// TODO: If we are receiving REMB, consider such value and avoid probation. // TODO: If we are receiving REMB, consider such value and avoid probation.
else else
{ {
this->isProbing = true; StartProbation(newTargetProfile);
this->probingProfile = newTargetProfile;
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtp, rtp,
...@@ -1089,4 +1053,35 @@ namespace RTC ...@@ -1089,4 +1053,35 @@ namespace RTC
this->notifier->Emit(this->consumerId, "effectiveprofilechange", eventData); this->notifier->Emit(this->consumerId, "effectiveprofilechange", eventData);
} }
void Consumer::SendProbation(RTC::RtpPacket* packet)
{
if (!IsProbing())
return;
RetransmitRtpPacket(packet);
switch (this->effectiveProfile)
{
case RTC::RtpEncodingParameters::Profile::HIGH:
this->probationPackets--;
break;
case RTC::RtpEncodingParameters::Profile::MEDIUM:
this->probationPackets -= 2;
break;
case RTC::RtpEncodingParameters::Profile::LOW:
this->probationPackets -= 4;
break;
default:
MS_ABORT("invalid profile");
}
if (this->probationPackets == 0)
{
RecalculateTargetProfile();
}
}
} // namespace RTC } // namespace RTC
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论