提交 4de0f4e7 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Improve SimulcastConsumer and SvcConsumer:

- Better handling of GetBitratePriority()
上级 2fa00787
...@@ -65,7 +65,7 @@ namespace RTC ...@@ -65,7 +65,7 @@ namespace RTC
virtual void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) = 0; virtual void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) = 0;
void ProducerClosed(); void ProducerClosed();
virtual void SetExternallyManagedBitrate(); virtual void SetExternallyManagedBitrate();
virtual int16_t GetBitratePriority() const; virtual uint16_t GetBitratePriority() const;
virtual uint32_t UseAvailableBitrate(uint32_t bitrate); virtual uint32_t UseAvailableBitrate(uint32_t bitrate);
virtual uint32_t IncreaseLayer(uint32_t bitrate); virtual uint32_t IncreaseLayer(uint32_t bitrate);
virtual void ApplyLayers(); virtual void ApplyLayers();
......
...@@ -74,6 +74,7 @@ namespace RTC ...@@ -74,6 +74,7 @@ namespace RTC
virtual void Resume() = 0; virtual void Resume() = 0;
virtual uint32_t GetBitrate(uint64_t now) = 0; virtual uint32_t GetBitrate(uint64_t now) = 0;
virtual uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) = 0; virtual uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) = 0;
virtual uint32_t GetSpatialLayerBitrate(uint64_t now, uint8_t spatialLayer) = 0;
virtual uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) = 0; virtual uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) = 0;
void ResetScore(uint8_t score, bool notify); void ResetScore(uint8_t score, bool notify);
uint8_t GetFractionLost() const; uint8_t GetFractionLost() const;
......
...@@ -31,6 +31,7 @@ namespace RTC ...@@ -31,6 +31,7 @@ namespace RTC
void Update(RTC::RtpPacket* packet); void Update(RTC::RtpPacket* packet);
uint32_t GetBitrate(uint64_t now); uint32_t GetBitrate(uint64_t now);
uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer); uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer);
uint32_t GetSpatialLayerBitrate(uint64_t now, uint8_t spatialLayer);
uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer); uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer);
size_t GetPacketCount() const; size_t GetPacketCount() const;
size_t GetBytes() const; size_t GetBytes() const;
...@@ -53,6 +54,7 @@ namespace RTC ...@@ -53,6 +54,7 @@ namespace RTC
void Resume() override; void Resume() override;
uint32_t GetBitrate(uint64_t now) override; uint32_t GetBitrate(uint64_t now) override;
uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override; uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override;
uint32_t GetSpatialLayerBitrate(uint64_t now, uint8_t spatialLayer) override;
uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override; uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override;
private: private:
...@@ -98,6 +100,11 @@ namespace RTC ...@@ -98,6 +100,11 @@ namespace RTC
return this->transmissionCounter.GetBitrate(now, spatialLayer, temporalLayer); return this->transmissionCounter.GetBitrate(now, spatialLayer, temporalLayer);
} }
inline uint32_t RtpStreamRecv::GetSpatialLayerBitrate(uint64_t now, uint8_t spatialLayer)
{
return this->transmissionCounter.GetSpatialLayerBitrate(now, spatialLayer);
}
inline uint32_t RtpStreamRecv::GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) inline uint32_t RtpStreamRecv::GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer)
{ {
return this->transmissionCounter.GetLayerBitrate(now, spatialLayer, temporalLayer); return this->transmissionCounter.GetLayerBitrate(now, spatialLayer, temporalLayer);
......
...@@ -51,6 +51,7 @@ namespace RTC ...@@ -51,6 +51,7 @@ namespace RTC
void SendProbationRtpPacket(uint16_t seq); void SendProbationRtpPacket(uint16_t seq);
uint32_t GetBitrate(uint64_t now) override; uint32_t GetBitrate(uint64_t now) override;
uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override; uint32_t GetBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override;
uint32_t GetSpatialLayerBitrate(uint64_t now, uint8_t spatialLayer) override;
uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override; uint32_t GetLayerBitrate(uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) override;
private: private:
......
...@@ -26,7 +26,7 @@ namespace RTC ...@@ -26,7 +26,7 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override; void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
void SetExternallyManagedBitrate() override; void SetExternallyManagedBitrate() override;
int16_t GetBitratePriority() const override; uint16_t GetBitratePriority() const override;
uint32_t UseAvailableBitrate(uint32_t bitrate) override; uint32_t UseAvailableBitrate(uint32_t bitrate) override;
uint32_t IncreaseLayer(uint32_t bitrate) override; uint32_t IncreaseLayer(uint32_t bitrate) override;
void ApplyLayers() override; void ApplyLayers() override;
......
...@@ -26,7 +26,7 @@ namespace RTC ...@@ -26,7 +26,7 @@ namespace RTC
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override; void ProducerRtcpSenderReport(RTC::RtpStream* rtpStream, bool first) override;
void SetExternallyManagedBitrate() override; void SetExternallyManagedBitrate() override;
int16_t GetBitratePriority() const override; uint16_t GetBitratePriority() const override;
uint32_t UseAvailableBitrate(uint32_t bitrate) override; uint32_t UseAvailableBitrate(uint32_t bitrate) override;
uint32_t IncreaseLayer(uint32_t bitrate) override; uint32_t IncreaseLayer(uint32_t bitrate) override;
void ApplyLayers() override; void ApplyLayers() override;
......
...@@ -337,13 +337,13 @@ namespace RTC ...@@ -337,13 +337,13 @@ namespace RTC
// Do nothing. // Do nothing.
} }
int16_t Consumer::GetBitratePriority() const uint16_t Consumer::GetBitratePriority() const
{ {
MS_TRACE(); MS_TRACE();
// This method must be override by subclasses with layers. By default // This method must be override by subclasses with layers. By default
// it just returns 0. // it just returns 0.
return 0; return 0u;
} }
uint32_t Consumer::UseAvailableBitrate(uint32_t /*bitrate*/) uint32_t Consumer::UseAvailableBitrate(uint32_t /*bitrate*/)
......
...@@ -13,6 +13,8 @@ namespace RTC ...@@ -13,6 +13,8 @@ namespace RTC
RtpStreamRecv::TransmissionCounter::TransmissionCounter(uint8_t spatialLayers, uint8_t temporalLayers) RtpStreamRecv::TransmissionCounter::TransmissionCounter(uint8_t spatialLayers, uint8_t temporalLayers)
{ {
MS_TRACE();
// Reserve vectors capacity. // Reserve vectors capacity.
this->spatialLayerCounters = std::vector<std::vector<RTC::RtpDataCounter>>(spatialLayers); this->spatialLayerCounters = std::vector<std::vector<RTC::RtpDataCounter>>(spatialLayers);
; ;
...@@ -25,6 +27,8 @@ namespace RTC ...@@ -25,6 +27,8 @@ namespace RTC
void RtpStreamRecv::TransmissionCounter::Update(RTC::RtpPacket* packet) void RtpStreamRecv::TransmissionCounter::Update(RTC::RtpPacket* packet)
{ {
MS_TRACE();
auto spatialLayer = packet->GetSpatialLayer(); auto spatialLayer = packet->GetSpatialLayer();
auto temporalLayer = packet->GetTemporalLayer(); auto temporalLayer = packet->GetTemporalLayer();
...@@ -36,13 +40,15 @@ namespace RTC ...@@ -36,13 +40,15 @@ namespace RTC
if (temporalLayer > this->spatialLayerCounters[0].size() - 1) if (temporalLayer > this->spatialLayerCounters[0].size() - 1)
temporalLayer = this->spatialLayerCounters[0].size() - 1; temporalLayer = this->spatialLayerCounters[0].size() - 1;
auto& counter = this->spatialLayerCounters.at(spatialLayer).at(temporalLayer); auto& counter = this->spatialLayerCounters[spatialLayer][temporalLayer];
counter.Update(packet); counter.Update(packet);
} }
uint32_t RtpStreamRecv::TransmissionCounter::GetBitrate(uint64_t now) uint32_t RtpStreamRecv::TransmissionCounter::GetBitrate(uint64_t now)
{ {
MS_TRACE();
uint32_t rate{ 0u }; uint32_t rate{ 0u };
for (auto& spatialLayerCounter : this->spatialLayerCounters) for (auto& spatialLayerCounter : this->spatialLayerCounters)
...@@ -59,9 +65,11 @@ namespace RTC ...@@ -59,9 +65,11 @@ namespace RTC
uint32_t RtpStreamRecv::TransmissionCounter::GetBitrate( uint32_t RtpStreamRecv::TransmissionCounter::GetBitrate(
uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer)
{ {
MS_TRACE();
MS_ASSERT(spatialLayer < this->spatialLayerCounters.size(), "spatialLayer too high"); MS_ASSERT(spatialLayer < this->spatialLayerCounters.size(), "spatialLayer too high");
MS_ASSERT( MS_ASSERT(
temporalLayer < this->spatialLayerCounters.at(spatialLayer).size(), "temporalLayer too high"); temporalLayer < this->spatialLayerCounters[spatialLayer].size(), "temporalLayer too high");
uint32_t rate{ 0u }; uint32_t rate{ 0u };
...@@ -86,12 +94,38 @@ namespace RTC ...@@ -86,12 +94,38 @@ namespace RTC
return rate; return rate;
} }
uint32_t RtpStreamRecv::TransmissionCounter::GetSpatialLayerBitrate(uint64_t now, uint8_t spatialLayer)
{
MS_TRACE();
MS_ASSERT(spatialLayer < this->spatialLayerCounters.size(), "spatialLayer too high");
uint32_t rate{ 0u };
// clang-format off
for (
uint8_t tIdx{ 0u };
tIdx < this->spatialLayerCounters[spatialLayer].size();
++tIdx
)
// clang-format on
{
auto& temporalLayerCounter = this->spatialLayerCounters[spatialLayer][tIdx];
rate += temporalLayerCounter.GetBitrate(now);
}
return rate;
}
uint32_t RtpStreamRecv::TransmissionCounter::GetLayerBitrate( uint32_t RtpStreamRecv::TransmissionCounter::GetLayerBitrate(
uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer) uint64_t now, uint8_t spatialLayer, uint8_t temporalLayer)
{ {
MS_TRACE();
MS_ASSERT(spatialLayer < this->spatialLayerCounters.size(), "spatialLayer too high"); MS_ASSERT(spatialLayer < this->spatialLayerCounters.size(), "spatialLayer too high");
MS_ASSERT( MS_ASSERT(
temporalLayer < this->spatialLayerCounters.at(spatialLayer).size(), "temporalLayer too high"); temporalLayer < this->spatialLayerCounters[spatialLayer].size(), "temporalLayer too high");
auto& counter = this->spatialLayerCounters[spatialLayer][temporalLayer]; auto& counter = this->spatialLayerCounters[spatialLayer][temporalLayer];
...@@ -100,6 +134,8 @@ namespace RTC ...@@ -100,6 +134,8 @@ namespace RTC
size_t RtpStreamRecv::TransmissionCounter::GetPacketCount() const size_t RtpStreamRecv::TransmissionCounter::GetPacketCount() const
{ {
MS_TRACE();
size_t packetCount{ 0u }; size_t packetCount{ 0u };
for (auto& spatialLayerCounter : this->spatialLayerCounters) for (auto& spatialLayerCounter : this->spatialLayerCounters)
...@@ -115,6 +151,8 @@ namespace RTC ...@@ -115,6 +151,8 @@ namespace RTC
size_t RtpStreamRecv::TransmissionCounter::GetBytes() const size_t RtpStreamRecv::TransmissionCounter::GetBytes() const
{ {
MS_TRACE();
size_t bytes{ 0u }; size_t bytes{ 0u };
for (auto& spatialLayerCounter : this->spatialLayerCounters) for (auto& spatialLayerCounter : this->spatialLayerCounters)
......
...@@ -259,12 +259,23 @@ namespace RTC ...@@ -259,12 +259,23 @@ namespace RTC
uint32_t RtpStreamSend::GetBitrate(uint64_t /*now*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/) uint32_t RtpStreamSend::GetBitrate(uint64_t /*now*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
{ {
MS_TRACE();
MS_ABORT("invalid method call");
}
uint32_t RtpStreamSend::GetSpatialLayerBitrate(uint64_t /*now*/, uint8_t /*spatialLayer*/)
{
MS_TRACE();
MS_ABORT("invalid method call"); MS_ABORT("invalid method call");
} }
uint32_t RtpStreamSend::GetLayerBitrate( uint32_t RtpStreamSend::GetLayerBitrate(
uint64_t /*now*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/) uint64_t /*now*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
{ {
MS_TRACE();
MS_ABORT("invalid method call"); MS_ABORT("invalid method call");
} }
......
...@@ -333,18 +333,24 @@ namespace RTC ...@@ -333,18 +333,24 @@ namespace RTC
this->externallyManagedBitrate = true; this->externallyManagedBitrate = true;
} }
int16_t SimulcastConsumer::GetBitratePriority() const uint16_t SimulcastConsumer::GetBitratePriority() const
{ {
MS_TRACE(); MS_TRACE();
if (!RTC::Consumer::IsActive()) if (!RTC::Consumer::IsActive())
return 0; return 0u;
int16_t prioritySpatialLayer{ 0 }; int16_t prioritySpatialLayer{ -1 };
for (size_t sIdx{ 0 }; sIdx < this->producerRtpStreams.size(); ++sIdx) for (size_t sIdx{ 0 }; sIdx < this->producerRtpStreams.size(); ++sIdx)
{ {
auto spatialLayer = static_cast<int16_t>(sIdx); auto spatialLayer = static_cast<int16_t>(sIdx);
// Do not choose a layer greater than the preferred one if we already found
// an available layer equal or less than the preferred one.
if (spatialLayer > this->preferredSpatialLayer && prioritySpatialLayer != -1)
break;
auto* producerRtpStream = this->producerRtpStreams.at(sIdx); auto* producerRtpStream = this->producerRtpStreams.at(sIdx);
// Ignore spatial layers for non existing Producer streams or for those // Ignore spatial layers for non existing Producer streams or for those
...@@ -352,17 +358,18 @@ namespace RTC ...@@ -352,17 +358,18 @@ namespace RTC
if (!producerRtpStream || producerRtpStream->GetScore() == 0) if (!producerRtpStream || producerRtpStream->GetScore() == 0)
continue; continue;
// Do not choose a layer greater than the preferred one if we already found
// an available layer equal or less than the preferred one.
if (spatialLayer > this->preferredSpatialLayer && prioritySpatialLayer >= -1)
break;
// Choose this layer for now. // Choose this layer for now.
prioritySpatialLayer = spatialLayer; prioritySpatialLayer = spatialLayer;
} }
// If no spatial layer was chosen (because all producer streams were inactive),
// we have to return >0 anyway. Otherwise UseAvailableBitrate() won't be called
// and we could never switch to target & current spatial -1.
if (prioritySpatialLayer == -1)
return 1u;
// Return the choosen spatial layer plus one. // Return the choosen spatial layer plus one.
return prioritySpatialLayer + 1; return static_cast<uint16_t>(prioritySpatialLayer + 1);
} }
uint32_t SimulcastConsumer::UseAvailableBitrate(uint32_t bitrate) uint32_t SimulcastConsumer::UseAvailableBitrate(uint32_t bitrate)
......
...@@ -307,33 +307,44 @@ namespace RTC ...@@ -307,33 +307,44 @@ namespace RTC
this->externallyManagedBitrate = true; this->externallyManagedBitrate = true;
} }
int16_t SvcConsumer::GetBitratePriority() const uint16_t SvcConsumer::GetBitratePriority() const
{ {
MS_TRACE(); MS_TRACE();
if (!RTC::Consumer::IsActive()) if (!RTC::Consumer::IsActive())
return 0; return 0u;
// Return a 0 priority if score of Producer stream is 0. // Return a 0 priority if score of producer stream is 0.
if (!this->producerRtpStream || this->producerRtpStream->GetScore() == 0) if (!this->producerRtpStream || this->producerRtpStream->GetScore() == 0)
return 0; return 0u;
auto now = DepLibUV::GetTime();
int16_t prioritySpatialLayer{ -1 };
int16_t spatialLayer{ 0 }; int16_t spatialLayer{ 0 };
int16_t prioritySpatialLayer{ 0 };
for (; spatialLayer < this->producerRtpStream->GetSpatialLayers(); ++spatialLayer) for (; spatialLayer < this->producerRtpStream->GetSpatialLayers(); ++spatialLayer)
{ {
// Do not choose a layer greater than the preferred one if we already found // Do not choose a layer greater than the preferred one if we already found
// an available layer equal or less than the preferred one. // an available layer equal or less than the preferred one.
if (spatialLayer > this->preferredSpatialLayer && prioritySpatialLayer >= -1) if (spatialLayer > this->preferredSpatialLayer && prioritySpatialLayer != -1)
break; break;
// Ignore spatial layers with 0 bitrate.
if (this->producerRtpStream->GetSpatialLayerBitrate(now, spatialLayer) == 0)
continue;
// Choose this layer for now. // Choose this layer for now.
prioritySpatialLayer = spatialLayer; prioritySpatialLayer = spatialLayer;
} }
// If no spatial layer was chosen (because the producer stream was inactive),
// we have to return >0 anyway. Otherwise UseAvailableBitrate() won't be called
// and we could never switch to target & current spatial -1.
if (prioritySpatialLayer == -1)
return 1u;
// Return the choosen spatial layer plus one. // Return the choosen spatial layer plus one.
return prioritySpatialLayer + 1; return static_cast<uint16_t>(prioritySpatialLayer + 1);
} }
uint32_t SvcConsumer::UseAvailableBitrate(uint32_t bitrate) uint32_t SvcConsumer::UseAvailableBitrate(uint32_t bitrate)
...@@ -521,7 +532,7 @@ namespace RTC ...@@ -521,7 +532,7 @@ namespace RTC
continue; continue;
} }
requiredBitrate = producerRtpStream->GetLayerBitrate(now, spatialLayer, temporalLayer); requiredBitrate = this->producerRtpStream->GetLayerBitrate(now, spatialLayer, temporalLayer);
// If active layer, end iterations here. Otherwise move to next spatial layer. // If active layer, end iterations here. Otherwise move to next spatial layer.
if (requiredBitrate) if (requiredBitrate)
...@@ -978,7 +989,7 @@ namespace RTC ...@@ -978,7 +989,7 @@ namespace RTC
for (; spatialLayer < this->producerRtpStream->GetSpatialLayers(); ++spatialLayer) for (; spatialLayer < this->producerRtpStream->GetSpatialLayers(); ++spatialLayer)
{ {
if (!producerRtpStream->GetBitrate(now, spatialLayer, 0)) if (!this->producerRtpStream->GetSpatialLayerBitrate(now, spatialLayer))
continue; continue;
newTargetSpatialLayer = spatialLayer; newTargetSpatialLayer = spatialLayer;
......
...@@ -869,8 +869,8 @@ namespace RTC ...@@ -869,8 +869,8 @@ namespace RTC
// MS_ASSERT(this->rembClient || this->transportCcClient, "no REMB client nor Transport-CC client"); // MS_ASSERT(this->rembClient || this->transportCcClient, "no REMB client nor Transport-CC client");
MS_ASSERT(this->rembClient, "no REMB client"); MS_ASSERT(this->rembClient, "no REMB client");
std::multimap<int16_t, RTC::Consumer*> multimapPriorityConsumer; std::multimap<uint16_t, RTC::Consumer*> multimapPriorityConsumer;
int16_t totalPriorities{ 0 }; uint16_t totalPriorities{ 0 };
// Fill the map with Consumers and their priority (if > 0). // Fill the map with Consumers and their priority (if > 0).
for (auto& kv : this->mapConsumers) for (auto& kv : this->mapConsumers)
...@@ -913,7 +913,7 @@ namespace RTC ...@@ -913,7 +913,7 @@ namespace RTC
MS_DEBUG_TAG( MS_DEBUG_TAG(
bwe, bwe,
"main bitrate for Consumer [priority:%" PRIi16 ", bitrate:%" PRIu32 ", consumerId:%s]", "main bitrate for Consumer [priority:%" PRIu16 ", bitrate:%" PRIu32 ", consumerId:%s]",
priority, priority,
bitrate, bitrate,
consumer->id.c_str()); consumer->id.c_str());
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论