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

EncodingContext: set codec layer preferences

上级 8d4b7e9c
......@@ -596,6 +596,34 @@ class Consumer extends EnhancedEventEmitter
});
}
/**
* Sets the encoding preferences.
*
* @param {String} profile
*/
setEncodingPreferences(preferences)
{
logger.debug('setEncodingPreferences() [preferences:%o]', preferences);
if (this._closed)
{
logger.error('setEncodingPreferences() | Consumer closed');
return;
}
this._channel.request('consumer.setEncodingPreferences', this._internal, preferences)
.then(() =>
{
logger.debug('"consumer.setEncodingPreferences" request succeeded');
})
.catch((error) =>
{
logger.error(
'"consumer.setEncodingPreferences" request failed: %s', String(error));
});
}
/**
* Preferred receiving profile was set on my remote Consumer.
*
......
......@@ -49,6 +49,7 @@ namespace Channel
CONSUMER_PAUSE,
CONSUMER_RESUME,
CONSUMER_SET_PREFERRED_PROFILE,
CONSUMER_SET_ENCODING_PREFERENCES,
CONSUMER_REQUEST_KEY_FRAME
};
......
......@@ -2,6 +2,7 @@
#define MS_RTC_PAYLOAD_DESCRIPTOR_HANDLER_HPP
#include "common.hpp"
#include <limits> // std::numeric_limits
namespace RTC
{
......@@ -17,21 +18,40 @@ namespace RTC
// Encoding context used by PayloadDescriptionHandler to properly rewrite the PayloadDescriptor.
class EncodingContext
{
public:
struct Preferences
{
Preferences() = default;
uint8_t qualityLayer{ std::numeric_limits<uint8_t>::max() };
uint8_t spatialLayer{ std::numeric_limits<uint8_t>::max() };
uint8_t temporalLayer{ std::numeric_limits<uint8_t>::max() };
};
public:
virtual void SyncRequired() = 0;
virtual void SetPreferences(Preferences preferences);
public:
Preferences preferences;
};
class PayloadDescriptorHandler
{
public:
virtual void Dump() const = 0;
virtual void 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 bool IsKeyFrame() const = 0;
public:
virtual ~PayloadDescriptorHandler() = default;
};
inline void EncodingContext::SetPreferences(Preferences preferences)
{
this->preferences = preferences;
}
} // namespace Codecs
} // namespace RTC
......
......@@ -93,7 +93,7 @@ namespace RTC
public:
void Dump() const;
void Encode(RTC::Codecs::EncodingContext* context, uint8_t* data);
bool Encode(RTC::Codecs::EncodingContext* context, uint8_t* data);
void Restore(uint8_t* data);
bool IsKeyFrame() const;
......
......@@ -49,6 +49,7 @@ namespace RTC
void SourceRtpParametersUpdated();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetEncodingPreferences(const RTC::Codecs::EncodingContext::Preferences preferences);
void Disable();
bool IsEnabled() const;
const RTC::RtpParameters& GetParameters() const;
......
......@@ -127,7 +127,7 @@ namespace RTC
void RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq);
bool RtxDecode(uint8_t payloadType, uint32_t ssrc);
void SetPayloadDescriptorHandler(RTC::Codecs::PayloadDescriptorHandler* payloadDescriptorHandler);
void EncodePayload(RTC::Codecs::EncodingContext* context);
bool EncodePayload(RTC::Codecs::EncodingContext* context);
void RestorePayload();
void ShiftPayload(size_t payloadOffset, size_t shift, bool expand = true);
......
......@@ -39,6 +39,8 @@ namespace RTC
void Sync(T input);
void Drop(T input);
bool Input(const T input, T& output);
T GetMaxInput() const;
T GetMaxOutput() const;
private:
T base{ 0 };
......
......@@ -44,6 +44,7 @@ namespace Channel
{ "consumer.pause", Request::MethodId::CONSUMER_PAUSE },
{ "consumer.resume", Request::MethodId::CONSUMER_RESUME },
{ "consumer.setPreferredProfile", Request::MethodId::CONSUMER_SET_PREFERRED_PROFILE },
{ "consumer.setEncodingPreferences", Request::MethodId::CONSUMER_SET_ENCODING_PREFERENCES },
{ "consumer.requestKeyFrame", Request::MethodId::CONSUMER_REQUEST_KEY_FRAME }
};
// clang-format on
......
......@@ -158,7 +158,7 @@ namespace RTC
this->payloadDescriptor.reset(payloadDescriptor);
}
void VP8::PayloadDescriptorHandler::Encode(
bool VP8::PayloadDescriptorHandler::Encode(
RTC::Codecs::EncodingContext* encodingContext, uint8_t* data)
{
EncodingContext* context = dynamic_cast<EncodingContext*>(encodingContext);
......@@ -172,14 +172,35 @@ namespace RTC
context->syncRequired = false;
}
// Incremental pictureId. Check the temporal layer.
if (RTC::SeqManager<uint16_t>::IsSeqHigherThan(
this->payloadDescriptor->pictureId, context->pictureIdManager.GetMaxInput()))
{
if (this->payloadDescriptor->t && this->payloadDescriptor->tlIndex > context->preferences.temporalLayer)
{
context->pictureIdManager.Drop(this->payloadDescriptor->pictureId);
context->tl0PictureIndexManager.Drop(this->payloadDescriptor->tl0PictureIndex);
return false;
}
}
// Update pictureId and tl0PictureIndex values.
uint16_t pictureId;
uint8_t tl0PictureIndex;
context->pictureIdManager.Input(this->payloadDescriptor->pictureId, pictureId);
context->tl0PictureIndexManager.Input(this->payloadDescriptor->tl0PictureIndex, tl0PictureIndex);
// Do not send a dropped pictureId.
if (!context->pictureIdManager.Input(this->payloadDescriptor->pictureId, pictureId))
return false;
// Do not send a dropped tl0PicutreIndex.
if (!context->tl0PictureIndexManager.Input(
this->payloadDescriptor->tl0PictureIndex, tl0PictureIndex))
return false;
this->payloadDescriptor->Encode(data, pictureId, tl0PictureIndex);
return true;
};
void VP8::PayloadDescriptorHandler::Restore(uint8_t* data)
......
......@@ -372,6 +372,24 @@ namespace RTC
RecalculateTargetProfile(true /*forcePreferred*/);
}
void Consumer::SetEncodingPreferences(const RTC::Codecs::EncodingContext::Preferences preferences)
{
MS_TRACE();
if (!this->encodingContext)
return;
MS_DEBUG_TAG(
rtp,
"setting encoding preferences [qualityLayer:%" PRIu8 ", spatialLayer:%" PRIu8
", temporalLayer:%" PRIu8 "]",
preferences.qualityLayer,
preferences.spatialLayer,
preferences.temporalLayer);
this->encodingContext->SetPreferences(preferences);
}
/**
* Called when the Transport assigned to this Consumer has been closed, so this
* Consumer becomes unhandled.
......@@ -477,6 +495,15 @@ namespace RTC
this->encodingContext->SyncRequired();
}
// Rewrite payload if needed. Drop packet if necessary.
if (this->encodingContext && !packet->EncodePayload(this->encodingContext.get()))
{
this->rtpSeqManager.Drop(packet->GetSequenceNumber());
this->rtpTimestampManager.Drop(packet->GetTimestamp());
return;
}
// Update RTP seq number and timestamp.
uint16_t rtpSeq;
uint32_t rtpTimestamp;
......@@ -502,10 +529,6 @@ namespace RTC
// Rewrite packet timestamp.
packet->SetTimestamp(rtpTimestamp);
// Rewrite payload if needed.
if (this->encodingContext)
packet->EncodePayload(this->encodingContext.get());
if (isSyncPacket)
{
MS_DEBUG_TAG(
......
......@@ -1343,6 +1343,41 @@ namespace RTC
break;
}
case Channel::Request::MethodId::CONSUMER_SET_ENCODING_PREFERENCES:
{
static const Json::StaticString JsonStringQualityLayer{ "qualityLayer" };
static const Json::StaticString JsonStringSpatialLayer{ "spatialLayer" };
static const Json::StaticString JsonStringTemporalLayer{ "temporalLayer" };
RTC::Consumer* consumer;
try
{
consumer = GetConsumerFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
RTC::Codecs::EncodingContext::Preferences preferences;
if (request->data[JsonStringQualityLayer].isUInt())
preferences.qualityLayer = request->data[JsonStringQualityLayer].asUInt();
if (request->data[JsonStringSpatialLayer].isUInt())
preferences.spatialLayer = request->data[JsonStringSpatialLayer].asUInt();
if (request->data[JsonStringTemporalLayer].isUInt())
preferences.temporalLayer = request->data[JsonStringTemporalLayer].asUInt();
consumer->SetEncodingPreferences(preferences);
request->Accept();
break;
}
case Channel::Request::MethodId::CONSUMER_REQUEST_KEY_FRAME:
{
RTC::Consumer* consumer;
......
......@@ -456,14 +456,14 @@ namespace RTC
return true;
}
void RtpPacket::EncodePayload(RTC::Codecs::EncodingContext* context)
bool RtpPacket::EncodePayload(RTC::Codecs::EncodingContext* context)
{
MS_TRACE();
if (!this->payloadDescriptorHandler)
return;
return true;
this->payloadDescriptorHandler->Encode(context, this->payload);
return this->payloadDescriptorHandler->Encode(context, this->payload);
}
void RtpPacket::RestorePayload()
......
......@@ -111,6 +111,18 @@ namespace RTC
return true;
}
template<typename T>
T SeqManager<T>::GetMaxInput() const
{
return this->maxInput;
}
template<typename T>
T SeqManager<T>::GetMaxOutput() const
{
return this->maxOutput;
}
// Explicit instantiation to have all SeqManager definitions in this file.
template class SeqManager<uint8_t>;
template class SeqManager<uint16_t>;
......
......@@ -265,6 +265,7 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_PROFILE:
case Channel::Request::MethodId::CONSUMER_SET_ENCODING_PREFERENCES:
case Channel::Request::MethodId::CONSUMER_REQUEST_KEY_FRAME:
{
RTC::Router* router;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论