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

Consumer: add probation

上级 af04dc66
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
namespace RTC namespace RTC
{ {
class Consumer : public RtpStream::Listener class Consumer : public RTC::RtpStream::Listener
{ {
public: public:
Consumer( Consumer(
...@@ -43,7 +43,7 @@ namespace RTC ...@@ -43,7 +43,7 @@ namespace RTC
void Resume(); void Resume();
void SourcePause(); void SourcePause();
void SourceResume(); void SourceResume();
void AddProfile(const RTC::RtpEncodingParameters::Profile profile); void AddProfile(const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStreamInfo* info);
void RemoveProfile(const RTC::RtpEncodingParameters::Profile profile); void RemoveProfile(const RTC::RtpEncodingParameters::Profile profile);
void SourceRtpParametersUpdated(); void SourceRtpParametersUpdated();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile); void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
...@@ -62,8 +62,9 @@ namespace RTC ...@@ -62,8 +62,9 @@ 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 forcePreferred = false);
void SetEffectiveProfile(RTC::RtpEncodingParameters::Profile profile); void SetEffectiveProfile(RTC::RtpEncodingParameters::Profile profile);
void MayRunProbation();
/* Pure virtual methods inherited from RTC::RtpStream::Listener. */ /* Pure virtual methods inherited from RTC::RtpStream::Listener. */
public: public:
...@@ -103,9 +104,14 @@ namespace RTC ...@@ -103,9 +104,14 @@ namespace RTC
// RTP profiles. // RTP profiles.
std::set<RTC::RtpEncodingParameters::Profile> profiles; std::set<RTC::RtpEncodingParameters::Profile> profiles;
std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStreamInfo*> mapProfileRtpStreamInfo;
RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile targetProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; RTC::RtpEncodingParameters::Profile targetProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile probingProfile{ RTC::RtpEncodingParameters::Profile::NONE };
// RTP probation
bool isProbing{ false };
uint16_t rtpPacketsBeforeProbation{ 0 };
}; };
/* Inline methods. */ /* Inline methods. */
......
...@@ -69,7 +69,8 @@ namespace RTC ...@@ -69,7 +69,8 @@ namespace RTC
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet) const; void ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet) const;
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet) const; void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet) const;
void RequestKeyFrame(bool force = false); void RequestKeyFrame(bool force = false);
const std::set<RTC::RtpEncodingParameters::Profile>& GetHealthyProfiles() const; const std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStreamInfo*>& GetHealthyProfiles()
const;
private: private:
void FillHeaderExtensionIds(); void FillHeaderExtensionIds();
...@@ -111,7 +112,7 @@ namespace RTC ...@@ -111,7 +112,7 @@ namespace RTC
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams; std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams; std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
std::map<RTC::RtpStreamRecv*, std::set<RTC::RtpEncodingParameters::Profile>> mapRtpStreamProfiles; std::map<RTC::RtpStreamRecv*, std::set<RTC::RtpEncodingParameters::Profile>> mapRtpStreamProfiles;
std::set<RTC::RtpEncodingParameters::Profile> healthyProfiles; std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStreamInfo*> healthyProfiles;
Timer* keyFrameRequestBlockTimer{ nullptr }; Timer* keyFrameRequestBlockTimer{ nullptr };
// Others. // Others.
std::vector<RtpEncodingParameters> outputEncodings; std::vector<RtpEncodingParameters> outputEncodings;
...@@ -163,7 +164,8 @@ namespace RTC ...@@ -163,7 +164,8 @@ namespace RTC
} }
} }
inline const std::set<RTC::RtpEncodingParameters::Profile>& Producer::GetHealthyProfiles() const inline const std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStreamInfo*>& Producer::
GetHealthyProfiles() const
{ {
return this->healthyProfiles; return this->healthyProfiles;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define MS_RTC_PRODUCER_LISTENER_HPP #define MS_RTC_PRODUCER_LISTENER_HPP
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "RTC/RtpStream.hpp"
namespace RTC namespace RTC
{ {
...@@ -19,7 +20,9 @@ namespace RTC ...@@ -19,7 +20,9 @@ namespace RTC
virtual void OnProducerRtpPacket( virtual void OnProducerRtpPacket(
RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0; RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0;
virtual void OnProducerProfileEnabled( virtual void OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) = 0; RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile,
const RTC::RtpStreamInfo* info) = 0;
virtual void OnProducerProfileDisabled( virtual void OnProducerProfileDisabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) = 0; RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) = 0;
}; };
......
...@@ -72,7 +72,9 @@ namespace RTC ...@@ -72,7 +72,9 @@ namespace RTC
RTC::RtpPacket* packet, RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override; RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileEnabled( void OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override; RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile,
const RTC::RtpStreamInfo* info) override;
void OnProducerProfileDisabled( void OnProducerProfileDisabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override; RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override;
......
...@@ -10,7 +10,18 @@ ...@@ -10,7 +10,18 @@
namespace RTC namespace RTC
{ {
class RtpStream : public Timer::Listener class RtpStreamInfo
{
public:
uint32_t totalLost{ 0 };
uint8_t fractionLost{ 0 };
uint32_t jitter{ 0 };
uint32_t rtt{ 0 };
size_t numPlis{ 0 };
size_t numNacks{ 0 };
};
class RtpStream : public Timer::Listener, public RtpStreamInfo
{ {
public: public:
class Listener class Listener
...@@ -45,6 +56,7 @@ namespace RTC ...@@ -45,6 +56,7 @@ namespace RTC
uint8_t GetPayloadType(); uint8_t GetPayloadType();
const RTC::RtpCodecMimeType& GetMimeType() const; const RTC::RtpCodecMimeType& GetMimeType() const;
bool IsHealthy(); bool IsHealthy();
void ResetHealthCheckTimer();
protected: protected:
bool UpdateSeq(RTC::RtpPacket* packet); bool UpdateSeq(RTC::RtpPacket* packet);
...@@ -76,10 +88,7 @@ namespace RTC ...@@ -76,10 +88,7 @@ namespace RTC
uint64_t maxPacketMs{ 0 }; // When the packet with highest timestammp was seen. uint64_t maxPacketMs{ 0 }; // When the packet with highest timestammp was seen.
Timer* healthCheckTimer{ nullptr }; Timer* healthCheckTimer{ nullptr };
bool healthy{ true }; bool healthy{ true };
uint32_t totalLost{ 0 }; bool notifyHealth{ true };
uint8_t fractionLost{ 0 };
uint32_t jitter{ 0 };
uint32_t rtt{ 0 };
// RTP counters. // RTP counters.
RTC::RtpDataCounter counter; RTC::RtpDataCounter counter;
}; };
......
...@@ -26,7 +26,7 @@ namespace RTC ...@@ -26,7 +26,7 @@ namespace RTC
}; };
public: public:
RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize); RtpStreamSend(RTC::RtpStream::Listener* listener, RTC::RtpStream::Params& params, size_t bufferSize);
~RtpStreamSend() override; ~RtpStreamSend() override;
bool ReceivePacket(RTC::RtpPacket* packet) override; bool ReceivePacket(RTC::RtpPacket* packet) override;
...@@ -47,6 +47,8 @@ namespace RTC ...@@ -47,6 +47,8 @@ namespace RTC
void CheckHealth() override; void CheckHealth() override;
private: private:
// Passed by argument.
RTC::RtpStream::Listener* listener{ nullptr };
std::vector<StorageItem> storage; std::vector<StorageItem> storage;
using Buffer = std::list<BufferItem>; using Buffer = std::list<BufferItem>;
Buffer buffer; Buffer buffer;
......
...@@ -96,7 +96,9 @@ namespace RTC ...@@ -96,7 +96,9 @@ namespace RTC
RTC::RtpPacket* packet, RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override; RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileEnabled( void OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override; RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile,
const RTC::RtpStreamInfo* info) override;
void OnProducerProfileDisabled( void OnProducerProfileDisabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override; RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override;
......
...@@ -14,6 +14,8 @@ namespace RTC ...@@ -14,6 +14,8 @@ namespace RTC
{ {
/* Static. */ /* Static. */
static constexpr uint16_t RtpPacketsBeforeProbation{ 2000 };
static std::vector<RTC::RtpPacket*> RtpRetransmissionContainer(18); static std::vector<RTC::RtpPacket*> RtpRetransmissionContainer(18);
/* Instance methods. */ /* Instance methods. */
...@@ -133,7 +135,10 @@ namespace RTC ...@@ -133,7 +135,10 @@ namespace RTC
MS_DEBUG_DEV("Consumer paused [consumerId:%" PRIu32 "]", this->consumerId); MS_DEBUG_DEV("Consumer paused [consumerId:%" PRIu32 "]", this->consumerId);
if (IsEnabled() && !this->sourcePaused) if (IsEnabled() && !this->sourcePaused)
{
this->rtpStream->ClearRetransmissionBuffer(); this->rtpStream->ClearRetransmissionBuffer();
this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation;
}
} }
void Consumer::Resume() void Consumer::Resume()
...@@ -165,7 +170,10 @@ namespace RTC ...@@ -165,7 +170,10 @@ namespace RTC
this->notifier->Emit(this->consumerId, "sourcepaused"); this->notifier->Emit(this->consumerId, "sourcepaused");
if (IsEnabled() && !this->paused) if (IsEnabled() && !this->paused)
{
this->rtpStream->ClearRetransmissionBuffer(); this->rtpStream->ClearRetransmissionBuffer();
this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation;
}
} }
void Consumer::SourceResume() void Consumer::SourceResume()
...@@ -195,7 +203,8 @@ namespace RTC ...@@ -195,7 +203,8 @@ namespace RTC
this->syncRequired = true; this->syncRequired = true;
} }
void Consumer::AddProfile(const RTC::RtpEncodingParameters::Profile profile) void Consumer::AddProfile(
const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStreamInfo* info)
{ {
MS_ASSERT( MS_ASSERT(
profile != RTC::RtpEncodingParameters::Profile::NONE && profile != RTC::RtpEncodingParameters::Profile::NONE &&
...@@ -204,12 +213,14 @@ namespace RTC ...@@ -204,12 +213,14 @@ namespace RTC
MS_ASSERT(this->profiles.find(profile) == this->profiles.end(), "profile already exists"); MS_ASSERT(this->profiles.find(profile) == this->profiles.end(), "profile already exists");
// Insert profile. // Insert the new profile.
this->mapProfileRtpStreamInfo[profile] = info;
this->profiles.insert(profile); this->profiles.insert(profile);
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtp, "profile added [profile:%s]", RTC::RtpEncodingParameters::profile2String[profile].c_str()); rtp, "profile added [profile:%s]", RTC::RtpEncodingParameters::profile2String[profile].c_str());
if (profile > this->targetProfile)
RecalculateTargetProfile(); RecalculateTargetProfile();
} }
...@@ -222,20 +233,54 @@ namespace RTC ...@@ -222,20 +233,54 @@ namespace RTC
MS_ASSERT(this->profiles.find(profile) != this->profiles.end(), "profile not found"); MS_ASSERT(this->profiles.find(profile) != this->profiles.end(), "profile not found");
// Remove profile. // Remove the profile.
this->mapProfileRtpStreamInfo.erase(profile);
this->profiles.erase(profile); this->profiles.erase(profile);
// If it was our effective profile, set it to none.
if (this->effectiveProfile == profile)
SetEffectiveProfile(RtpEncodingParameters::Profile::NONE);
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtp, rtp,
"profile removed [profile:%s]", "profile removed [profile:%s]",
RTC::RtpEncodingParameters::profile2String[profile].c_str()); RTC::RtpEncodingParameters::profile2String[profile].c_str());
if (this->profiles.empty())
{
RecalculateTargetProfile(); RecalculateTargetProfile();
} }
// If there is an ongoing probation for this profile, disable it.
else if (this->isProbing && this->probingProfile == profile)
{
// Disable probation flag.
this->isProbing = false;
this->probingProfile = RtpEncodingParameters::Profile::NONE;
// Reset the health check timer so this probation doesn't affect the effective profile.
this->rtpStream->ResetHealthCheckTimer();
return;
}
// If it is the effective profile, try to downgrade, or upgrade it to the next higher profile if the removed profile was lower than other existing ones.
else if (this->effectiveProfile == profile)
{
SetEffectiveProfile(RtpEncodingParameters::Profile::NONE);
auto it = this->profiles.begin();
auto newTargetProfile = *it;
while (++it != this->profiles.end())
{
auto candidateTargetProfile = *it;
if (candidateTargetProfile < profile)
newTargetProfile = candidateTargetProfile;
else
break;
}
this->targetProfile = newTargetProfile;
return;
}
}
void Consumer::SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile) void Consumer::SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile)
{ {
...@@ -251,7 +296,7 @@ namespace RTC ...@@ -251,7 +296,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 /*forcePreferred*/);
} }
/** /**
...@@ -401,6 +446,20 @@ namespace RTC ...@@ -401,6 +446,20 @@ namespace RTC
{ {
// Send the packet. // Send the packet.
this->transport->SendRtpPacket(packet); this->transport->SendRtpPacket(packet);
// Send a fake RTP packet if probing.
if (this->isProbing)
{
packet->SetSsrc(0xFFFF);
// TODO: Remove log.
MS_DEBUG_TAG(rtp, " probation..., sending fake rtp packet");
// Send the packet.
this->transport->SendRtpPacket(packet);
// TODO: Update transmitted RTP data counter.
}
} }
else else
{ {
...@@ -428,6 +487,13 @@ namespace RTC ...@@ -428,6 +487,13 @@ namespace RTC
// Restore the original payload if needed. // Restore the original payload if needed.
if (this->encodingContext) if (this->encodingContext)
packet->RestorePayload(); packet->RestorePayload();
// Run probation if needed.
if (this->kind == RTC::Media::Kind::VIDEO && --this->rtpPacketsBeforeProbation == 0)
{
this->rtpPacketsBeforeProbation = RtpPacketsBeforeProbation;
MayRunProbation();
}
} }
void Consumer::GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) void Consumer::GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now)
...@@ -512,19 +578,119 @@ namespace RTC ...@@ -512,19 +578,119 @@ namespace RTC
} }
} }
void Consumer::OnRtpStreamDied(RtpStream* /*stream*/) void Consumer::OnRtpStreamDied(RtpStream* stream)
{ {
MS_TRACE(); MS_TRACE();
OnRtpStreamUnhealthy(stream);
} }
void Consumer::OnRtpStreamHealthy(RtpStream* /*stream*/) void Consumer::OnRtpStreamHealthy(RtpStream* /*stream*/)
{ {
MS_TRACE(); MS_TRACE();
if (!this->isProbing)
return;
MS_DEBUG_TAG(
rtp,
"successful probation [profile:%s]",
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 [profile:%s]",
RTC::RtpEncodingParameters::profile2String[this->targetProfile].c_str());
} }
void Consumer::OnRtpStreamUnhealthy(RtpStream* /*stream*/) void Consumer::OnRtpStreamUnhealthy(RtpStream* /*stream*/)
{ {
MS_TRACE(); MS_TRACE();
// Probation failed.
if (this->isProbing)
{
MS_DEBUG_TAG(
rtp,
"unsuccessful probation [profile:%s]",
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 [profile:%s]",
RTC::RtpEncodingParameters::profile2String[this->effectiveProfile].c_str());
// No simulcast/SVC.
if (this->effectiveProfile == RtpEncodingParameters::Profile::DEFAULT)
{
// TODO: Notify the user.
MS_WARN_TAG(rtp, "unhealthy rtp stream");
return;
}
auto it = this->profiles.find(this->effectiveProfile);
// This is already the lowest profile.
if (it == this->profiles.begin())
{
// TODO: Notify the user.
MS_WARN_TAG(rtp, "no healthy profile to serve");
return;
}
// Downgrade the target profile.
this->targetProfile = *(std::prev(it));
if (IsEnabled() && !IsPaused())
RequestKeyFrame();
// We want to be notified about this new profile's health.
this->rtpStream->ResetHealthCheckTimer();
MS_DEBUG_TAG(
rtp,
"target profile set [profile:%s]",
RTC::RtpEncodingParameters::profile2String[this->targetProfile].c_str());
}
void Consumer::MayRunProbation()
{
// No simulcast or SVC.
if (this->profiles.empty())
return;
// There is an ongoing profile upgrade. Do not interfere.
if (this->effectiveProfile != this->targetProfile)
return;
// Ongoing probation.
if (this->isProbing)
return;
// Current health status is not good.
if (!this->rtpStream->IsHealthy())
return;
RecalculateTargetProfile();
} }
void Consumer::FillSupportedCodecPayloadTypes() void Consumer::FillSupportedCodecPayloadTypes()
...@@ -575,9 +741,9 @@ namespace RTC ...@@ -575,9 +741,9 @@ namespace RTC
// Create a RtpStreamSend for sending a single media stream. // Create a RtpStreamSend for sending a single media stream.
if (useNack) if (useNack)
this->rtpStream = new RTC::RtpStreamSend(params, 1500); this->rtpStream = new RTC::RtpStreamSend(this, params, 1500);
else else
this->rtpStream = new RTC::RtpStreamSend(params, 0); this->rtpStream = new RTC::RtpStreamSend(this, params, 0);
if (encoding.hasRtx && encoding.rtx.ssrc != 0u) if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
{ {
...@@ -632,7 +798,7 @@ namespace RTC ...@@ -632,7 +798,7 @@ namespace RTC
delete rtxPacket; delete rtxPacket;
} }
void Consumer::RecalculateTargetProfile() void Consumer::RecalculateTargetProfile(bool forcePreferred)
{ {
MS_TRACE(); MS_TRACE();
...@@ -652,32 +818,89 @@ namespace RTC ...@@ -652,32 +818,89 @@ namespace RTC
else else
newTargetProfile = RtpEncodingParameters::Profile::DEFAULT; newTargetProfile = RtpEncodingParameters::Profile::DEFAULT;
} }
// If there is no preferred profile, take the best one available. // If there is no preferred profile, try the next higher profile.
else if (this->preferredProfile == RTC::RtpEncodingParameters::Profile::DEFAULT) else if (this->preferredProfile == RTC::RtpEncodingParameters::Profile::DEFAULT)
{ {
auto it = this->profiles.crbegin(); newTargetProfile = this->effectiveProfile;
auto it = this->profiles.upper_bound(this->effectiveProfile);
if (it != this->profiles.end())
newTargetProfile = *it; newTargetProfile = *it;
} }
// Otherwise take the highest available profile equal or lower than the preferred. // If the preferred profile is forced, try with the closest profile to it.
else else if (forcePreferred)
{ {
std::set<RtpEncodingParameters::Profile>::reverse_iterator it; auto it = this->profiles.crbegin();
newTargetProfile = *it;
newTargetProfile = RtpEncodingParameters::Profile::NONE;
for (it = this->profiles.rbegin(); it != this->profiles.rend(); ++it) for (; it != this->profiles.crend(); ++it)
{ {
auto profile = *it; auto profile = *it;
if (profile <= this->preferredProfile) if (profile <= this->preferredProfile)
{ {
newTargetProfile = *it; newTargetProfile = profile;
break; break;
} }
} }
} }
// Try with the next higher profile which is lower or equal to the preferred.
else
{
newTargetProfile = this->effectiveProfile;
auto it = this->profiles.upper_bound(this->effectiveProfile);
if (it != this->profiles.end() && *it <= this->preferredProfile)
newTargetProfile = *it;
}
// Ongoing probation.
if (this->isProbing)
{
// New profile higher or equal than the one being probed. Do not upgrade.
if (newTargetProfile >= this->probingProfile)
{
return;
}
// New profile lower than the one begin probed. Stop probation.
else
{
this->isProbing = false;
this->probingProfile = RTC::RtpEncodingParameters::Profile::NONE;
this->targetProfile = newTargetProfile; this->targetProfile = newTargetProfile;
}
}
// New profile is higher than current target.
else if (newTargetProfile > this->targetProfile)
{
// No specific profile is being sent.
if (
this->targetProfile == RTC::RtpEncodingParameters::Profile::NONE ||
this->targetProfile == RTC::RtpEncodingParameters::Profile::DEFAULT)
{
this->targetProfile = newTargetProfile;
}
// Probe it before promotion.
// TODO: If we are receiving REMB, consider such value and avoid probation.
else
{
this->isProbing = true;
this->probingProfile = newTargetProfile;
this->rtpStream->ResetHealthCheckTimer();
MS_DEBUG_TAG(
rtp,
"probing profile [%s]",
RTC::RtpEncodingParameters::profile2String[this->probingProfile].c_str());
return;
}
}
// New profile is lower than the targe profile.
else
{
this->targetProfile = newTargetProfile;
}
if (this->targetProfile == this->effectiveProfile) if (this->targetProfile == this->effectiveProfile)
return; return;
......
...@@ -636,14 +636,16 @@ namespace RTC ...@@ -636,14 +636,16 @@ namespace RTC
if (profile == RTC::RtpEncodingParameters::Profile::DEFAULT) if (profile == RTC::RtpEncodingParameters::Profile::DEFAULT)
break; break;
MS_ASSERT(
this->healthyProfiles.find(profile) == this->healthyProfiles.end(),
"profile already in headltyProfiles set");
// Add the profile to the healthy profiles set. // Add the profile to the healthy profiles set.
const auto& result = this->healthyProfiles.insert(profile); this->healthyProfiles[profile] = rtpStream;
bool profileExisted = !result.second;
MS_ASSERT(!profileExisted, "profile already in headltyProfiles set");
for (auto& listener : this->listeners) for (auto& listener : this->listeners)
{ {
listener->OnProducerProfileEnabled(this, profile); listener->OnProducerProfileEnabled(this, profile, rtpStream);
} }
} }
} }
...@@ -659,10 +661,12 @@ namespace RTC ...@@ -659,10 +661,12 @@ namespace RTC
if (profile == RTC::RtpEncodingParameters::Profile::DEFAULT) if (profile == RTC::RtpEncodingParameters::Profile::DEFAULT)
break; break;
MS_ASSERT(
this->healthyProfiles.find(profile) != this->healthyProfiles.end(),
"profile already in headltyProfiles set");
// Remove the profile from the healthy profiles set. // Remove the profile from the healthy profiles set.
auto result = this->healthyProfiles.erase(profile); this->healthyProfiles.erase(profile);
bool profileExisted = (result == 1);
MS_ASSERT(profileExisted, "profile not present in headltyProfiles set");
for (auto& listener : this->listeners) for (auto& listener : this->listeners)
{ {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include "RTC/WebRtcTransport.hpp" #include "RTC/WebRtcTransport.hpp"
#include <cmath> // std::lround() #include <cmath> // std::lround()
#include <map>
#include <set> #include <set>
#include <string> #include <string>
...@@ -511,14 +512,15 @@ namespace RTC ...@@ -511,14 +512,15 @@ namespace RTC
// Add us as listener. // Add us as listener.
consumer->AddListener(this); consumer->AddListener(this);
auto profiles = producer->GetHealthyProfiles(); auto healthyProfiles = producer->GetHealthyProfiles();
std::set<RtpEncodingParameters::Profile>::reverse_iterator it; std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStreamInfo*>::reverse_iterator it;
for (it = profiles.rbegin(); it != profiles.rend(); ++it) for (it = healthyProfiles.rbegin(); it != healthyProfiles.rend(); ++it)
{ {
auto profile = *it; auto profile = it->first;
auto info = it->second;
consumer->AddProfile(profile); consumer->AddProfile(profile, info);
} }
// Insert into the maps. // Insert into the maps.
...@@ -1530,7 +1532,7 @@ namespace RTC ...@@ -1530,7 +1532,7 @@ namespace RTC
} }
void Router::OnProducerProfileEnabled( void Router::OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStreamInfo* info)
{ {
MS_TRACE(); MS_TRACE();
...@@ -1542,7 +1544,7 @@ namespace RTC ...@@ -1542,7 +1544,7 @@ namespace RTC
for (auto* consumer : consumers) for (auto* consumer : consumers)
{ {
consumer->AddProfile(profile); consumer->AddProfile(profile, info);
} }
} }
......
...@@ -49,6 +49,7 @@ namespace RTC ...@@ -49,6 +49,7 @@ namespace RTC
static const Json::StaticString JsonStringRtt{ "rtt" }; static const Json::StaticString JsonStringRtt{ "rtt" };
static const Json::StaticString JsonStringFractionLost{ "fractionLost" }; static const Json::StaticString JsonStringFractionLost{ "fractionLost" };
static const Json::StaticString JsonStringTotalLost{ "totalLost" }; static const Json::StaticString JsonStringTotalLost{ "totalLost" };
static const Json::StaticString JsonStringHealthy{ "healthy" };
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
...@@ -62,6 +63,7 @@ namespace RTC ...@@ -62,6 +63,7 @@ namespace RTC
json[JsonStringRtt] = Json::UInt{ this->rtt }; json[JsonStringRtt] = Json::UInt{ this->rtt };
json[JsonStringFractionLost] = Json::UInt{ this->fractionLost }; json[JsonStringFractionLost] = Json::UInt{ this->fractionLost };
json[JsonStringTotalLost] = Json::UInt{ this->totalLost }; json[JsonStringTotalLost] = Json::UInt{ this->totalLost };
json[JsonStringHealthy] = this->healthy ? "true" : "false";
return json; return json;
} }
...@@ -115,6 +117,13 @@ namespace RTC ...@@ -115,6 +117,13 @@ namespace RTC
return this->counter.GetRate(now); return this->counter.GetRate(now);
} }
void RtpStream::ResetHealthCheckTimer()
{
// Notify about next health status.
this->notifyHealth = true;
this->healthCheckTimer->Start(HealthCheckPeriod, HealthCheckPeriod);
}
void RtpStream::InitSeq(uint16_t seq) void RtpStream::InitSeq(uint16_t seq)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -15,11 +15,13 @@ namespace RTC ...@@ -15,11 +15,13 @@ namespace RTC
// TODO: This must be tunned. // TODO: This must be tunned.
static constexpr uint32_t MaxRetransmissionDelay{ 1000 }; static constexpr uint32_t MaxRetransmissionDelay{ 1000 };
static constexpr uint32_t DefaultRtt{ 100 }; static constexpr uint32_t DefaultRtt{ 100 };
static constexpr uint8_t MaxHealthLossPercentage{ 20 };
/* Instance methods. */ /* Instance methods. */
RtpStreamSend::RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize) RtpStreamSend::RtpStreamSend(
: RtpStream::RtpStream(params), storage(bufferSize) RTC::RtpStream::Listener* listener, RTC::RtpStream::Params& params, size_t bufferSize)
: RtpStream::RtpStream(params), listener(listener), storage(bufferSize)
{ {
MS_TRACE(); MS_TRACE();
} }
...@@ -416,6 +418,25 @@ namespace RTC ...@@ -416,6 +418,25 @@ namespace RTC
void RtpStreamSend::CheckHealth() void RtpStreamSend::CheckHealth()
{ {
MS_TRACE(); MS_TRACE();
uint16_t currentLossPercentage = this->fractionLost * 100 / 256;
if (currentLossPercentage >= MaxHealthLossPercentage)
{
MS_DEBUG_TAG(rtp, "last period loss percentage: %" PRIu16, currentLossPercentage);
if (this->notifyHealth || this->healthy)
{
this->healthy = false;
this->listener->OnRtpStreamUnhealthy(this);
}
}
else if (this->notifyHealth || !this->healthy)
{
this->healthy = true;
this->listener->OnRtpStreamHealthy(this);
}
this->notifyHealth = false;
} }
void RtpStreamSend::SetRtx(uint8_t payloadType, uint32_t ssrc) void RtpStreamSend::SetRtx(uint8_t payloadType, uint32_t ssrc)
......
...@@ -542,7 +542,9 @@ namespace RTC ...@@ -542,7 +542,9 @@ namespace RTC
} }
void Transport::OnProducerProfileEnabled( void Transport::OnProducerProfileEnabled(
RTC::Producer* /*producer*/, RTC::RtpEncodingParameters::Profile /*profile*/) RTC::Producer* /*producer*/,
RTC::RtpEncodingParameters::Profile /*profile*/,
const RTC::RtpStreamInfo* /*info*/)
{ {
// Do nothing. // Do nothing.
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论