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

Consumer: add probation

上级 af04dc66
......@@ -21,7 +21,7 @@
namespace RTC
{
class Consumer : public RtpStream::Listener
class Consumer : public RTC::RtpStream::Listener
{
public:
Consumer(
......@@ -43,7 +43,7 @@ namespace RTC
void Resume();
void SourcePause();
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 SourceRtpParametersUpdated();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
......@@ -62,8 +62,9 @@ namespace RTC
void FillSupportedCodecPayloadTypes();
void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void RetransmitRtpPacket(RTC::RtpPacket* packet);
void RecalculateTargetProfile();
void RecalculateTargetProfile(bool forcePreferred = false);
void SetEffectiveProfile(RTC::RtpEncodingParameters::Profile profile);
void MayRunProbation();
/* Pure virtual methods inherited from RTC::RtpStream::Listener. */
public:
......@@ -103,9 +104,14 @@ namespace RTC
// RTP 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 targetProfile{ 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. */
......
......@@ -69,7 +69,8 @@ namespace RTC
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet) const;
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet) const;
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:
void FillHeaderExtensionIds();
......@@ -111,7 +112,7 @@ namespace RTC
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
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 };
// Others.
std::vector<RtpEncodingParameters> outputEncodings;
......@@ -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;
}
......
......@@ -2,6 +2,7 @@
#define MS_RTC_PRODUCER_LISTENER_HPP
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpStream.hpp"
namespace RTC
{
......@@ -19,7 +20,9 @@ namespace RTC
virtual void OnProducerRtpPacket(
RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0;
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(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) = 0;
};
......
......@@ -72,7 +72,9 @@ namespace RTC
RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override;
RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile,
const RTC::RtpStreamInfo* info) override;
void OnProducerProfileDisabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override;
......
......@@ -10,7 +10,18 @@
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:
class Listener
......@@ -45,6 +56,7 @@ namespace RTC
uint8_t GetPayloadType();
const RTC::RtpCodecMimeType& GetMimeType() const;
bool IsHealthy();
void ResetHealthCheckTimer();
protected:
bool UpdateSeq(RTC::RtpPacket* packet);
......@@ -76,10 +88,7 @@ namespace RTC
uint64_t maxPacketMs{ 0 }; // When the packet with highest timestammp was seen.
Timer* healthCheckTimer{ nullptr };
bool healthy{ true };
uint32_t totalLost{ 0 };
uint8_t fractionLost{ 0 };
uint32_t jitter{ 0 };
uint32_t rtt{ 0 };
bool notifyHealth{ true };
// RTP counters.
RTC::RtpDataCounter counter;
};
......
......@@ -26,7 +26,7 @@ namespace RTC
};
public:
RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize);
RtpStreamSend(RTC::RtpStream::Listener* listener, RTC::RtpStream::Params& params, size_t bufferSize);
~RtpStreamSend() override;
bool ReceivePacket(RTC::RtpPacket* packet) override;
......@@ -47,6 +47,8 @@ namespace RTC
void CheckHealth() override;
private:
// Passed by argument.
RTC::RtpStream::Listener* listener{ nullptr };
std::vector<StorageItem> storage;
using Buffer = std::list<BufferItem>;
Buffer buffer;
......
......@@ -96,7 +96,9 @@ namespace RTC
RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override;
RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile,
const RTC::RtpStreamInfo* info) override;
void OnProducerProfileDisabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) override;
......
......@@ -636,14 +636,16 @@ namespace RTC
if (profile == RTC::RtpEncodingParameters::Profile::DEFAULT)
break;
MS_ASSERT(
this->healthyProfiles.find(profile) == this->healthyProfiles.end(),
"profile already in headltyProfiles set");
// Add the profile to the healthy profiles set.
const auto& result = this->healthyProfiles.insert(profile);
bool profileExisted = !result.second;
MS_ASSERT(!profileExisted, "profile already in headltyProfiles set");
this->healthyProfiles[profile] = rtpStream;
for (auto& listener : this->listeners)
{
listener->OnProducerProfileEnabled(this, profile);
listener->OnProducerProfileEnabled(this, profile, rtpStream);
}
}
}
......@@ -659,10 +661,12 @@ namespace RTC
if (profile == RTC::RtpEncodingParameters::Profile::DEFAULT)
break;
MS_ASSERT(
this->healthyProfiles.find(profile) != this->healthyProfiles.end(),
"profile already in headltyProfiles set");
// Remove the profile from the healthy profiles set.
auto result = this->healthyProfiles.erase(profile);
bool profileExisted = (result == 1);
MS_ASSERT(profileExisted, "profile not present in headltyProfiles set");
this->healthyProfiles.erase(profile);
for (auto& listener : this->listeners)
{
......
......@@ -9,6 +9,7 @@
#include "RTC/RtpDictionaries.hpp"
#include "RTC/WebRtcTransport.hpp"
#include <cmath> // std::lround()
#include <map>
#include <set>
#include <string>
......@@ -511,14 +512,15 @@ namespace RTC
// Add us as listener.
consumer->AddListener(this);
auto profiles = producer->GetHealthyProfiles();
std::set<RtpEncodingParameters::Profile>::reverse_iterator it;
auto healthyProfiles = producer->GetHealthyProfiles();
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.
......@@ -1530,7 +1532,7 @@ namespace RTC
}
void Router::OnProducerProfileEnabled(
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile)
RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStreamInfo* info)
{
MS_TRACE();
......@@ -1542,7 +1544,7 @@ namespace RTC
for (auto* consumer : consumers)
{
consumer->AddProfile(profile);
consumer->AddProfile(profile, info);
}
}
......
......@@ -49,6 +49,7 @@ namespace RTC
static const Json::StaticString JsonStringRtt{ "rtt" };
static const Json::StaticString JsonStringFractionLost{ "fractionLost" };
static const Json::StaticString JsonStringTotalLost{ "totalLost" };
static const Json::StaticString JsonStringHealthy{ "healthy" };
Json::Value json(Json::objectValue);
......@@ -62,6 +63,7 @@ namespace RTC
json[JsonStringRtt] = Json::UInt{ this->rtt };
json[JsonStringFractionLost] = Json::UInt{ this->fractionLost };
json[JsonStringTotalLost] = Json::UInt{ this->totalLost };
json[JsonStringHealthy] = this->healthy ? "true" : "false";
return json;
}
......@@ -115,6 +117,13 @@ namespace RTC
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)
{
MS_TRACE();
......
......@@ -15,11 +15,13 @@ namespace RTC
// TODO: This must be tunned.
static constexpr uint32_t MaxRetransmissionDelay{ 1000 };
static constexpr uint32_t DefaultRtt{ 100 };
static constexpr uint8_t MaxHealthLossPercentage{ 20 };
/* Instance methods. */
RtpStreamSend::RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize)
: RtpStream::RtpStream(params), storage(bufferSize)
RtpStreamSend::RtpStreamSend(
RTC::RtpStream::Listener* listener, RTC::RtpStream::Params& params, size_t bufferSize)
: RtpStream::RtpStream(params), listener(listener), storage(bufferSize)
{
MS_TRACE();
}
......@@ -416,6 +418,25 @@ namespace RTC
void RtpStreamSend::CheckHealth()
{
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)
......
......@@ -542,7 +542,9 @@ namespace RTC
}
void Transport::OnProducerProfileEnabled(
RTC::Producer* /*producer*/, RTC::RtpEncodingParameters::Profile /*profile*/)
RTC::Producer* /*producer*/,
RTC::RtpEncodingParameters::Profile /*profile*/,
const RTC::RtpStreamInfo* /*info*/)
{
// Do nothing.
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论