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

Codecs: PayloadDescriptorHandler to rewrite the PayloadDescriptor

上级 f61437a6
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "common.hpp" #include "common.hpp"
#include "RTC/Codecs/VP8.hpp" #include "RTC/Codecs/VP8.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
...@@ -12,6 +13,7 @@ namespace RTC ...@@ -12,6 +13,7 @@ namespace RTC
{ {
bool CanBeKeyFrame(const RTC::RtpCodecMimeType& mimeType); bool CanBeKeyFrame(const RTC::RtpCodecMimeType& mimeType);
void ProcessRtpPacket(RTC::RtpPacket* packet, const RTC::RtpCodecMimeType& mimeType); void ProcessRtpPacket(RTC::RtpPacket* packet, const RTC::RtpCodecMimeType& mimeType);
EncodingContext* GetEncodingContext(const RTC::RtpCodecMimeType& mimeType);
// Inline namespace methods. // Inline namespace methods.
...@@ -28,6 +30,20 @@ namespace RTC ...@@ -28,6 +30,20 @@ namespace RTC
return false; return false;
} }
} }
inline EncodingContext* GetEncodingContext(const RTC::RtpCodecMimeType& mimeType)
{
if (mimeType.type != RTC::RtpCodecMimeType::Type::VIDEO)
return nullptr;
switch (mimeType.subtype)
{
case RTC::RtpCodecMimeType::Subtype::VP8:
return new Codecs::VP8::EncodingContext();
default:
return nullptr;
}
}
} // namespace Codecs } // namespace Codecs
} // namespace RTC } // namespace RTC
......
#ifndef MS_RTC_PAYLOAD_DESCRIPTOR_HANDLER_HPP
#define MS_RTC_PAYLOAD_DESCRIPTOR_HANDLER_HPP
#include "common.hpp"
namespace RTC
{
namespace Codecs
{
// Codec payload descriptor.
struct PayloadDescriptor
{
virtual ~PayloadDescriptor() = default;
virtual void Dump() const = 0;
};
// Encoding context used by PayloadDescriptionHandler to properly rewrite the PayloadDescriptor.
class EncodingContext
{
public:
virtual void SyncRequired() = 0;
};
class PayloadDescriptorHandler
{
public:
virtual void Dump() const = 0;
virtual void 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;
};
} // namespace Codecs
} // namespace RTC
#endif
...@@ -2,9 +2,33 @@ ...@@ -2,9 +2,33 @@
#define MS_RTC_CODECS_VP8_HPP #define MS_RTC_CODECS_VP8_HPP
#include "common.hpp" #include "common.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include "RTC/RtpPacket.hpp" #include "RTC/RtpPacket.hpp"
/* RFC 7741
* VP8 Payload Descriptor
*
Single octet PictureID (M = 0) Dual octet PictureID (M = 1)
=============================== ============================
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
|X|R|N|S|R| PID | (REQUIRED) |X|R|N|S|R| PID | (REQUIRED)
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
X: |I|L|T|K| RSV | (OPTIONAL) X: |I|L|T|K| RSV | (OPTIONAL)
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
I: |M| PictureID | (OPTIONAL) I: |M| PictureID | (OPTIONAL)
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
L: | TL0PICIDX | (OPTIONAL) | PictureID |
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
T/K: |TID|Y| KEYIDX | (OPTIONAL) L: | TL0PICIDX | (OPTIONAL)
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
T/K: |TID|Y| KEYIDX | (OPTIONAL)
+-+-+-+-+-+-+-+-+
*/
namespace RTC namespace RTC
{ {
namespace Codecs namespace Codecs
...@@ -12,8 +36,92 @@ namespace RTC ...@@ -12,8 +36,92 @@ namespace RTC
class VP8 class VP8
{ {
public: public:
struct PayloadDescriptor : public RTC::Codecs::PayloadDescriptor
{
/* Pure virtual methods inherited from RTC::Codecs::PayloadDescriptor. */
~PayloadDescriptor() = default;
void Dump() const;
// Rewrite the buffer with the given pictureId and tl0PicutreIndex values.
void Encode(uint8_t* data, uint16_t pictureId, uint8_t tl0PictureIndex);
void Restore(uint8_t* data);
// mandatory fields.
uint8_t extended : 1;
uint8_t nonReference : 1;
uint8_t start : 1;
uint8_t partitionIndex : 4;
// optional field flags.
uint8_t i : 1; // PictureID present.
uint8_t l : 1; // TL0PICIDX present.
uint8_t t : 1; // TID present.
uint8_t k : 1; // KEYIDX present.
// optional fields.
uint16_t pictureId;
uint8_t tl0PictureIndex;
uint8_t tlIndex : 2;
uint8_t y : 1;
uint8_t keyIndex : 5;
bool isKeyFrame = { false };
};
public:
static VP8::PayloadDescriptor* Parse(uint8_t* data, size_t len);
static void ProcessRtpPacket(RTC::RtpPacket* packet); static void ProcessRtpPacket(RTC::RtpPacket* packet);
public:
class EncodingContext : public RTC::Codecs::EncodingContext
{
/* Pure virtual methods inherited from RTC::Codecs::EncodingContext. */
public:
void SyncRequired() override;
public:
uint16_t pictureId{ 0 };
uint16_t pictureIdBase{ 0 };
uint16_t pictureIdPreviousBase{ 0 };
uint8_t tl0PictureIndex{ 0 };
uint8_t tl0PictureIndexBase{ 0 };
uint8_t tl0PictureIndexPreviousBase{ 0 };
bool syncRequired{ false };
};
class PayloadDescriptorHandler : public RTC::Codecs::PayloadDescriptorHandler
{
public:
PayloadDescriptorHandler(PayloadDescriptor* payloadDescriptor);
~PayloadDescriptorHandler() = default;
public:
void Dump() const;
void Encode(RTC::Codecs::EncodingContext* context, uint8_t* data);
void Restore(uint8_t* data);
bool IsKeyFrame() const;
private:
std::unique_ptr<PayloadDescriptor> payloadDescriptor;
};
}; };
/* Inline EncondingContext methods */
inline void VP8::EncodingContext::SyncRequired()
{
this->syncRequired = true;
};
/* Inline PayloadDescriptorHandler methods */
inline bool VP8::PayloadDescriptorHandler::IsKeyFrame() const
{
return this->payloadDescriptor->isKeyFrame;
};
inline void VP8::PayloadDescriptorHandler::Dump() const
{
this->payloadDescriptor->Dump();
}
} // namespace Codecs } // namespace Codecs
} // namespace RTC } // namespace RTC
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "common.hpp" #include "common.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
#include "RTC/ConsumerListener.hpp" #include "RTC/ConsumerListener.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include "RTC/RTCP/CompoundPacket.hpp" #include "RTC/RTCP/CompoundPacket.hpp"
#include "RTC/RTCP/FeedbackRtpNack.hpp" #include "RTC/RTCP/FeedbackRtpNack.hpp"
#include "RTC/RTCP/ReceiverReport.hpp" #include "RTC/RTCP/ReceiverReport.hpp"
...@@ -101,6 +102,8 @@ namespace RTC ...@@ -101,6 +102,8 @@ namespace RTC
uint32_t rtpBaseTimestamp{ 0 }; uint32_t rtpBaseTimestamp{ 0 };
uint32_t rtpPreviousBaseTimestamp{ 0 }; uint32_t rtpPreviousBaseTimestamp{ 0 };
bool syncRequired{ true }; bool syncRequired{ true };
std::unique_ptr<RTC::Codecs::EncodingContext> encodingContext;
// RTP profiles. // RTP profiles.
std::set<RTC::RtpEncodingParameters::Profile> profiles; std::set<RTC::RtpEncodingParameters::Profile> profiles;
RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT }; RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "common.hpp" #include "common.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include "RTC/RtpDictionaries.hpp" #include "RTC/RtpDictionaries.hpp"
#include <map> #include <map>
...@@ -127,6 +128,9 @@ namespace RTC ...@@ -127,6 +128,9 @@ namespace RTC
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);
bool RtxDecode(uint8_t payloadType, uint32_t ssrc); bool RtxDecode(uint8_t payloadType, uint32_t ssrc);
void SetPayloadDescriptorHandler(RTC::Codecs::PayloadDescriptorHandler* payloadDescriptorHandler);
void EncodePayload(RTC::Codecs::EncodingContext* context);
void RestorePayload();
private: private:
void ParseExtensions(); void ParseExtensions();
...@@ -144,7 +148,8 @@ namespace RTC ...@@ -144,7 +148,8 @@ namespace RTC
uint8_t payloadPadding{ 0 }; uint8_t payloadPadding{ 0 };
size_t size{ 0 }; // Full size of the packet in bytes. size_t size{ 0 }; // Full size of the packet in bytes.
uint32_t seq32{ 0 }; // Extended seq number. uint32_t seq32{ 0 }; // Extended seq number.
bool isKeyFrame{ false }; // Whether this packet contains a key frame. // Codecs
std::unique_ptr<Codecs::PayloadDescriptorHandler> payloadDescriptorHandler;
}; };
/* Inline static methods. */ /* Inline static methods. */
...@@ -375,14 +380,18 @@ namespace RTC ...@@ -375,14 +380,18 @@ namespace RTC
return this->payloadLength; return this->payloadLength;
} }
inline void RtpPacket::SetKeyFrame(bool flag) inline bool RtpPacket::IsKeyFrame() const
{ {
this->isKeyFrame = flag; if (!this->payloadDescriptorHandler)
return false;
return this->payloadDescriptorHandler->IsKeyFrame();
} }
inline bool RtpPacket::IsKeyFrame() const inline void RtpPacket::SetPayloadDescriptorHandler(
RTC::Codecs::PayloadDescriptorHandler* payloadDescriptorHandler)
{ {
return this->isKeyFrame; this->payloadDescriptorHandler.reset(payloadDescriptorHandler);
} }
} // namespace RTC } // namespace RTC
......
...@@ -136,6 +136,7 @@ ...@@ -136,6 +136,7 @@
'include/RTC/UdpSocket.hpp', 'include/RTC/UdpSocket.hpp',
'include/RTC/WebRtcTransport.hpp', 'include/RTC/WebRtcTransport.hpp',
'include/RTC/Codecs/Codecs.hpp', 'include/RTC/Codecs/Codecs.hpp',
'include/RTC/Codecs/PayloadDescriptionHandler.hpp',
'include/RTC/Codecs/VP8.hpp', 'include/RTC/Codecs/VP8.hpp',
'include/RTC/RTCP/Packet.hpp', 'include/RTC/RTCP/Packet.hpp',
'include/RTC/RTCP/CompoundPacket.hpp', 'include/RTC/RTCP/CompoundPacket.hpp',
......
...@@ -9,67 +9,203 @@ namespace RTC ...@@ -9,67 +9,203 @@ namespace RTC
{ {
namespace Codecs namespace Codecs
{ {
void VP8::ProcessRtpPacket(RTC::RtpPacket* packet) VP8::PayloadDescriptor* VP8::Parse(uint8_t* data, size_t len)
{ {
MS_TRACE(); MS_TRACE();
auto data = packet->GetPayload(); std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());
auto len = packet->GetPayloadLength();
if (len < 1) if (len < 1)
return; return nullptr;
size_t offset = 0; size_t offset = 0;
uint8_t byte = data[offset++]; uint8_t byte = data[offset];
uint8_t x = (byte >> 7) & 0x1; payloadDescriptor->extended = (byte >> 7) & 0x01;
uint8_t start = (byte >> 4) & 0x1; payloadDescriptor->start = (byte >> 4) & 0x01;
uint8_t pid = byte & 0x07; payloadDescriptor->partitionIndex = byte & 0x07;
uint8_t i = 0; if (payloadDescriptor->extended)
uint8_t l = 0; {
uint8_t t = 0; if (len < ++offset + 1)
uint8_t k = 0; return nullptr;
if (x) byte = data[offset];
payloadDescriptor->i = (byte >> 7) & 0x01;
payloadDescriptor->l = (byte >> 6) & 0x01;
payloadDescriptor->t = (byte >> 5) & 0x01;
payloadDescriptor->k = (byte >> 4) & 0x01;
}
if (payloadDescriptor->i)
{ {
if (len < offset + 1) if (len < ++offset + 1)
return; return nullptr;
byte = data[offset++]; byte = data[offset];
i = (byte >> 7) & 0x1; if ((byte >> 7) & 0x01)
l = (byte >> 6) & 0x1; {
t = (byte >> 5) & 0x1; if (len < ++offset + 1)
k = (byte >> 4) & 0x1; return nullptr;
payloadDescriptor->pictureId = (byte & 0x7F) << 8;
payloadDescriptor->pictureId += data[offset];
}
else
{
payloadDescriptor->pictureId = byte & 0x7F;
}
} }
if (i) if (payloadDescriptor->l)
{ {
if (len < offset++) if (len < ++offset + 1)
return; return nullptr;
payloadDescriptor->tl0PictureIndex = data[offset];
}
if ((byte >> 7) & 0x1) if (payloadDescriptor->t || payloadDescriptor->k)
{ {
if (len < offset++) if (len < ++offset + 1)
return; return nullptr;
byte = data[offset];
payloadDescriptor->tlIndex = (byte >> 6) & 0x03;
payloadDescriptor->y = (byte >> 5) & 0x01;
payloadDescriptor->keyIndex = byte & 0x1F;
} }
if (payloadDescriptor->start && payloadDescriptor->partitionIndex == 0 && (!(data[++offset] & 0x01)))
payloadDescriptor->isKeyFrame = true;
return payloadDescriptor.release();
} }
if (l) void VP8::PayloadDescriptor::Encode(uint8_t* data, uint16_t pictureId, uint8_t tl0PictureIndex)
{ {
if (len < offset++) MS_TRACE();
// Nothing to do.
if (!this->extended)
return; return;
data += 2;
if (this->i)
{
bool twoBytePictureId = (*data >> 7) & 0x01;
if (twoBytePictureId)
{
uint16_t netPictureId = htons(pictureId);
std::memcpy(data, &netPictureId, 2);
data[0] |= 0x80;
data += 2;
} }
else
{
*data = pictureId;
data++;
if (t || k) if (pictureId > 127)
MS_WARN_TAG(rtp, "casting pictureId value to one byte");
}
}
if (this->l)
{ {
if (len < offset++) *data = tl0PictureIndex;
return; }
} }
if (start && pid == 0 && (!(data[offset] & 0x1))) void VP8::PayloadDescriptor::Restore(uint8_t* data)
packet->SetKeyFrame(true); {
this->Encode(data, this->pictureId, this->tl0PictureIndex);
}
void VP8::PayloadDescriptor::Dump() const
{
MS_TRACE();
MS_DUMP("<PayloadDescriptor>");
MS_DUMP(" extended : %" PRIu8, this->extended);
MS_DUMP(" nonReference : %" PRIu8, this->nonReference);
MS_DUMP(" start : %" PRIu8, this->start);
MS_DUMP(" partitionIndex : %" PRIu8, this->partitionIndex);
MS_DUMP(
" i|l|t|k : %" PRIu8 "|%" PRIu8 "|%" PRIu8 "|%" PRIu8,
this->i,
this->l,
this->t,
this->k);
MS_DUMP(" pictureId : %" PRIu16, this->pictureId);
MS_DUMP(" tl0PictureIndex : %" PRIu8, this->tl0PictureIndex);
MS_DUMP(" tlIndex : %" PRIu8, this->tlIndex);
MS_DUMP(" y : %" PRIu8, this->y);
MS_DUMP(" keyIndex : %" PRIu8, this->keyIndex);
MS_DUMP(" isKeyFrame : %s", this->isKeyFrame ? "true" : "false");
MS_DUMP("</PayloadDescriptor>");
}
VP8::PayloadDescriptorHandler::PayloadDescriptorHandler(VP8::PayloadDescriptor* payloadDescriptor)
{
this->payloadDescriptor.reset(payloadDescriptor);
}
void VP8::PayloadDescriptorHandler::Encode(RTC::Codecs::EncodingContext* encodingContext, uint8_t* data)
{
EncodingContext* context = dynamic_cast<EncodingContext*>(encodingContext);
// Check whether pictureId and tl0PictureIndex sync is required.
if (context->syncRequired)
{
context->pictureIdPreviousBase = context->pictureId;
context->pictureIdBase = this->payloadDescriptor->pictureId;
context->tl0PictureIndexPreviousBase = context->tl0PictureIndex;
context->tl0PictureIndexBase = this->payloadDescriptor->tl0PictureIndex;
context->syncRequired = false;
}
// Update pictureId and tl0PictureIndex values.
context->pictureId =
(this->payloadDescriptor->pictureId - context->pictureIdBase) + context->pictureIdPreviousBase + 1;
context->tl0PictureIndex = (this->payloadDescriptor->tl0PictureIndex - context->tl0PictureIndexBase) + context->tl0PictureIndexPreviousBase + 1;
auto pictureId = context->pictureId;
auto tl0PictureIndex = context->tl0PictureIndex;
this->payloadDescriptor->Encode(data, pictureId, tl0PictureIndex);
};
void VP8::PayloadDescriptorHandler::Restore(uint8_t* data)
{
this->payloadDescriptor->Restore(data);
}
void VP8::ProcessRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
auto data = packet->GetPayload();
auto len = packet->GetPayloadLength();
PayloadDescriptor* payloadDescriptor = Parse(data, len);
if (!payloadDescriptor)
return;
PayloadDescriptorHandler* payloadDescriptorHandler =
new PayloadDescriptorHandler(payloadDescriptor);
packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);
} }
} // namespace Codecs } // namespace Codecs
} // namespace RTC } // namespace RTC
...@@ -359,6 +359,9 @@ namespace RTC ...@@ -359,6 +359,9 @@ namespace RTC
this->syncRequired = false; this->syncRequired = false;
if (this->encodingContext)
this->encodingContext->SyncRequired();
MS_DEBUG_TAG(rtp, "re-syncing Consumer stream [rtpLastSeq:%" PRIu16 "]", this->rtpLastSeq); MS_DEBUG_TAG(rtp, "re-syncing Consumer stream [rtpLastSeq:%" PRIu16 "]", this->rtpLastSeq);
} }
...@@ -396,6 +399,10 @@ namespace RTC ...@@ -396,6 +399,10 @@ namespace RTC
// Rewrite packet timestamp. // Rewrite packet timestamp.
packet->SetTimestamp(this->rtpLastTimestamp); packet->SetTimestamp(this->rtpLastTimestamp);
// Rewrite payload if needed.
if (this->encodingContext)
packet->EncodePayload(this->encodingContext.get());
if (isSyncPacket) if (isSyncPacket)
{ {
MS_DEBUG_TAG( MS_DEBUG_TAG(
...@@ -441,6 +448,10 @@ namespace RTC ...@@ -441,6 +448,10 @@ namespace RTC
// Restore the original timestamp. // Restore the original timestamp.
packet->SetTimestamp(origTimestamp); packet->SetTimestamp(origTimestamp);
// Restore the original payload if needed.
if (this->encodingContext)
packet->RestorePayload();
} }
void Consumer::GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) void Consumer::GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now)
...@@ -598,6 +609,8 @@ namespace RTC ...@@ -598,6 +609,8 @@ namespace RTC
this->rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc); this->rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
} }
this->encodingContext.reset(RTC::Codecs::GetEncodingContext(codec.mimeType));
} }
void Consumer::RetransmitRtpPacket(RTC::RtpPacket* packet) void Consumer::RetransmitRtpPacket(RTC::RtpPacket* packet)
......
...@@ -383,9 +383,6 @@ namespace RTC ...@@ -383,9 +383,6 @@ namespace RTC
// Clone seq32. // Clone seq32.
packet->seq32 = this->seq32; packet->seq32 = this->seq32;
// Copy isKeyFrame.
packet->isKeyFrame = this->isKeyFrame;
// Parse RFC 5285 extension header. // Parse RFC 5285 extension header.
packet->ParseExtensions(); packet->ParseExtensions();
...@@ -469,6 +466,22 @@ namespace RTC ...@@ -469,6 +466,22 @@ namespace RTC
return true; return true;
} }
void RtpPacket::EncodePayload(RTC::Codecs::EncodingContext* context)
{
if (!this->payloadDescriptorHandler)
return;
this->payloadDescriptorHandler->Encode(context, this->payload);
}
void RtpPacket::RestorePayload()
{
if (!this->payloadDescriptorHandler)
return;
this->payloadDescriptorHandler->Restore(this->payload);
}
void RtpPacket::ParseExtensions() void RtpPacket::ParseExtensions()
{ {
MS_TRACE(); MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论