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

SimulcastConsumer: rewrite RecalculateTargetSpatialLayer()

上级 f3352b63
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
#include "RTC/Codecs/Codecs.hpp" #include "RTC/Codecs/Codecs.hpp"
#include <iostream>
namespace RTC namespace RTC
{ {
...@@ -193,8 +194,6 @@ namespace RTC ...@@ -193,8 +194,6 @@ namespace RTC
// Recalculate layers. // Recalculate layers.
RecalculateTargetSpatialLayer(); RecalculateTargetSpatialLayer();
// TODO: Probably just if this stream is the current spatial layer.
//
// Emit the score event. // Emit the score event.
EmitScore(); EmitScore();
} }
...@@ -609,36 +608,136 @@ namespace RTC ...@@ -609,36 +608,136 @@ namespace RTC
int16_t newTargetSpatialLayer{ -1 }; int16_t newTargetSpatialLayer{ -1 };
// No Producer streams. if (this->GetProducerCurrentRtpStream())
if (this->producerRtpStreams.empty())
{ {
newTargetSpatialLayer = -1; std::cout << "RecalculateTargetSpatialLayer."
<< ", consumer score: " << static_cast<unsigned int>(this->rtpStream->GetScore())
<< ", producer score: "
<< static_cast<unsigned int>(this->GetProducerCurrentRtpStream()->GetScore())
<< ", currentSpatialLayer: " << static_cast<int>(this->currentSpatialLayer)
<< ", targetSpatialLayer: " << static_cast<int>(this->targetSpatialLayer)
<< ", preferredSpatialLayer: " << static_cast<int>(this->preferredSpatialLayer)
<< std::endl;
} }
// Try with the closest spatial layer to the preferred one.
else else
{ {
std::cout << "RecalculateTargetSpatialLayer."
<< ", consumer score: " << static_cast<unsigned int>(this->rtpStream->GetScore())
<< ", producer score: "
<< "nullptr"
<< ", currentSpatialLayer: " << static_cast<int>(this->currentSpatialLayer)
<< ", targetSpatialLayer: " << static_cast<int>(this->targetSpatialLayer)
<< ", preferredSpatialLayer: " << static_cast<int>(this->preferredSpatialLayer)
<< std::endl;
}
// No current or preferred spatial layer, select the highest possible.
if (this->currentSpatialLayer == -1 || this->preferredSpatialLayer == -1)
{
std::cout << "RecalculateTargetSpatialLayer. no current or preferred spatial layer, selecting the highest possible..."
<< std::endl;
uint8_t maxScore = 0;
for (int idx = this->producerRtpStreams.size() - 1; idx >= 0; --idx) for (int idx = this->producerRtpStreams.size() - 1; idx >= 0; --idx)
{ {
auto spatialLayer = static_cast<int16_t>(idx); auto spatialLayer = static_cast<int16_t>(idx);
auto* producerRtpStream = this->producerRtpStreams[idx]; auto* producerRtpStream = this->producerRtpStreams[idx];
// Ignore spatial layers higher than the preferred one. // Ignore spatial layers for non existing Producer streams.
if (spatialLayer > this->preferredSpatialLayer) if (!producerRtpStream)
continue; continue;
// Ignore spatial layers for non existing or unhealthy Producer streams. // Ignore spatial layers higher than the preferred one, if defined.
if (!producerRtpStream || producerRtpStream->GetScore() < 5) if (this->preferredSpatialLayer != -1 && idx > this->preferredSpatialLayer)
return;
if (producerRtpStream->GetScore() >= maxScore)
{
std::cout << "RecalculateTargetSpatialLayer. we have something: " << spatialLayer
<< std::endl;
maxScore = producerRtpStream->GetScore();
newTargetSpatialLayer = spatialLayer;
if (producerRtpStream->GetScore() > 7)
{
std::cout << "RecalculateTargetSpatialLayer. score > 7, keep it : " << spatialLayer
<< std::endl;
newTargetSpatialLayer = spatialLayer;
break;
}
}
}
}
// Downgrade is needed.
else if (
!this->GetProducerCurrentRtpStream() || this->GetProducerCurrentRtpStream()->GetScore() < 7 ||
this->rtpStream->GetScore() < 5 || this->preferredSpatialLayer < this->currentSpatialLayer)
{
std::cout << "RecalculateTargetSpatialLayer. downgrading..." << std::endl;
uint8_t maxScore = 0;
for (int idx = this->currentSpatialLayer - 1; idx >= 0; --idx)
{
auto spatialLayer = static_cast<int16_t>(idx);
auto* producerRtpStream = this->producerRtpStreams[idx];
// Ignore spatial layers for non existing Producer streams.
if (!producerRtpStream)
continue; continue;
newTargetSpatialLayer = spatialLayer; if (producerRtpStream->GetScore() >= maxScore)
{
std::cout << "RecalculateTargetSpatialLayer. we have something: " << spatialLayer
<< std::endl;
maxScore = producerRtpStream->GetScore();
newTargetSpatialLayer = spatialLayer;
if (producerRtpStream->GetScore() > 7)
{
std::cout << "RecalculateTargetSpatialLayer. score > 7, keep it : " << spatialLayer
<< std::endl;
newTargetSpatialLayer = spatialLayer;
break;
}
}
}
}
// Update to the highest possible spatial layer.
else
{
std::cout << "RecalculateTargetSpatialLayer. trying to upgrade..." << std::endl;
break; size_t idx = this->currentSpatialLayer == -1 ? 0 : this->currentSpatialLayer + 1;
for (; idx <= this->producerRtpStreams.size() - 1; ++idx)
{
auto spatialLayer = static_cast<int16_t>(idx);
auto* producerRtpStream = this->producerRtpStreams[idx];
// Ignore spatial layers above the preferred one.
if (spatialLayer > this->preferredSpatialLayer)
break;
// Ignore spatial layers for non existing Producer streams.
if (!producerRtpStream)
continue;
// Take this as the new target if it is good enough.
if (producerRtpStream->GetScore() >= 7)
{
std::cout << "RecalculateTargetSpatialLayer. good enough spatial layer found: "
<< spatialLayer << std::endl;
newTargetSpatialLayer = spatialLayer;
break;
}
} }
}
// TODO: It may happen that spatial layer 1 exists and it's healthy while 0 if (newTargetSpatialLayer == -1)
// does not exist or is unhealthy. If preferred spatial layer was 0 then we {
// end here without newTargetSpatialLayer. In that scenario we should take std::cout << "RecalculateTargetSpatialLayer, target layer not updated" << std::endl;
// whichever available. return;
} }
// Nothing changed. // Nothing changed.
...@@ -686,8 +785,7 @@ namespace RTC ...@@ -686,8 +785,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// TODO: Should recalculate target spatial layer based on our own score this->RecalculateTargetSpatialLayer();
// and so on.
// Emit the score event. // Emit the score event.
EmitScore(); EmitScore();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论