提交 46bd7687 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

WIP

上级 d6632c9a
......@@ -79,13 +79,12 @@ namespace RTC
virtual void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) = 0;
void ProducerClosed();
void SetExternallyManagedBitrate();
virtual uint8_t GetBitratePriority() const = 0;
virtual uint32_t UseAvailableBitrate(uint32_t bitrate, bool considerLoss) = 0;
virtual uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) = 0;
virtual void ApplyLayers() = 0;
virtual uint32_t GetDesiredBitrate() const = 0;
virtual void SendRtpPacket(RTC::RtpPacket* packet) = 0;
virtual std::vector<RTC::RtpStreamSend*> GetRtpStreams() = 0;
virtual uint8_t GetBitratePriority() const = 0;
virtual uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) = 0;
virtual void ApplyLayers() = 0;
virtual uint32_t GetDesiredBitrate() const = 0;
virtual void SendRtpPacket(RTC::RtpPacket* packet) = 0;
virtual std::vector<RTC::RtpStreamSend*> GetRtpStreams() = 0;
virtual void GetRtcp(
RTC::RTCP::CompoundPacket* packet, RTC::RtpStreamSend* rtpStream, uint64_t nowMs) = 0;
virtual void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) = 0;
......
......@@ -23,7 +23,6 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
uint8_t GetBitratePriority() const override;
uint32_t UseAvailableBitrate(uint32_t bitrate, bool considerLoss) override;
uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) override;
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
......
......@@ -24,7 +24,6 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
uint8_t GetBitratePriority() const override;
uint32_t UseAvailableBitrate(uint32_t bitrate, bool considerLoss) override;
uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) override;
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
......
......@@ -25,7 +25,6 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
uint8_t GetBitratePriority() const override;
uint32_t UseAvailableBitrate(uint32_t bitrate, bool considerLoss) override;
uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) override;
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
......
......@@ -26,7 +26,6 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
uint8_t GetBitratePriority() const override;
uint32_t UseAvailableBitrate(uint32_t bitrate, bool considerLoss) override;
uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) override;
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
......
......@@ -143,14 +143,6 @@ namespace RTC
return 0u;
}
uint32_t PipeConsumer::UseAvailableBitrate(uint32_t /*bitrate*/, bool /*considerLoss*/)
{
MS_TRACE();
// PipeConsumer does not play the BWE game.
return 0u;
}
uint32_t PipeConsumer::IncreaseLayer(uint32_t /*bitrate*/, bool /*considerLoss*/)
{
MS_TRACE();
......
......@@ -155,14 +155,6 @@ namespace RTC
return 1u;
}
uint32_t SimpleConsumer::UseAvailableBitrate(uint32_t /*bitrate*/, bool /*considerLoss*/)
{
MS_TRACE();
// SimpleConsumer does not play the BWE game.
return 0u;
}
uint32_t SimpleConsumer::IncreaseLayer(uint32_t /*bitrate*/, bool /*considerLoss*/)
{
MS_TRACE();
......
......@@ -346,187 +346,12 @@ namespace RTC
return 1u;
}
uint32_t SimulcastConsumer::UseAvailableBitrate(uint32_t bitrate, bool considerLoss)
{
MS_TRACE();
MS_ASSERT(this->externallyManagedBitrate, "bitrate is not externally managed");
this->provisionalTargetSpatialLayer = -1;
this->provisionalTargetTemporalLayer = -1;
if (!RTC::Consumer::IsActive())
return 0u;
uint32_t virtualBitrate;
if (considerLoss)
{
// Calculate virtual available bitrate based on given bitrate and our
// packet lost.
auto lossPercentage = this->rtpStream->GetLossPercentage();
if (lossPercentage < 2)
virtualBitrate = 1.08 * bitrate;
else if (lossPercentage > 10)
virtualBitrate = (1 - 0.5 * (lossPercentage / 100)) * bitrate;
else
virtualBitrate = bitrate;
}
else
{
virtualBitrate = bitrate;
}
uint32_t usedBitrate{ 0u };
uint8_t maxProducerScore{ 0u };
auto nowMs = DepLibUV::GetTimeMs();
for (size_t sIdx{ 0u }; sIdx < this->producerRtpStreams.size(); ++sIdx)
{
auto spatialLayer = static_cast<int16_t>(sIdx);
auto* producerRtpStream = this->producerRtpStreams.at(sIdx);
auto producerScore = producerRtpStream ? producerRtpStream->GetScore() : 0u;
// If this is higher than current spatial layer and we moved to to current spatial
// layer due to BWE limitations, check how much it has elapsed since then.
if (nowMs - this->lastBweDowngradeAtMs < BweDowngradeConservativeMs)
{
if (usedBitrate > 0u && spatialLayer > this->currentSpatialLayer)
{
MS_DEBUG_DEV(
"avoid upgrading to spatial layer %" PRIi16 " due to recent BWE downgrade", spatialLayer);
goto done;
}
}
// Ignore spatial layers for non existing producer streams or for those
// with score 0.
if (producerScore == 0u)
continue;
// If the stream has not been active time enough and we have an active one
// already, move to the next spatial layer.
if (usedBitrate > 0u && producerRtpStream->GetActiveMs() < StreamMinActiveMs)
continue;
// We may not yet switch to this spatial layer.
if (!CanSwitchToSpatialLayer(spatialLayer))
continue;
// If the stream score is worse than the best seen and not good enough, ignore
// this stream.
if (producerScore < maxProducerScore && producerScore < StreamGoodScore)
continue;
maxProducerScore = producerScore;
int16_t temporalLayer{ 0 };
// Check bitrate of every temporal layer.
for (; temporalLayer < producerRtpStream->GetTemporalLayers(); ++temporalLayer)
{
auto requiredBitrate = producerRtpStream->GetBitrate(nowMs, 0u, temporalLayer);
MS_DEBUG_DEV(
"testing layers %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
", required bitrate:%" PRIu32 "]",
spatialLayer,
temporalLayer,
virtualBitrate,
requiredBitrate);
// If layer is not active move to next spatial layer.
if (requiredBitrate == 0u)
break;
// If this layer requires more bitrate than the given one, abort the loop
// (so use the previous chosen layers if any).
if (requiredBitrate > virtualBitrate)
goto done;
// Set provisional layers and used bitrate.
this->provisionalTargetSpatialLayer = spatialLayer;
this->provisionalTargetTemporalLayer = temporalLayer;
usedBitrate = requiredBitrate;
// If this is the preferred spatial and temporal layer, exit the loops.
// clang-format off
if (
this->provisionalTargetSpatialLayer == this->preferredSpatialLayer &&
this->provisionalTargetTemporalLayer == this->preferredTemporalLayer &&
producerScore >= StreamGoodScore
)
// clang-format on
{
goto done;
}
}
// If this is the preferred or higher spatial layer and has good score,
// take it and exit.
// clang-format off
if (
this->provisionalTargetSpatialLayer >= this->preferredSpatialLayer &&
producerScore >= StreamGoodScore
)
// clang-format on
{
break;
}
}
done:
// clang-format off
if (
this->provisionalTargetSpatialLayer != this->targetSpatialLayer ||
this->provisionalTargetTemporalLayer != this->targetTemporalLayer
)
// clang-format on
{
MS_DEBUG_2TAGS(
bwe,
simulcast,
"choosing layers %" PRIi16 ":%" PRIi16 " [bitrate:%" PRIu32 ", virtual bitrate:%" PRIu32
", used bitrate:%" PRIu32 ", consumerId:%s]",
this->provisionalTargetSpatialLayer,
this->provisionalTargetTemporalLayer,
bitrate,
virtualBitrate,
usedBitrate,
this->id.c_str());
}
else
{
MS_DEBUG_DEV(
"choosing layers %" PRIi16 ":%" PRIi16 " [bitrate:%" PRIu32 ", virtual bitrate:%" PRIu32
", used bitrate:%" PRIu32 ", consumerId:%s]",
this->provisionalTargetSpatialLayer,
this->provisionalTargetTemporalLayer,
bitrate,
virtualBitrate,
usedBitrate,
this->id.c_str());
}
// Must recompute usedBitrate based on given bitrate, virtualBitrate and
// usedBitrate.
if (usedBitrate <= bitrate)
return usedBitrate;
else if (usedBitrate <= virtualBitrate)
return bitrate;
else
return usedBitrate;
}
uint32_t SimulcastConsumer::IncreaseLayer(uint32_t bitrate, bool considerLoss)
{
MS_TRACE();
MS_ASSERT(this->externallyManagedBitrate, "bitrate is not externally managed");
MS_ASSERT(IsActive(), "should be active (Consumer::IsActive())");
MS_ASSERT(IsActive(), "should be active");
// If already in the preferred layers, do nothing.
// clang-format off
......@@ -650,6 +475,14 @@ namespace RTC
requiredBitrate = 1u; // Don't set 0 since it would be ignored.
}
MS_DEBUG_DEV(
"testing layers %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
", required bitrate:%" PRIu32 "]",
spatialLayer,
temporalLayer,
virtualBitrate,
requiredBitrate);
// If active layer, end iterations here. Otherwise move to next spatial layer.
if (requiredBitrate)
goto done;
......@@ -676,10 +509,8 @@ namespace RTC
this->provisionalTargetSpatialLayer = spatialLayer;
this->provisionalTargetTemporalLayer = temporalLayer;
MS_DEBUG_2TAGS(
bwe,
simulcast,
"upgrading to layers %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
MS_DEBUG_DEV(
"setting provisional layers to %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
", required bitrate:%" PRIu32 "]",
this->provisionalTargetSpatialLayer,
this->provisionalTargetTemporalLayer,
......@@ -928,7 +759,7 @@ namespace RTC
{
// Max delay in ms we allow for the stream when switching.
// https://en.wikipedia.org/wiki/Audio-to-video_synchronization#Recommendations
static const uint32_t MaxExtraOffsetMs{ 50u };
static const uint32_t MaxExtraOffsetMs{ 75u };
int64_t maxTsExtraOffset = MaxExtraOffsetMs * this->rtpStream->GetClockRate() / 1000;
uint32_t tsExtraOffset =
......@@ -938,7 +769,7 @@ namespace RTC
if (this->keyFrameForTsOffsetRequested)
{
// Give up and use the theoretical offset.
tsExtraOffset = 0u;
tsExtraOffset = 1u;
}
else if (tsExtraOffset > maxTsExtraOffset)
{
......
......@@ -306,165 +306,12 @@ namespace RTC
return 1u;
}
uint32_t SvcConsumer::UseAvailableBitrate(uint32_t bitrate, bool considerLoss)
{
MS_TRACE();
MS_ASSERT(this->externallyManagedBitrate, "bitrate is not externally managed");
this->provisionalTargetSpatialLayer = -1;
this->provisionalTargetTemporalLayer = -1;
if (!RTC::Consumer::IsActive())
return 0u;
uint32_t virtualBitrate;
if (considerLoss)
{
// Calculate virtual available bitrate based on given bitrate and our
// packet lost.
auto lossPercentage = this->rtpStream->GetLossPercentage();
if (lossPercentage < 2)
virtualBitrate = 1.08 * bitrate;
else if (lossPercentage > 10)
virtualBitrate = (1 - 0.5 * (lossPercentage / 100)) * bitrate;
else
virtualBitrate = bitrate;
}
else
{
virtualBitrate = bitrate;
}
uint32_t usedBitrate{ 0u };
auto nowMs = DepLibUV::GetTimeMs();
int16_t spatialLayer{ 0 };
if (!this->producerRtpStream)
goto done;
if (this->producerRtpStream->GetScore() == 0u)
goto done;
for (; spatialLayer < this->producerRtpStream->GetSpatialLayers(); ++spatialLayer)
{
// If this is higher than current spatial layer and we moved to to current spatial
// layer due to BWE limitations, check how much it has elapsed since then.
if (nowMs - this->lastBweDowngradeAtMs < BweDowngradeConservativeMs)
{
if (usedBitrate > 0u && spatialLayer > this->encodingContext->GetCurrentSpatialLayer())
{
MS_DEBUG_DEV(
"avoid upgrading to spatial layer %" PRIi16 " due to recent BWE downgrade", spatialLayer);
goto done;
}
}
int16_t temporalLayer{ 0 };
// Check bitrate of every temporal layer.
for (; temporalLayer < this->producerRtpStream->GetTemporalLayers(); ++temporalLayer)
{
auto requiredBitrate =
this->producerRtpStream->GetBitrate(nowMs, spatialLayer, temporalLayer);
MS_DEBUG_DEV(
"testing layers %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
", required bitrate:%" PRIu32 "]",
spatialLayer,
temporalLayer,
virtualBitrate,
requiredBitrate);
// If layer is not active move to next spatial layer.
if (requiredBitrate == 0u)
break;
// If this layer requires more bitrate than the given one, abort the loop
// (so use the previous chosen layers if any).
if (requiredBitrate > virtualBitrate)
goto done;
// Set provisional layers and used bitrate.
this->provisionalTargetSpatialLayer = spatialLayer;
this->provisionalTargetTemporalLayer = temporalLayer;
usedBitrate = requiredBitrate;
// If this is the preferred spatial and temporal layer, exit the loops.
// clang-format off
if (
this->provisionalTargetSpatialLayer == this->preferredSpatialLayer &&
this->provisionalTargetTemporalLayer == this->preferredTemporalLayer
)
// clang-format on
{
goto done;
}
}
// If this is the preferred or higher spatial layer, take it and exit.
if (this->provisionalTargetSpatialLayer >= this->preferredSpatialLayer)
break;
}
done:
// clang-format off
if (
this->provisionalTargetSpatialLayer != this->encodingContext->GetTargetSpatialLayer() ||
this->provisionalTargetTemporalLayer != this->encodingContext->GetTargetTemporalLayer()
)
// clang-format on
{
MS_DEBUG_2TAGS(
bwe,
svc,
"choosing layers %" PRIi16 ":%" PRIi16 " [bitrate:%" PRIu32 ", virtual bitrate:%" PRIu32
", used bitrate:%" PRIu32 ", consumerId:%s]",
this->provisionalTargetSpatialLayer,
this->provisionalTargetTemporalLayer,
bitrate,
virtualBitrate,
usedBitrate,
this->id.c_str());
}
else
{
MS_DEBUG_DEV(
"choosing layers %" PRIi16 ":%" PRIi16 " [bitrate:%" PRIu32 ", virtual bitrate:%" PRIu32
", used bitrate:%" PRIu32 ", consumerId:%s]",
this->provisionalTargetSpatialLayer,
this->provisionalTargetTemporalLayer,
bitrate,
virtualBitrate,
usedBitrate,
this->id.c_str());
}
// Must recompute usedBitrate based on given bitrate, virtualBitrate and
// usedBitrate.
if (usedBitrate <= bitrate)
return usedBitrate;
else if (usedBitrate <= virtualBitrate)
return bitrate;
else
return usedBitrate;
}
uint32_t SvcConsumer::IncreaseLayer(uint32_t bitrate, bool considerLoss)
{
MS_TRACE();
MS_ASSERT(this->externallyManagedBitrate, "bitrate is not externally managed");
if (!RTC::Consumer::IsActive())
return 0u;
if (!this->producerRtpStream)
return 0u;
MS_ASSERT(IsActive(), "should be active");
if (this->producerRtpStream->GetScore() == 0u)
return 0u;
......@@ -544,6 +391,14 @@ namespace RTC
requiredBitrate =
this->producerRtpStream->GetLayerBitrate(nowMs, spatialLayer, temporalLayer);
MS_DEBUG_DEV(
"testing layers %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
", required bitrate:%" PRIu32 "]",
spatialLayer,
temporalLayer,
virtualBitrate,
requiredBitrate);
// If active layer, end iterations here. Otherwise move to next spatial layer.
if (requiredBitrate)
goto done;
......@@ -570,9 +425,7 @@ namespace RTC
this->provisionalTargetSpatialLayer = spatialLayer;
this->provisionalTargetTemporalLayer = temporalLayer;
MS_DEBUG_2TAGS(
bwe,
svc,
MS_DEBUG_DEV(
"upgrading to layers %" PRIi16 ":%" PRIi16 " [virtual bitrate:%" PRIu32
", required bitrate:%" PRIu32 "]",
this->provisionalTargetSpatialLayer,
......
......@@ -1977,7 +1977,7 @@ namespace RTC
this->tccClient->RescheduleNextAvailableBitrateEvent();
MS_DEBUG_DEV("before iterations [availableBitrate:%" PRIu32 "]", availableBitrate);
MS_DEBUG_DEV("before layer-by-layer iterations [availableBitrate:%" PRIu32 "]", availableBitrate);
// Redistribute the available bitrate by allowing Consumers to increase
// layer by layer.
......@@ -1988,14 +1988,8 @@ namespace RTC
for (auto it = multimapPriorityConsumer.rbegin(); it != multimapPriorityConsumer.rend(); ++it)
{
auto* consumer = it->second;
MS_DEBUG_DEV(
"layer bitrate for Consumer [bitrate:%" PRIu32 ", consumerId:%s]",
availableBitrate,
consumer->id.c_str());
auto bweType = this->tccClient->GetBweType();
uint32_t usedBitrate;
auto bweType = this->tccClient->GetBweType();
switch (bweType)
{
......@@ -2016,7 +2010,7 @@ namespace RTC
break;
}
MS_DEBUG_DEV("after layer-by-layer iteration [availableBitrate:%" PRIu32 "]", availableBitrate);
MS_DEBUG_DEV("after layer-by-layer iterations [availableBitrate:%" PRIu32 "]", availableBitrate);
// Finally instruct Consumers to apply their computed layers.
for (auto it = multimapPriorityConsumer.rbegin(); it != multimapPriorityConsumer.rend(); ++it)
......
......@@ -12,8 +12,8 @@ namespace RTC
/* Static. */
static constexpr uint32_t MinBitrate{ 30000u };
static constexpr float MaxBitrateIncrementFactor{ 1.25f }; // TODO: Let's see.
static constexpr float MaxPaddingBitrateFactor{ 0.75f }; // TODO: Let's see.
static constexpr float MaxBitrateIncrementFactor{ 1.35f };
static constexpr float MaxPaddingBitrateFactor{ 0.85f };
static constexpr uint64_t AvailableBitrateEventInterval{ 2000u }; // In ms.
/* Instance methods. */
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论