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

Improve RtpPacket::RtxDecode() by avoiding a call to Clone() from the Producer.

上级 c2b7a206
# TODO in mediasoup v2 (server-side)
* `RtpPacket::RtxEncode()` should remove the original padding (also the header flag by using `SetPayloadPaddingFlag(false)`), then set the RTX payload, then check whether padding is required and, if so, set the padding flag and bytes.
- Similar stuff (inverse) with `RtxDecode()`.
- NOTE: Nobody does this. Also, it's dangerous since after `RtxDecode()` it may happen that N bytes of padding are needed, but those exceed the original RTX packet size.
* In `RtpStreamSend::StorePacket()` we should remove old packets (as `NackGenerator` does).
* Remove `producer.on('rtprawpacket')` and, instead, create a special `RtpConsumer` or something like that.
......
......@@ -95,6 +95,7 @@ namespace RTC
void SetPayloadType(uint8_t payloadType);
bool HasMarker() const;
void SetMarker(bool marker);
void SetPayloadPaddingFlag(bool flag);
uint16_t GetSequenceNumber() const;
void SetSequenceNumber(uint16_t seq);
uint32_t GetExtendedSequenceNumber() const;
......@@ -190,6 +191,11 @@ namespace RTC
this->header->marker = marker;
}
inline void RtpPacket::SetPayloadPaddingFlag(bool flag)
{
this->header->padding = flag;
}
inline uint16_t RtpPacket::GetSequenceNumber() const
{
return uint16_t{ ntohs(this->header->sequenceNumber) };
......
......@@ -14,8 +14,6 @@ namespace RTC
/* Static. */
static constexpr uint64_t KeyFrameRequestBlockTimeout{ 1000 }; // In ms.
static constexpr size_t ClonedPacketBufferSize{ 65536 };
static uint8_t ClonedPacketBuffer[ClonedPacketBufferSize];
/* Instance methods. */
......@@ -294,14 +292,6 @@ namespace RTC
// Process the packet.
if (!rtpStream->ReceiveRtxPacket(packet))
return;
// We need to clone the RTX decoded packet so it becomes later serializable
// by just taking its GetData() and GetSize(), so clone it and reassign the
// new RtpPacket to the packet pointer (so the rest of the code below doesn't
// need any change) and assign it to the unique_ptr to be automatically
// freed once this method returns.
packet = packet->Clone(ClonedPacketBuffer);
rtxDecodedPacket.reset(packet);
}
else
{
......
......@@ -3,7 +3,7 @@
#include "RTC/RtpPacket.hpp"
#include "Logger.hpp"
#include <cstring> // std::memcpy()
#include <cstring> // std::memcpy(), std::memmove()
#include <iterator> // std::ostream_iterator
#include <sstream> // std::ostringstream
#include <vector>
......@@ -334,31 +334,34 @@ namespace RTC
return packet;
}
// NOTE: The caller must ensure that the buffer/memmory of the packet has
// space enough for adding 2 extra bytes.
void RtpPacket::RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq)
{
MS_TRACE();
// Rewrite the payload type.
this->SetPayloadType(payloadType);
SetPayloadType(payloadType);
// Rewrite the SSRC.
this->SetSsrc(ssrc);
SetSsrc(ssrc);
// Write the original sequence number at the begining of the payload.
std::memmove(this->payload + 2, this->payload, this->payloadLength);
Utils::Byte::Set2Bytes(this->payload, 0, this->GetSequenceNumber());
Utils::Byte::Set2Bytes(this->payload, 0, GetSequenceNumber());
// Rewrite the sequence number.
this->SetSequenceNumber(seq);
SetSequenceNumber(seq);
// Fix the payload length.
this->payloadLength += 2;
// Fix the packet size.
this->size += 2;
// Remove padding.
SetPayloadPaddingFlag(false);
this->payloadPadding = 0u;
// Fix the packet size.
this->size += 2;
}
bool RtpPacket::RtxDecode(uint8_t payloadType, uint32_t ssrc)
......@@ -371,24 +374,27 @@ namespace RTC
return false;
// Rewrite the payload type.
this->SetPayloadType(payloadType);
SetPayloadType(payloadType);
// Rewrite the sequence number.
SetSequenceNumber(Utils::Byte::Get2Bytes(this->payload, 0));
// Rewrite the SSRC.
this->SetSsrc(ssrc);
SetSsrc(ssrc);
// Rewrite the sequence number.
this->SetSequenceNumber(Utils::Byte::Get2Bytes(this->payload, 0));
// Shift the payload to its original place.
std::memmove(this->payload, this->payload + 2, this->payloadLength - 2);
// Fix the payload and payload length.
this->payload += 2;
// Fix the payload length.
this->payloadLength -= 2;
// Fix the packet size.
this->size -= 2;
// Remove padding.
SetPayloadPaddingFlag(false);
this->payloadPadding = 0u;
// Fix the packet size.
this->size -= 2;
return true;
}
......
......@@ -283,7 +283,8 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
0b00010000, 0, 0, 3, // Extension header
1, 0, 2, 1,
0xFF, 0, 3, 4,
0xFF, 0xFF, 0xFF, 0xFF
0xFF, 0xFF, 0xFF, 0xFF,
0x11, 0x11, 0x11, 0x11 // Payload
};
uint8_t rtxPayloadType = 102;
......@@ -302,7 +303,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->GetSequenceNumber() == 8);
REQUIRE(packet->GetTimestamp() == 4);
REQUIRE(packet->GetSsrc() == 5);
REQUIRE(packet->GetPayloadLength() == 0);
REQUIRE(packet->GetPayloadLength() == 4);
REQUIRE(!packet->HasOneByteExtensions());
REQUIRE(packet->HasTwoBytesExtensions());
......@@ -319,7 +320,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(rtxPacket->GetSequenceNumber() == rtxSeq);
REQUIRE(rtxPacket->GetTimestamp() == 4);
REQUIRE(rtxPacket->GetSsrc() == rtxSsrc);
REQUIRE(rtxPacket->GetPayloadLength() == 2);
REQUIRE(rtxPacket->GetPayloadLength() == 6);
REQUIRE(!rtxPacket->HasOneByteExtensions());
REQUIRE(rtxPacket->HasTwoBytesExtensions());
......@@ -332,7 +333,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(rtxPacket->GetSequenceNumber() == 8);
REQUIRE(rtxPacket->GetTimestamp() == 4);
REQUIRE(rtxPacket->GetSsrc() == 5);
REQUIRE(rtxPacket->GetPayloadLength() == 0);
REQUIRE(rtxPacket->GetPayloadLength() == 4);
REQUIRE(!rtxPacket->HasOneByteExtensions());
REQUIRE(rtxPacket->HasTwoBytesExtensions());
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论