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

Propagate profile activation/deactivation events.

上级 3ff70302
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "RTC/Transport.hpp" #include "RTC/Transport.hpp"
#include <json/json.h> #include <json/json.h>
#include <unordered_set> #include <unordered_set>
#include <set>
namespace RTC namespace RTC
{ {
...@@ -42,6 +43,8 @@ namespace RTC ...@@ -42,6 +43,8 @@ namespace RTC
void Resume(); void Resume();
void SourcePause(); void SourcePause();
void SourceResume(); void SourceResume();
void AddProfile(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);
void Disable(); void Disable();
...@@ -59,6 +62,7 @@ namespace RTC ...@@ -59,6 +62,7 @@ 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 RecalculateEffectiveProfile();
/* Pure virtual methods inherited from RTC::RtpStream::Listener. */ /* Pure virtual methods inherited from RTC::RtpStream::Listener. */
public: public:
...@@ -95,8 +99,9 @@ namespace RTC ...@@ -95,8 +99,9 @@ namespace RTC
uint32_t lastRecvRtpTimestamp{ 0 }; uint32_t lastRecvRtpTimestamp{ 0 };
bool syncRequired{ true }; bool syncRequired{ true };
// RTP profiles. // RTP profiles.
RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; std::set<RTC::RtpEncodingParameters::Profile> profiles;
RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::NONE };
RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::NONE };
}; };
/* Inline methods. */ /* Inline methods. */
......
...@@ -84,13 +84,14 @@ namespace RTC ...@@ -84,13 +84,14 @@ 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 RequestFullFrame(bool force = false); void RequestFullFrame(bool force = false);
const std::set<RTC::RtpEncodingParameters::Profile> GetProfiles() const;
private: private:
void FillHeaderExtensionIds(); void FillHeaderExtensionIds();
void CreateRtpStream(RTC::RtpEncodingParameters& encoding); void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void ClearRtpStreams(); void ClearRtpStreams();
void ApplyRtpMapping(RTC::RtpPacket* packet) const; void ApplyRtpMapping(RTC::RtpPacket* packet) const;
RTC::RtpEncodingParameters::Profile GetRtpProfile(RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet); RTC::RtpEncodingParameters::Profile GetProfile(RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet);
/* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */ /* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */
public: public:
...@@ -165,6 +166,23 @@ namespace RTC ...@@ -165,6 +166,23 @@ namespace RTC
rtpStream->ReceiveRtcpSenderReport(report); rtpStream->ReceiveRtcpSenderReport(report);
} }
} }
inline const std::set<RTC::RtpEncodingParameters::Profile> Producer::GetProfiles() const
{
std::set<RTC::RtpEncodingParameters::Profile> profiles;
for (const auto& it : this->rtpProfiles)
{
for (const auto& it2 : it.second)
{
auto profile = it2;
profiles.insert(profile);
}
}
return profiles;
}
} // namespace RTC } // namespace RTC
#endif #endif
...@@ -18,6 +18,8 @@ namespace RTC ...@@ -18,6 +18,8 @@ namespace RTC
virtual void OnProducerResumed(RTC::Producer* producer) = 0; virtual void OnProducerResumed(RTC::Producer* producer) = 0;
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(RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) = 0;
virtual void OnProducerProfileDisabled(RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile) = 0;
}; };
} // namespace RTC } // namespace RTC
......
...@@ -71,6 +71,12 @@ namespace RTC ...@@ -71,6 +71,12 @@ namespace RTC
RTC::Producer* producer, RTC::Producer* producer,
RTC::RtpPacket* packet, RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override; RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileEnabled(
RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileDisabled(
RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile) override;
/* Pure virtual methods inherited from RTC::ConsumerListener. */ /* Pure virtual methods inherited from RTC::ConsumerListener. */
public: public:
......
...@@ -209,7 +209,7 @@ namespace RTC ...@@ -209,7 +209,7 @@ namespace RTC
public: public:
enum class Profile : uint8_t enum class Profile : uint8_t
{ {
DEFAULT = 0, NONE = 0,
LOW, LOW,
MEDIUM, MEDIUM,
HIGH HIGH
...@@ -239,7 +239,7 @@ namespace RTC ...@@ -239,7 +239,7 @@ namespace RTC
bool active{ true }; bool active{ true };
std::string encodingId; std::string encodingId;
std::vector<std::string> dependencyEncodingIds; std::vector<std::string> dependencyEncodingIds;
Profile profile{ Profile::DEFAULT }; Profile profile{ Profile::NONE };
}; };
class RtpHeaderExtensionParameters class RtpHeaderExtensionParameters
......
...@@ -157,6 +157,12 @@ namespace RTC ...@@ -157,6 +157,12 @@ namespace RTC
RTC::Producer* producer, RTC::Producer* producer,
RTC::RtpPacket* packet, RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override; RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileEnabled(
RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile) override;
void OnProducerProfileDisabled(
RTC::Producer* producer,
RTC::RtpEncodingParameters::Profile profile) override;
/* Pure virtual methods inherited from RTC::ConsumerListener. */ /* Pure virtual methods inherited from RTC::ConsumerListener. */
public: public:
......
...@@ -224,6 +224,31 @@ namespace RTC ...@@ -224,6 +224,31 @@ namespace RTC
this->rtpStream->ClearRetransmissionBuffer(); this->rtpStream->ClearRetransmissionBuffer();
} }
void Consumer::AddProfile(const RTC::RtpEncodingParameters::Profile profile)
{
// If this is the first enabled profile, remove the NONE entry from the set.
if (this->profiles.size() == 1 &&
(*(this->profiles.begin()) == RTC::RtpEncodingParameters::Profile::NONE))
this->profiles.clear();
// Insert profile.
this->profiles.insert(profile);
MS_DEBUG_TAG(rtp, "profile added: %s", RTC::RtpEncodingParameters::profile2String[profile].c_str());;
RecalculateEffectiveProfile();
}
void Consumer::RemoveProfile(const RTC::RtpEncodingParameters::Profile profile)
{
// Remove profile.
this->profiles.erase(profile);
MS_DEBUG_TAG(rtp, "profile removed: %s", RTC::RtpEncodingParameters::profile2String[this->effectiveProfile].c_str());;
RecalculateEffectiveProfile();
}
void Consumer::SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile) void Consumer::SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile)
{ {
MS_TRACE(); MS_TRACE();
...@@ -233,16 +258,7 @@ namespace RTC ...@@ -233,16 +258,7 @@ namespace RTC
this->preferredProfile = profile; this->preferredProfile = profile;
// TODO: Set it dynamically. RecalculateEffectiveProfile();
this->effectiveProfile = profile;
if (IsEnabled() && !IsPaused())
{
this->syncRequired = true;
this->rtpStream->ClearRetransmissionBuffer();
RequestFullFrame();
}
} }
/** /**
...@@ -567,4 +583,46 @@ namespace RTC ...@@ -567,4 +583,46 @@ namespace RTC
if (rtxPacket != packet) if (rtxPacket != packet)
delete rtxPacket; delete rtxPacket;
} }
void Consumer::RecalculateEffectiveProfile()
{
static const Json::StaticString JsonStringProfile{ "profile" };
Json::Value eventData(Json::objectValue);
RTC::RtpEncodingParameters::Profile newProfile;
// If there is no preferred profile, take the best one available.
if (this->preferredProfile == RTC::RtpEncodingParameters::Profile::NONE)
{
auto it = this->profiles.crbegin();
newProfile = *it;
}
// Otherwise take the highest available profile equal or lower than the preferred.
else
{
auto it = this->profiles.lower_bound(this->preferredProfile);
newProfile = *it;
}
if (newProfile == this->effectiveProfile)
return;
this->effectiveProfile = newProfile;
MS_DEBUG_TAG(rtp, "new effective profile: %s", RTC::RtpEncodingParameters::profile2String[this->effectiveProfile].c_str());;
// Notify.
eventData[JsonStringProfile] = RTC::RtpEncodingParameters::profile2String[this->effectiveProfile];
this->notifier->Emit(this->consumerId, "effectiveprofilechange", eventData);
if (IsEnabled() && !IsPaused())
{
this->rtpStream->ClearRetransmissionBuffer();
RequestFullFrame();
}
this->syncRequired = true;
}
} // namespace RTC } // namespace RTC
...@@ -315,7 +315,7 @@ namespace RTC ...@@ -315,7 +315,7 @@ namespace RTC
try try
{ {
profile = GetRtpProfile(rtpStream, packet); profile = GetProfile(rtpStream, packet);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
{ {
...@@ -612,7 +612,7 @@ namespace RTC ...@@ -612,7 +612,7 @@ namespace RTC
} }
} }
RTC::RtpEncodingParameters::Profile Producer::GetRtpProfile(RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet) RTC::RtpEncodingParameters::Profile Producer::GetProfile(RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet)
{ {
auto ssrc = packet->GetSsrc(); auto ssrc = packet->GetSsrc();
...@@ -632,6 +632,11 @@ namespace RTC ...@@ -632,6 +632,11 @@ namespace RTC
{ {
this->rtpProfiles[rtpStream].insert(encoding.profile); this->rtpProfiles[rtpStream].insert(encoding.profile);
for (auto& listener : this->listeners)
{
listener->OnProducerProfileEnabled(this, encoding.profile);
}
return encoding.profile; return encoding.profile;
} }
} }
...@@ -707,8 +712,33 @@ namespace RTC ...@@ -707,8 +712,33 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!healthy) auto rtpStreamRecv = static_cast<RtpStreamRecv*>(rtpStream);
MS_DEBUG_TAG(rtp, "stream is not healthy [ssrc:%" PRIu32 "]", rtpStream->GetSsrc()); auto& profiles = this->rtpProfiles[rtpStreamRecv];
// The stream has transitioned to non healthy.
if (rtpStream->IsHealthy() && !healthy)
{
MS_DEBUG_TAG(rtp, "stream is now unhealthy [ssrc:%" PRIu32 "]", rtpStream->GetSsrc());
// Notify about the profiles being disabled.
for (auto& profile : profiles)
{
for (auto& listener : this->listeners)
listener->OnProducerProfileDisabled(this, profile);
}
}
// The stream has transitioned to healthy.
if (!rtpStream->IsHealthy() && healthy)
{
MS_DEBUG_TAG(rtp, "stream is now healthy [ssrc:%" PRIu32 "]", rtpStream->GetSsrc());
// Notify about the profiles being disabled.
for (auto& profile : profiles)
{
for (auto& listener : this->listeners)
listener->OnProducerProfileDisabled(this, profile);
}
}
} }
inline void Producer::OnTimer(Timer* timer) inline void Producer::OnTimer(Timer* timer)
......
...@@ -454,6 +454,16 @@ namespace RTC ...@@ -454,6 +454,16 @@ namespace RTC
// Add us as listener. // Add us as listener.
consumer->AddListener(this); consumer->AddListener(this);
auto profiles = producer->GetProfiles();
std::set<RtpEncodingParameters::Profile>::reverse_iterator it;
for (it = profiles.rbegin(); it != profiles.rend(); ++it)
{
auto profile = *it;
consumer->AddProfile(profile);
}
// Insert into the maps. // Insert into the maps.
this->consumers[consumerId] = consumer; this->consumers[consumerId] = consumer;
this->mapProducerConsumers[producer].insert(consumer); this->mapProducerConsumers[producer].insert(consumer);
...@@ -1406,6 +1416,39 @@ namespace RTC ...@@ -1406,6 +1416,39 @@ namespace RTC
} }
} }
void Router::OnProducerProfileEnabled(RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile)
{
MS_TRACE();
MS_ASSERT(
this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end(),
"Producer not present in mapProducerConsumers");
auto& consumers = this->mapProducerConsumers[producer];
for (auto* consumer : consumers)
{
consumer->AddProfile(profile);
}
}
void Router::OnProducerProfileDisabled(RTC::Producer* producer, RTC::RtpEncodingParameters::Profile profile)
{
MS_TRACE();
MS_ASSERT(
this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end(),
"Producer not present in mapProducerConsumers");
auto& consumers = this->mapProducerConsumers[producer];
for (auto* consumer : consumers)
{
consumer->RemoveProfile(profile);
}
}
void Router::OnConsumerClosed(RTC::Consumer* consumer) void Router::OnConsumerClosed(RTC::Consumer* consumer)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -12,18 +12,18 @@ namespace RTC ...@@ -12,18 +12,18 @@ namespace RTC
// clang-format off // clang-format off
std::map<std::string, RTC::RtpEncodingParameters::Profile> RTC::RtpEncodingParameters::string2Profile = std::map<std::string, RTC::RtpEncodingParameters::Profile> RTC::RtpEncodingParameters::string2Profile =
{ {
{ "default", RTC::RtpEncodingParameters::Profile::DEFAULT }, { "none", RTC::RtpEncodingParameters::Profile::NONE },
{ "low", RTC::RtpEncodingParameters::Profile::LOW }, { "low", RTC::RtpEncodingParameters::Profile::LOW },
{ "medium", RTC::RtpEncodingParameters::Profile::MEDIUM }, { "medium", RTC::RtpEncodingParameters::Profile::MEDIUM },
{ "high", RTC::RtpEncodingParameters::Profile::HIGH } { "high", RTC::RtpEncodingParameters::Profile::HIGH }
}; };
std::map<RTC::RtpEncodingParameters::Profile, std::string> RTC::RtpEncodingParameters::profile2String = std::map<RTC::RtpEncodingParameters::Profile, std::string> RTC::RtpEncodingParameters::profile2String =
{ {
{ RTC::RtpEncodingParameters::Profile::DEFAULT, "default" }, { RTC::RtpEncodingParameters::Profile::NONE, "none" },
{ RTC::RtpEncodingParameters::Profile::LOW, "low" }, { RTC::RtpEncodingParameters::Profile::LOW, "low" },
{ RTC::RtpEncodingParameters::Profile::MEDIUM, "medium" }, { RTC::RtpEncodingParameters::Profile::MEDIUM, "medium" },
{ RTC::RtpEncodingParameters::Profile::HIGH, "high" } { RTC::RtpEncodingParameters::Profile::HIGH, "high" }
}; };
// clang-format on // clang-format on
......
...@@ -250,13 +250,14 @@ namespace RTC ...@@ -250,13 +250,14 @@ namespace RTC
void RtpStreamRecv::CheckHealth() void RtpStreamRecv::CheckHealth()
{ {
auto now = DepLibUV::GetTime(); auto now = DepLibUV::GetTime();
bool healthy = true;
if (this->receivedCounter.GetRate(now) == 0) if (this->receivedCounter.GetRate(now) == 0)
this->healthy = false; healthy = false;
else
this->healthy = true;
this->listener->OnRtpStreamHealthReport(this, this->healthy); // NOTE: Update the 'healthy' value after notification.
this->listener->OnRtpStreamHealthReport(this, healthy);
this->healthy = healthy;
} }
void RtpStreamRecv::OnNackGeneratorNackRequired(const std::vector<uint16_t>& seqNumbers) void RtpStreamRecv::OnNackGeneratorNackRequired(const std::vector<uint16_t>& seqNumbers)
......
...@@ -1563,6 +1563,18 @@ namespace RTC ...@@ -1563,6 +1563,18 @@ namespace RTC
// Do nothing. // Do nothing.
} }
void Transport::OnProducerProfileEnabled(RTC::Producer* /*producer*/,
RTC::RtpEncodingParameters::Profile /*profile*/)
{
// Do nothing.
}
void Transport::OnProducerProfileDisabled(RTC::Producer* /*producer*/,
RTC::RtpEncodingParameters::Profile /*profile*/)
{
// Do nothing.
}
void Transport::OnConsumerClosed(RTC::Consumer* consumer) void Transport::OnConsumerClosed(RTC::Consumer* consumer)
{ {
MS_TRACE(); MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论