提交 02d6ea96 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

VP8 & H264: improve temporal layer switching (better currentTemporalLayer handling)

上级 4a75900b
...@@ -15,7 +15,8 @@ namespace RTC ...@@ -15,7 +15,8 @@ namespace RTC
virtual void Dump() const = 0; virtual void Dump() const = 0;
}; };
// Encoding context used by PayloadDescriptorHandler to properly rewrite the PayloadDescriptor. // Encoding context used by PayloadDescriptorHandler to properly rewrite the
// PayloadDescriptor.
class EncodingContext class EncodingContext
{ {
public: public:
...@@ -29,11 +30,10 @@ namespace RTC ...@@ -29,11 +30,10 @@ namespace RTC
}; };
public: public:
virtual void SyncRequired() = 0; virtual ~EncodingContext() = default;
virtual void SetPreferences(Preferences preferences);
public: public:
virtual ~EncodingContext() = default; virtual void SyncRequired() = 0;
public: public:
Preferences preferences; Preferences preferences;
...@@ -41,6 +41,9 @@ namespace RTC ...@@ -41,6 +41,9 @@ namespace RTC
class PayloadDescriptorHandler class PayloadDescriptorHandler
{ {
public:
virtual ~PayloadDescriptorHandler() = default;
public: public:
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;
...@@ -48,15 +51,7 @@ namespace RTC ...@@ -48,15 +51,7 @@ namespace RTC
virtual uint8_t GetSpatialLayer() const = 0; virtual uint8_t GetSpatialLayer() const = 0;
virtual uint8_t GetTemporalLayer() const = 0; virtual uint8_t GetTemporalLayer() const = 0;
virtual bool IsKeyFrame() const = 0; virtual bool IsKeyFrame() const = 0;
public:
virtual ~PayloadDescriptorHandler() = default;
}; };
inline void EncodingContext::SetPreferences(Preferences preferences)
{
this->preferences = preferences;
}
} // namespace Codecs } // namespace Codecs
} // namespace RTC } // namespace RTC
......
...@@ -172,26 +172,24 @@ namespace RTC ...@@ -172,26 +172,24 @@ namespace RTC
auto* context = static_cast<RTC::Codecs::H264::EncodingContext*>(encodingContext); auto* context = static_cast<RTC::Codecs::H264::EncodingContext*>(encodingContext);
// Check temporal layer. // If a key frame, update currentTemporalLayer.
if (this->payloadDescriptor->isKeyFrame)
context->currentTemporalLayer = std::numeric_limits<uint8_t>::max();
// TODO: We should check incremental picture id here somehow and let it pass // TODO: We should check incremental picture id here somehow and let it pass
// if so. // if older than highest seen.
// Check temporal layer.
if (this->payloadDescriptor->tid > context->preferences.temporalLayer) if (this->payloadDescriptor->tid > context->preferences.temporalLayer)
{ {
return false; return false;
} }
else if (context->currentTemporalLayer != context->preferences.temporalLayer)
{
// Payload descriptor contains the target temporal layer.
if (this->payloadDescriptor->tid == context->preferences.temporalLayer)
{
// Upgrade required. Drop current packet if base flag is not set. // Upgrade required. Drop current packet if base flag is not set.
// NOTE: It seems that Base Layer Sync flag is never set when in lowest // TODO: Cannot enable this until this issue is fixed (in libwebrtc?):
// simulcast stream. We may need to implement "The Layer Refresh Request // https://github.com/versatica/mediasoup/issues/306
// (LRR) RTCP Feedback Message":
// https://tools.ietf.org/html/draft-ietf-avtext-lrr-07)
// //
// clang-format off // clang-format off
// if ( // else if (
// this->payloadDescriptor->tid > context->currentTemporalLayer && // this->payloadDescriptor->tid > context->currentTemporalLayer &&
// !this->payloadDescriptor->b // !this->payloadDescriptor->b
// ) // )
...@@ -200,9 +198,11 @@ namespace RTC ...@@ -200,9 +198,11 @@ namespace RTC
// return false; // return false;
// } // }
// Update/fix currentTemporalLayer.
if (this->payloadDescriptor->tid > context->currentTemporalLayer)
context->currentTemporalLayer = this->payloadDescriptor->tid; context->currentTemporalLayer = this->payloadDescriptor->tid;
} else if (context->currentTemporalLayer > context->preferences.temporalLayer)
} context->currentTemporalLayer = context->preferences.temporalLayer;
return true; return true;
} }
......
...@@ -232,9 +232,14 @@ namespace RTC ...@@ -232,9 +232,14 @@ namespace RTC
{ {
context->pictureIdManager.Sync(this->payloadDescriptor->pictureId - 1); context->pictureIdManager.Sync(this->payloadDescriptor->pictureId - 1);
context->tl0PictureIndexManager.Sync(this->payloadDescriptor->tl0PictureIndex - 1); context->tl0PictureIndexManager.Sync(this->payloadDescriptor->tl0PictureIndex - 1);
context->syncRequired = false; context->syncRequired = false;
} }
// If a key frame, update currentTemporalLayer.
if (this->payloadDescriptor->isKeyFrame)
context->currentTemporalLayer = std::numeric_limits<uint8_t>::max();
// Incremental pictureId. Check the temporal layer. // Incremental pictureId. Check the temporal layer.
// clang-format off // clang-format off
if ( if (
...@@ -254,14 +259,9 @@ namespace RTC ...@@ -254,14 +259,9 @@ namespace RTC
return false; return false;
} }
else if (context->currentTemporalLayer != context->preferences.temporalLayer)
{
// Payload descriptor contains the target temporal layer.
if (this->payloadDescriptor->tlIndex == context->preferences.temporalLayer)
{
// Upgrade required. Drop current packet if sync flag is not set. // Upgrade required. Drop current packet if sync flag is not set.
// clang-format off // clang-format off
if ( else if (
this->payloadDescriptor->tlIndex > context->currentTemporalLayer && this->payloadDescriptor->tlIndex > context->currentTemporalLayer &&
!this->payloadDescriptor->y !this->payloadDescriptor->y
) )
...@@ -272,10 +272,6 @@ namespace RTC ...@@ -272,10 +272,6 @@ namespace RTC
return false; return false;
} }
context->currentTemporalLayer = this->payloadDescriptor->tlIndex;
}
}
} }
// Update pictureId and tl0PictureIndex values. // Update pictureId and tl0PictureIndex values.
...@@ -284,7 +280,9 @@ namespace RTC ...@@ -284,7 +280,9 @@ namespace RTC
// Do not send a dropped pictureId. // Do not send a dropped pictureId.
if (!context->pictureIdManager.Input(this->payloadDescriptor->pictureId, pictureId)) if (!context->pictureIdManager.Input(this->payloadDescriptor->pictureId, pictureId))
{
return false; return false;
}
// Do not send a dropped tl0PicutreIndex. // Do not send a dropped tl0PicutreIndex.
if (!context->tl0PictureIndexManager.Input( if (!context->tl0PictureIndexManager.Input(
...@@ -293,6 +291,12 @@ namespace RTC ...@@ -293,6 +291,12 @@ namespace RTC
return false; return false;
} }
// Update/fix currentTemporalLayer.
if (this->payloadDescriptor->tlIndex > context->currentTemporalLayer)
context->currentTemporalLayer = this->payloadDescriptor->tlIndex;
else if (context->currentTemporalLayer > context->preferences.temporalLayer)
context->currentTemporalLayer = context->preferences.temporalLayer;
this->payloadDescriptor->Encode(data, pictureId, tl0PictureIndex); this->payloadDescriptor->Encode(data, pictureId, tl0PictureIndex);
return true; return true;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论