提交 529538ae authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Improve SimulcastConsumer::RecalculateTargetSpatialLayer()

上级 54dde57d
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include "RTC/RtpHeaderExtensionIds.hpp" #include "RTC/RtpHeaderExtensionIds.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
#include "RTC/RtpStreamRecv.hpp" #include "RTC/RtpStream.hpp"
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
...@@ -55,9 +55,9 @@ namespace RTC ...@@ -55,9 +55,9 @@ namespace RTC
virtual uint32_t UseBandwidth(uint32_t availableBandwidth) = 0; virtual uint32_t UseBandwidth(uint32_t availableBandwidth) = 0;
void ProducerPaused(); void ProducerPaused();
void ProducerResumed(); void ProducerResumed();
virtual void ProducerRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) = 0; virtual void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) = 0;
virtual void ProducerNewRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) = 0; virtual void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) = 0;
virtual void ProducerRtpStreamScore(RTC::RtpStreamRecv* rtpStream, uint8_t score) = 0; virtual void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) = 0;
void ProducerClosed(); void ProducerClosed();
virtual void SendRtpPacket(RTC::RtpPacket* packet) = 0; virtual void SendRtpPacket(RTC::RtpPacket* packet) = 0;
virtual void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) = 0; virtual void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) = 0;
......
...@@ -18,9 +18,9 @@ namespace RTC ...@@ -18,9 +18,9 @@ namespace RTC
void FillJsonScore(json& jsonObject) const override; void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override; void HandleRequest(Channel::Request* request) override;
uint32_t UseBandwidth(uint32_t availableBandwidth) override; uint32_t UseBandwidth(uint32_t availableBandwidth) override;
void ProducerRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStreamRecv* rtpStream, uint8_t score) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override;
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override; void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override; void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
......
...@@ -27,9 +27,9 @@ namespace RTC ...@@ -27,9 +27,9 @@ namespace RTC
virtual void OnProducerPaused(RTC::Producer* producer) = 0; virtual void OnProducerPaused(RTC::Producer* producer) = 0;
virtual void OnProducerResumed(RTC::Producer* producer) = 0; virtual void OnProducerResumed(RTC::Producer* producer) = 0;
virtual void OnProducerNewRtpStream( virtual void OnProducerNewRtpStream(
RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) = 0; RTC::Producer* producer, RTC::RtpStream* rtpStream, uint32_t mappedSsrc) = 0;
virtual void OnProducerRtpStreamScore( virtual void OnProducerRtpStreamScore(
RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint8_t score) = 0; RTC::Producer* producer, RTC::RtpStream* rtpStream, uint8_t score) = 0;
virtual void OnProducerRtpPacketReceived(RTC::Producer* producer, RTC::RtpPacket* packet) = 0; virtual void OnProducerRtpPacketReceived(RTC::Producer* producer, RTC::RtpPacket* packet) = 0;
virtual void OnProducerSendRtcpPacket(RTC::Producer* producer, RTC::RTCP::Packet* packet) = 0; virtual void OnProducerSendRtcpPacket(RTC::Producer* producer, RTC::RTCP::Packet* packet) = 0;
virtual void OnProducerNeedWorstRemoteFractionLost( virtual void OnProducerNeedWorstRemoteFractionLost(
......
...@@ -44,7 +44,7 @@ namespace RTC ...@@ -44,7 +44,7 @@ namespace RTC
void OnTransportProducerNewRtpStream( void OnTransportProducerNewRtpStream(
RTC::Transport* transport, RTC::Transport* transport,
RTC::Producer* producer, RTC::Producer* producer,
RTC::RtpStreamRecv* rtpStream, RTC::RtpStream* rtpStream,
uint32_t mappedSsrc) override; uint32_t mappedSsrc) override;
void OnTransportProducerRtpPacketReceived( void OnTransportProducerRtpPacketReceived(
RTC::Transport* transport, RTC::Producer* producer, RTC::RtpPacket* packet) override; RTC::Transport* transport, RTC::Producer* producer, RTC::RtpPacket* packet) override;
...@@ -56,7 +56,7 @@ namespace RTC ...@@ -56,7 +56,7 @@ namespace RTC
void OnTransportProducerRtpStreamScore( void OnTransportProducerRtpStreamScore(
RTC::Transport* transport, RTC::Transport* transport,
RTC::Producer* producer, RTC::Producer* producer,
RTC::RtpStreamRecv* rtpStream, RTC::RtpStream* rtpStream,
uint8_t score) override; uint8_t score) override;
void OnTransportNewConsumer( void OnTransportNewConsumer(
RTC::Transport* transport, RTC::Consumer* consumer, std::string& producerId) override; RTC::Transport* transport, RTC::Consumer* consumer, std::string& producerId) override;
......
...@@ -19,9 +19,9 @@ namespace RTC ...@@ -19,9 +19,9 @@ namespace RTC
void FillJsonScore(json& jsonObject) const override; void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override; void HandleRequest(Channel::Request* request) override;
uint32_t UseBandwidth(uint32_t availableBandwidth) override; uint32_t UseBandwidth(uint32_t availableBandwidth) override;
void ProducerRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStreamRecv* rtpStream, uint8_t score) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override;
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override; void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override; void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
......
...@@ -20,9 +20,9 @@ namespace RTC ...@@ -20,9 +20,9 @@ namespace RTC
void FillJsonScore(json& jsonObject) const override; void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override; void HandleRequest(Channel::Request* request) override;
uint32_t UseBandwidth(uint32_t availableBandwidth) override; uint32_t UseBandwidth(uint32_t availableBandwidth) override;
void ProducerRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStreamRecv* rtpStream, uint8_t score) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override;
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override; void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override; void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
...@@ -39,7 +39,7 @@ namespace RTC ...@@ -39,7 +39,7 @@ namespace RTC
void RequestKeyFrame(); void RequestKeyFrame();
void RetransmitRtpPacket(RTC::RtpPacket* packet); void RetransmitRtpPacket(RTC::RtpPacket* packet);
void EmitScore() const; void EmitScore() const;
void UpdateLayers(); void UpdateCurrentLayers();
void RecalculateTargetSpatialLayer(); void RecalculateTargetSpatialLayer();
RTC::RtpStream* GetProducerCurrentRtpStream() const; RTC::RtpStream* GetProducerCurrentRtpStream() const;
RTC::RtpStream* GetProducerTargetRtpStream() const; RTC::RtpStream* GetProducerTargetRtpStream() const;
......
...@@ -35,12 +35,12 @@ namespace RTC ...@@ -35,12 +35,12 @@ namespace RTC
virtual void OnTransportProducerNewRtpStream( virtual void OnTransportProducerNewRtpStream(
RTC::Transport* transport, RTC::Transport* transport,
RTC::Producer* producer, RTC::Producer* producer,
RTC::RtpStreamRecv* rtpStream, RTC::RtpStream* rtpStream,
uint32_t mappedSsrc) = 0; uint32_t mappedSsrc) = 0;
virtual void OnTransportProducerRtpStreamScore( virtual void OnTransportProducerRtpStreamScore(
RTC::Transport* transport, RTC::Transport* transport,
RTC::Producer* producer, RTC::Producer* producer,
RTC::RtpStreamRecv* rtpStream, RTC::RtpStream* rtpStream,
uint8_t score) = 0; uint8_t score) = 0;
virtual void OnTransportProducerRtpPacketReceived( virtual void OnTransportProducerRtpPacketReceived(
RTC::Transport* transport, RTC::Producer* producer, RTC::RtpPacket* packet) = 0; RTC::Transport* transport, RTC::Producer* producer, RTC::RtpPacket* packet) = 0;
...@@ -101,9 +101,9 @@ namespace RTC ...@@ -101,9 +101,9 @@ namespace RTC
void OnProducerPaused(RTC::Producer* producer) override; void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override; void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerNewRtpStream( void OnProducerNewRtpStream(
RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) override; RTC::Producer* producer, RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void OnProducerRtpStreamScore( void OnProducerRtpStreamScore(
RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint8_t score) override; RTC::Producer* producer, RTC::RtpStream* rtpStream, uint8_t score) override;
void OnProducerRtpPacketReceived(RTC::Producer* producer, RTC::RtpPacket* packet) override; void OnProducerRtpPacketReceived(RTC::Producer* producer, RTC::RtpPacket* packet) override;
void OnProducerSendRtcpPacket(RTC::Producer* producer, RTC::RTCP::Packet* packet) override; void OnProducerSendRtcpPacket(RTC::Producer* producer, RTC::RTCP::Packet* packet) override;
void OnProducerNeedWorstRemoteFractionLost( void OnProducerNeedWorstRemoteFractionLost(
......
...@@ -90,21 +90,21 @@ namespace RTC ...@@ -90,21 +90,21 @@ namespace RTC
return 0; return 0;
} }
void PipeConsumer::ProducerRtpStream(RTC::RtpStreamRecv* /*rtpStream*/, uint32_t /*mappedSsrc*/) void PipeConsumer::ProducerRtpStream(RTC::RtpStream* /*rtpStream*/, uint32_t /*mappedSsrc*/)
{ {
MS_TRACE(); MS_TRACE();
// Do nothing. // Do nothing.
} }
void PipeConsumer::ProducerNewRtpStream(RTC::RtpStreamRecv* /*rtpStream*/, uint32_t /*mappedSsrc*/) void PipeConsumer::ProducerNewRtpStream(RTC::RtpStream* /*rtpStream*/, uint32_t /*mappedSsrc*/)
{ {
MS_TRACE(); MS_TRACE();
// Do nothing. // Do nothing.
} }
void PipeConsumer::ProducerRtpStreamScore(RTC::RtpStreamRecv* /*rtpStream*/, uint8_t /*score*/) void PipeConsumer::ProducerRtpStreamScore(RTC::RtpStream* /*rtpStream*/, uint8_t /*score*/)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -776,7 +776,8 @@ namespace RTC ...@@ -776,7 +776,8 @@ namespace RTC
rtpStream->Pause(); rtpStream->Pause();
// Notify to the listener. // Notify to the listener.
this->listener->OnProducerNewRtpStream(this, rtpStream, encodingMapping.mappedSsrc); this->listener->OnProducerNewRtpStream(
this, static_cast<RTC::RtpStream*>(rtpStream), encodingMapping.mappedSsrc);
// Request a key frame for this stream since we may have lost the first packets // Request a key frame for this stream since we may have lost the first packets
// (do not do it if this is a key frame). // (do not do it if this is a key frame).
...@@ -983,7 +984,7 @@ namespace RTC ...@@ -983,7 +984,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// Notify the listener. // Notify the listener.
this->listener->OnProducerRtpStreamScore(this, static_cast<RTC::RtpStreamRecv*>(rtpStream), score); this->listener->OnProducerRtpStreamScore(this, rtpStream, score);
// Emit the score event. // Emit the score event.
EmitScore(); EmitScore();
......
...@@ -549,10 +549,7 @@ namespace RTC ...@@ -549,10 +549,7 @@ namespace RTC
} }
inline void Router::OnTransportProducerNewRtpStream( inline void Router::OnTransportProducerNewRtpStream(
RTC::Transport* /*transport*/, RTC::Transport* /*transport*/, RTC::Producer* producer, RTC::RtpStream* rtpStream, uint32_t mappedSsrc)
RTC::Producer* producer,
RTC::RtpStreamRecv* rtpStream,
uint32_t mappedSsrc)
{ {
MS_TRACE(); MS_TRACE();
...@@ -565,7 +562,7 @@ namespace RTC ...@@ -565,7 +562,7 @@ namespace RTC
} }
inline void Router::OnTransportProducerRtpStreamScore( inline void Router::OnTransportProducerRtpStreamScore(
RTC::Transport* /*transport*/, RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint8_t score) RTC::Transport* /*transport*/, RTC::Producer* producer, RTC::RtpStream* rtpStream, uint8_t score)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -109,7 +109,7 @@ namespace RTC ...@@ -109,7 +109,7 @@ namespace RTC
return 0; return 0;
} }
void SimpleConsumer::ProducerRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t /*mappedSsrc*/) void SimpleConsumer::ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t /*mappedSsrc*/)
{ {
MS_TRACE(); MS_TRACE();
...@@ -119,7 +119,7 @@ namespace RTC ...@@ -119,7 +119,7 @@ namespace RTC
EmitScore(); EmitScore();
} }
void SimpleConsumer::ProducerNewRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t /*mappedSsrc*/) void SimpleConsumer::ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t /*mappedSsrc*/)
{ {
MS_TRACE(); MS_TRACE();
...@@ -136,7 +136,7 @@ namespace RTC ...@@ -136,7 +136,7 @@ namespace RTC
EmitScore(); EmitScore();
} }
void SimpleConsumer::ProducerRtpStreamScore(RTC::RtpStreamRecv* /*rtpStream*/, uint8_t /*score*/) void SimpleConsumer::ProducerRtpStreamScore(RTC::RtpStream* /*rtpStream*/, uint8_t /*score*/)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -62,6 +62,10 @@ namespace RTC ...@@ -62,6 +62,10 @@ namespace RTC
if (this->preferredTemporalLayer > encoding.temporalLayers - 1) if (this->preferredTemporalLayer > encoding.temporalLayers - 1)
this->preferredTemporalLayer = encoding.temporalLayers - 1; this->preferredTemporalLayer = encoding.temporalLayers - 1;
} }
else
{
this->preferredTemporalLayer = encoding.temporalLayers - 1;
}
} }
else else
{ {
...@@ -179,31 +183,31 @@ namespace RTC ...@@ -179,31 +183,31 @@ namespace RTC
MS_THROW_TYPE_ERROR("missing spatialLayer"); MS_THROW_TYPE_ERROR("missing spatialLayer");
} }
auto preferredSpatialLayer = jsonSpatialLayerIt->get<int16_t>(); this->preferredSpatialLayer = jsonSpatialLayerIt->get<int16_t>();
if (preferredSpatialLayer > this->rtpStream->GetSpatialLayers() - 1)
preferredSpatialLayer = this->rtpStream->GetSpatialLayers() - 1;
// Temporal layer. if (this->preferredSpatialLayer > this->rtpStream->GetSpatialLayers() - 1)
uint16_t preferredTemporalLayer; this->preferredSpatialLayer = this->rtpStream->GetSpatialLayers() - 1;
// perferredTemporaLayer is optional. // preferredTemporaLayer is optional.
if (jsonTemporalLayerIt != request->data.end() && jsonTemporalLayerIt->is_number_unsigned()) if (jsonTemporalLayerIt != request->data.end() && jsonTemporalLayerIt->is_number_unsigned())
{ {
preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>(); this->preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>();
if (this->preferredTemporalLayer > this->rtpStream->GetTemporalLayers() - 1)
this->preferredTemporalLayer = this->rtpStream->GetTemporalLayers() - 1;
} }
else else
{ {
preferredTemporalLayer = this->preferredTemporalLayer; this->preferredTemporalLayer = this->rtpStream->GetTemporalLayers() - 1;
} }
this->preferredSpatialLayer = preferredSpatialLayer; if (this->currentSpatialLayer == this->preferredSpatialLayer)
this->preferredTemporalLayer = preferredTemporalLayer; {
this->targetTemporalLayer = this->preferredTemporalLayer;
this->targetTemporalLayer = preferredTemporalLayer;
if (this->currentSpatialLayer == this->targetSpatialLayer && this->encodingContext) if (this->encodingContext)
this->encodingContext->preferences.temporalLayer = this->targetTemporalLayer; this->encodingContext->preferences.temporalLayer = this->targetTemporalLayer;
}
MS_DEBUG_DEV( MS_DEBUG_DEV(
"preferred layers changed to [spatial:%" PRIi16 ", temporal:%" PRIi16 ", consumerId:%s]", "preferred layers changed to [spatial:%" PRIi16 ", temporal:%" PRIi16 ", consumerId:%s]",
...@@ -242,7 +246,7 @@ namespace RTC ...@@ -242,7 +246,7 @@ namespace RTC
return 0; return 0;
} }
void SimulcastConsumer::ProducerRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) void SimulcastConsumer::ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc)
{ {
MS_TRACE(); MS_TRACE();
...@@ -258,7 +262,7 @@ namespace RTC ...@@ -258,7 +262,7 @@ namespace RTC
EmitScore(); EmitScore();
} }
void SimulcastConsumer::ProducerNewRtpStream(RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) void SimulcastConsumer::ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc)
{ {
MS_TRACE(); MS_TRACE();
...@@ -296,7 +300,7 @@ namespace RTC ...@@ -296,7 +300,7 @@ namespace RTC
EmitScore(); EmitScore();
} }
void SimulcastConsumer::ProducerRtpStreamScore(RTC::RtpStreamRecv* /*rtpStream*/, uint8_t /*score*/) void SimulcastConsumer::ProducerRtpStreamScore(RTC::RtpStream* /*rtpStream*/, uint8_t /*score*/)
{ {
MS_TRACE(); MS_TRACE();
...@@ -338,7 +342,7 @@ namespace RTC ...@@ -338,7 +342,7 @@ namespace RTC
return; return;
// Change current spatial layer. // Change current spatial layer.
UpdateLayers(); UpdateCurrentLayers();
// Need to resync the stream. // Need to resync the stream.
this->syncRequired = true; this->syncRequired = true;
...@@ -382,9 +386,6 @@ namespace RTC ...@@ -382,9 +386,6 @@ namespace RTC
this->syncRequired = false; this->syncRequired = false;
} }
// TODO: Not sure how to deal with it, but if this happens (and we drop the packet)
// we shouldn't have unset the syncRequired flag, etc.
//
// Rewrite payload if needed. Drop packet if necessary. // Rewrite payload if needed. Drop packet if necessary.
if (this->encodingContext && !packet->EncodePayload(this->encodingContext.get())) if (this->encodingContext && !packet->EncodePayload(this->encodingContext.get()))
{ {
...@@ -394,13 +395,12 @@ namespace RTC ...@@ -394,13 +395,12 @@ namespace RTC
return; return;
} }
// Update temporal layer only if we are using the target spatial layer. // Update temporal layer only if we are sending the target spatial layer.
if ( if (
this->currentSpatialLayer == this->targetSpatialLayer &&
this->currentTemporalLayer != this->targetTemporalLayer && this->currentTemporalLayer != this->targetTemporalLayer &&
packet->GetTemporalLayer() == this->targetTemporalLayer) packet->GetTemporalLayer() == this->targetTemporalLayer)
{ {
UpdateLayers(); UpdateCurrentLayers();
} }
// Update RTP seq number and timestamp. // Update RTP seq number and timestamp.
...@@ -561,8 +561,17 @@ namespace RTC ...@@ -561,8 +561,17 @@ namespace RTC
this->rtpStream->Pause(); this->rtpStream->Pause();
// Unset current and target layers. // Unset current and target layers.
this->targetSpatialLayer = -1; this->targetSpatialLayer = -1;
this->currentSpatialLayer = -1; this->currentSpatialLayer = -1;
this->targetTemporalLayer = -1;
this->currentTemporalLayer = -1;
if (this->encodingContext)
{
this->encodingContext->preferences.temporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
// TODO: Emit JS event?
} }
void SimulcastConsumer::Resumed() void SimulcastConsumer::Resumed()
...@@ -702,16 +711,31 @@ namespace RTC ...@@ -702,16 +711,31 @@ namespace RTC
Channel::Notifier::Emit(this->id, "score", data); Channel::Notifier::Emit(this->id, "score", data);
} }
void SimulcastConsumer::UpdateLayers() void SimulcastConsumer::UpdateCurrentLayers()
{ {
MS_TRACE(); MS_TRACE();
if (this->targetSpatialLayer == this->currentSpatialLayer && this->targetTemporalLayer == this->currentTemporalLayer)
if (this->currentSpatialLayer == this->targetSpatialLayer && this->currentTemporalLayer == this->targetTemporalLayer)
{
return; return;
}
// Reset the score of our RtpStream to 10 if spatial layer changed. // Reset the score of our RtpStream to 10 if spatial layer changed.
if (this->targetSpatialLayer != this->currentSpatialLayer) if (this->targetSpatialLayer != this->currentSpatialLayer)
this->rtpStream->ResetScore(10, false); this->rtpStream->ResetScore(10, false);
// If the new current spatial layer is the preferred one honor the preferred
// temporal layer.
if (this->targetSpatialLayer == this->preferredSpatialLayer)
{
this->targetTemporalLayer = this->preferredTemporalLayer;
}
// Otherwise, choose the highest temporal layer.
else
{
this->targetTemporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
this->currentSpatialLayer = this->targetSpatialLayer; this->currentSpatialLayer = this->targetSpatialLayer;
this->currentTemporalLayer = this->targetTemporalLayer; this->currentTemporalLayer = this->targetTemporalLayer;
...@@ -739,128 +763,29 @@ namespace RTC ...@@ -739,128 +763,29 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
uint8_t maxProducerScore{ 0 };
int16_t newTargetSpatialLayer{ -1 }; int16_t newTargetSpatialLayer{ -1 };
// No current spatial layer, select the highest possible. for (size_t idx = 0; idx < this->producerRtpStreams.size(); ++idx)
if (this->currentSpatialLayer == -1)
{ {
MS_DEBUG_TAG(simulcast, "no current spatial layer, selecting the highest possible"); auto spatialLayer = static_cast<int16_t>(idx);
auto* producerRtpStream = this->producerRtpStreams[idx];
auto producerScore = producerRtpStream ? producerRtpStream->GetScore() : 0;
uint8_t maxScore = 0; // Ignore spatial layers for non existing Producer streams or for those
// with score 0.
if (producerScore == 0)
continue;
// Ignore spatial layers higher than the preferred one. if (producerScore >= maxProducerScore)
for (int idx = this->preferredSpatialLayer; idx >= 0; --idx)
{ {
auto spatialLayer = static_cast<int16_t>(idx); maxProducerScore = producerScore;
auto* producerRtpStream = this->producerRtpStreams[idx]; newTargetSpatialLayer = spatialLayer;
// Ignore spatial layers for non existing Producer streams.
if (!producerRtpStream)
continue;
if (producerRtpStream->GetScore() >= maxScore)
{
maxScore = producerRtpStream->GetScore();
newTargetSpatialLayer = spatialLayer;
if (maxScore > 7)
{
MS_DEBUG_TAG(simulcast, "found Producer RtpStream with score > 7, keep it");
break;
}
}
}
}
// Downgrade is desired.
else if (this->preferredSpatialLayer < this->currentSpatialLayer)
{
MS_DEBUG_TAG(simulcast, "trying to downgrade current spatial layer to preferred one");
int16_t idx = this->preferredSpatialLayer;
uint8_t maxScore = 0;
for (; idx < this->currentSpatialLayer; ++idx)
{
auto spatialLayer = static_cast<int16_t>(idx);
auto* producerRtpStream = this->producerRtpStreams[idx];
// Ignore spatial layers for non existing Producer streams.
if (!producerRtpStream)
continue;
if (producerRtpStream->GetScore() >= maxScore)
{
maxScore = producerRtpStream->GetScore();
newTargetSpatialLayer = spatialLayer;
if (maxScore > 7)
{
MS_DEBUG_TAG(simulcast, "found Producer RtpStream with score > 7, keep it");
break;
}
}
}
}
// Downgrade is needed.
else if (
!this->GetProducerCurrentRtpStream() || this->GetProducerCurrentRtpStream()->GetScore() < 7 ||
this->rtpStream->GetScore() <= 6)
{
MS_DEBUG_TAG(simulcast, "trying to downgrade current spatial layer");
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;
if (producerRtpStream->GetScore() >= maxScore)
{
maxScore = producerRtpStream->GetScore();
newTargetSpatialLayer = spatialLayer;
if (maxScore > 7)
{
MS_DEBUG_TAG(simulcast, "found Producer RtpStream with score > 7, keep it");
break;
}
}
}
}
// Update to the highest possible spatial layer.
else
{
MS_DEBUG_TAG(simulcast, "trying to upgrade current spatial layer");
int16_t idx = this->currentSpatialLayer == -1 ? 0 : this->currentSpatialLayer + 1;
// Ignore spatial layers above the preferred one.
for (; idx <= this->preferredSpatialLayer; ++idx)
{
auto spatialLayer = idx;
auto* producerRtpStream = this->producerRtpStreams[idx];
// 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)
{
MS_DEBUG_TAG(simulcast, "good enough spatial layer found: %" PRIi16, spatialLayer);
newTargetSpatialLayer = spatialLayer;
// If this is the preferred or higher spatial layer and has good score,
// take it and exit.
if (spatialLayer >= this->preferredSpatialLayer && maxProducerScore >= 7)
break; break;
}
} }
} }
...@@ -872,7 +797,20 @@ namespace RTC ...@@ -872,7 +797,20 @@ namespace RTC
if (newTargetSpatialLayer == -1) if (newTargetSpatialLayer == -1)
{ {
MS_DEBUG_TAG(simulcast, "newTargetSpatialLayer remains unset"); MS_DEBUG_TAG(simulcast, "no valid spatial layer found");
// Unset current and target layers.
this->targetSpatialLayer = -1;
this->currentSpatialLayer = -1;
this->targetTemporalLayer = -1;
this->currentTemporalLayer = -1;
if (this->encodingContext)
{
this->encodingContext->preferences.temporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
// TODO: Emit JS event?
return; return;
} }
...@@ -883,17 +821,17 @@ namespace RTC ...@@ -883,17 +821,17 @@ namespace RTC
this->targetSpatialLayer = newTargetSpatialLayer; this->targetSpatialLayer = newTargetSpatialLayer;
// Already using the target layer. Do nothing.
if (this->targetSpatialLayer == this->currentSpatialLayer)
return;
RequestKeyFrame();
MS_DEBUG_TAG( MS_DEBUG_TAG(
simulcast, simulcast,
"target spatial layer changed to %" PRIi16 " [consumerId:%s]", "target spatial layer changed to %" PRIi16 " [consumerId:%s]",
this->targetSpatialLayer, this->targetSpatialLayer,
this->id.c_str()); this->id.c_str());
// Already using the target layer. Do nothing.
if (this->targetSpatialLayer == this->currentSpatialLayer)
return;
RequestKeyFrame();
} }
inline RTC::RtpStream* SimulcastConsumer::GetProducerCurrentRtpStream() const inline RTC::RtpStream* SimulcastConsumer::GetProducerCurrentRtpStream() const
......
...@@ -826,7 +826,7 @@ namespace RTC ...@@ -826,7 +826,7 @@ namespace RTC
} }
inline void Transport::OnProducerNewRtpStream( inline void Transport::OnProducerNewRtpStream(
RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint32_t mappedSsrc) RTC::Producer* producer, RTC::RtpStream* rtpStream, uint32_t mappedSsrc)
{ {
MS_TRACE(); MS_TRACE();
...@@ -834,7 +834,7 @@ namespace RTC ...@@ -834,7 +834,7 @@ namespace RTC
} }
inline void Transport::OnProducerRtpStreamScore( inline void Transport::OnProducerRtpStreamScore(
RTC::Producer* producer, RTC::RtpStreamRecv* rtpStream, uint8_t score) RTC::Producer* producer, RTC::RtpStream* rtpStream, uint8_t score)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -51,6 +51,10 @@ public: ...@@ -51,6 +51,10 @@ public:
{ {
return this->isKeyFrame; return this->isKeyFrame;
}; };
uint8_t GetTemporalLayer() const
{
return 0;
};
private: private:
bool isKeyFrame{ false }; bool isKeyFrame{ false };
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论