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

SimulcastConsumer: change temporal layers based on user preference

上级 48ed07c8
...@@ -46,6 +46,7 @@ namespace RTC ...@@ -46,6 +46,7 @@ namespace RTC
void Dump() const; void Dump() const;
bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data); bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data);
void Restore(uint8_t* data); void Restore(uint8_t* data);
uint8_t GetTemporalLayer() const;
bool IsKeyFrame() const; bool IsKeyFrame() const;
private: private:
...@@ -69,6 +70,11 @@ namespace RTC ...@@ -69,6 +70,11 @@ namespace RTC
{ {
} }
inline uint8_t H264::PayloadDescriptorHandler::GetTemporalLayer() const
{
return 0u;
};
inline bool H264::PayloadDescriptorHandler::IsKeyFrame() const inline bool H264::PayloadDescriptorHandler::IsKeyFrame() const
{ {
return this->payloadDescriptor->isKeyFrame; return this->payloadDescriptor->isKeyFrame;
......
...@@ -45,6 +45,7 @@ namespace RTC ...@@ -45,6 +45,7 @@ namespace RTC
virtual void Dump() const = 0; virtual void Dump() const = 0;
virtual bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data) = 0; virtual bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data) = 0;
virtual void Restore(uint8_t* data) = 0; virtual void Restore(uint8_t* data) = 0;
virtual uint8_t GetTemporalLayer() const = 0;
virtual bool IsKeyFrame() const = 0; virtual bool IsKeyFrame() const = 0;
public: public:
......
...@@ -102,6 +102,7 @@ namespace RTC ...@@ -102,6 +102,7 @@ namespace RTC
void Dump() const; void Dump() const;
bool Encode(RTC::Codecs::EncodingContext* encodingContext, uint8_t* data); bool Encode(RTC::Codecs::EncodingContext* encodingContext, uint8_t* data);
void Restore(uint8_t* data); void Restore(uint8_t* data);
uint8_t GetTemporalLayer() const;
bool IsKeyFrame() const; bool IsKeyFrame() const;
private: private:
...@@ -118,6 +119,14 @@ namespace RTC ...@@ -118,6 +119,14 @@ namespace RTC
/* Inline PayloadDescriptorHandler methods */ /* Inline PayloadDescriptorHandler methods */
inline uint8_t VP8::PayloadDescriptorHandler::GetTemporalLayer() const
{
if (this->payloadDescriptor->hasTlIndex)
return this->payloadDescriptor->tlIndex;
else
return 0u;
};
inline bool VP8::PayloadDescriptorHandler::IsKeyFrame() const inline bool VP8::PayloadDescriptorHandler::IsKeyFrame() const
{ {
return this->payloadDescriptor->isKeyFrame; return this->payloadDescriptor->isKeyFrame;
......
...@@ -137,6 +137,7 @@ namespace RTC ...@@ -137,6 +137,7 @@ namespace RTC
uint8_t* GetPayload() const; uint8_t* GetPayload() const;
size_t GetPayloadLength() const; size_t GetPayloadLength() const;
uint8_t GetPayloadPadding() const; uint8_t GetPayloadPadding() const;
uint8_t GetTemporalLayer() const;
bool IsKeyFrame() const; bool IsKeyFrame() const;
RtpPacket* Clone(const uint8_t* buffer) const; RtpPacket* Clone(const uint8_t* buffer) const;
void RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq); void RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq);
...@@ -500,6 +501,14 @@ namespace RTC ...@@ -500,6 +501,14 @@ namespace RTC
return this->payloadPadding; return this->payloadPadding;
} }
inline uint8_t RtpPacket::GetTemporalLayer() const
{
if (!this->payloadDescriptorHandler)
return 0u;
return this->payloadDescriptorHandler->GetTemporalLayer();
}
inline bool RtpPacket::IsKeyFrame() const inline bool RtpPacket::IsKeyFrame() const
{ {
if (!this->payloadDescriptorHandler) if (!this->payloadDescriptorHandler)
......
...@@ -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 SetCurrentSpatialLayer(int16_t spatialLayer); void UpdateLayers();
void RecalculateTargetSpatialLayer(); void RecalculateTargetSpatialLayer();
RTC::RtpStream* GetProducerCurrentRtpStream() const; RTC::RtpStream* GetProducerCurrentRtpStream() const;
RTC::RtpStream* GetProducerTargetRtpStream() const; RTC::RtpStream* GetProducerTargetRtpStream() const;
...@@ -63,6 +63,9 @@ namespace RTC ...@@ -63,6 +63,9 @@ namespace RTC
int16_t preferredSpatialLayer{ -1 }; int16_t preferredSpatialLayer{ -1 };
int16_t targetSpatialLayer{ -1 }; int16_t targetSpatialLayer{ -1 };
int16_t currentSpatialLayer{ -1 }; int16_t currentSpatialLayer{ -1 };
int16_t preferredTemporalLayer{ -1 };
int16_t targetTemporalLayer{ -1 };
int16_t currentTemporalLayer{ -1 };
}; };
} // namespace RTC } // namespace RTC
......
...@@ -148,10 +148,10 @@ namespace RTC ...@@ -148,10 +148,10 @@ namespace RTC
case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_LAYERS: case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_LAYERS:
{ {
// TODO: Handle temporalLayer.
auto jsonSpatialLayerIt = request->data.find("spatialLayer"); auto jsonSpatialLayerIt = request->data.find("spatialLayer");
auto jsonTemporalLayerIt = request->data.find("temporalLayer");
// Spatial layer.
if (jsonSpatialLayerIt == request->data.end() || !jsonSpatialLayerIt->is_number_unsigned()) if (jsonSpatialLayerIt == request->data.end() || !jsonSpatialLayerIt->is_number_unsigned())
{ {
MS_THROW_TYPE_ERROR("missing spatialLayer"); MS_THROW_TYPE_ERROR("missing spatialLayer");
...@@ -164,18 +164,31 @@ namespace RTC ...@@ -164,18 +164,31 @@ namespace RTC
preferredSpatialLayer = static_cast<int16_t>(this->consumableRtpEncodings.size()) - 1; preferredSpatialLayer = static_cast<int16_t>(this->consumableRtpEncodings.size()) - 1;
} }
if (preferredSpatialLayer == this->preferredSpatialLayer) // Temporal layer.
{ uint16_t preferredTemporalLayer;
request->Accept();
return; // perferredTemporaLayer is optional.
if (jsonTemporalLayerIt != request->data.end() && jsonTemporalLayerIt->is_number_unsigned())
{
preferredTemporalLayer = jsonTemporalLayerIt->get<int16_t>();
}
else
{
preferredTemporalLayer = this->preferredTemporalLayer;
} }
this->preferredSpatialLayer = preferredSpatialLayer; this->preferredSpatialLayer = preferredSpatialLayer;
this->preferredTemporalLayer = preferredTemporalLayer;
this->targetTemporalLayer = preferredTemporalLayer;
if (this->currentSpatialLayer == this->targetSpatialLayer && this->encodingContext)
this->encodingContext->preferences.temporalLayer = this->targetTemporalLayer;
MS_DEBUG_DEV( MS_DEBUG_DEV(
"preferredSpatialLayer changed to %" PRIi16 " [consumerId:%s]", "preferred layers changed to [spatial:%" PRIi16 ", temporal:%" PRIi16 ", consumerId:%s]",
this->preferredSpatialLayer, this->preferredSpatialLayer,
this->preferredTemporalLayer,
this->id.c_str()); this->id.c_str());
if (IsActive()) if (IsActive())
...@@ -305,7 +318,7 @@ namespace RTC ...@@ -305,7 +318,7 @@ namespace RTC
return; return;
// Change current spatial layer. // Change current spatial layer.
SetCurrentSpatialLayer(this->targetSpatialLayer); UpdateLayers();
// Need to resync the stream. // Need to resync the stream.
this->syncRequired = true; this->syncRequired = true;
...@@ -361,6 +374,15 @@ namespace RTC ...@@ -361,6 +374,15 @@ namespace RTC
return; return;
} }
// Update temporal layer only if we are using the target spatial layer.
if (
this->currentSpatialLayer == this->targetSpatialLayer &&
this->currentTemporalLayer != this->targetTemporalLayer &&
packet->GetTemporalLayer() == this->targetTemporalLayer)
{
UpdateLayers();
}
// Update RTP seq number and timestamp. // Update RTP seq number and timestamp.
uint16_t seq; uint16_t seq;
uint32_t timestamp; uint32_t timestamp;
...@@ -657,27 +679,32 @@ namespace RTC ...@@ -657,27 +679,32 @@ namespace RTC
Channel::Notifier::Emit(this->id, "score", data); Channel::Notifier::Emit(this->id, "score", data);
} }
void SimulcastConsumer::SetCurrentSpatialLayer(int16_t spatialLayer) void SimulcastConsumer::UpdateLayers()
{ {
MS_TRACE(); MS_TRACE();
if (this->targetSpatialLayer == this->currentSpatialLayer && this->targetTemporalLayer == this->currentTemporalLayer)
if (spatialLayer == this->currentSpatialLayer)
return; return;
this->currentSpatialLayer = spatialLayer; // Reset the score of our RtpStream to 10 if spatial layer changed.
if (this->targetSpatialLayer != this->currentSpatialLayer)
// Reset the score of our RtpStream to 10.
this->rtpStream->ResetScore(10, false); this->rtpStream->ResetScore(10, false);
MS_DEBUG_TAG( this->currentSpatialLayer = this->targetSpatialLayer;
simulcast, this->currentTemporalLayer = this->targetTemporalLayer;
"current spatial layer changed to %" PRIi16 " [consumerId:%s]",
if (this->encodingContext)
this->encodingContext->preferences.temporalLayer = this->targetTemporalLayer;
MS_DEBUG_DEV(
"current layers changed to [spatial:%" PRIi16 ", temporal:%" PRIi16 ", consumerId:%s]",
this->currentSpatialLayer, this->currentSpatialLayer,
this->currentTemporalLayer,
this->id.c_str()); this->id.c_str());
json data(json::object()); json data(json::object());
data["spatialLayer"] = this->currentSpatialLayer; data["spatialLayer"] = this->currentSpatialLayer;
data["temporalLayer"] = this->currentTemporalLayer;
Channel::Notifier::Emit(this->id, "layerschange", data); Channel::Notifier::Emit(this->id, "layerschange", data);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论