Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RtpPacket: Make (almost) all methods remove payload padding if present #1380

Merged
merged 6 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions worker/src/RTC/RtpPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ namespace RTC
}

payloadPadding = data[len - 1];

if (payloadPadding == 0)
{
MS_WARN_TAG(rtp, "padding byte cannot be 0, packet discarded");
Expand All @@ -112,6 +113,7 @@ namespace RTC

return nullptr;
}

payloadLength -= size_t{ payloadPadding };
}

Expand Down Expand Up @@ -650,20 +652,25 @@ namespace RTC
}
}

/**
* NOTE: This method automatically removes payload padding if present.
*/
void RtpPacket::SetPayloadLength(size_t length)
{
MS_TRACE();

// Pad desired length to 4 bytes.
length = static_cast<size_t>(Utils::Byte::PadTo4Bytes(static_cast<uint16_t>(length)));

this->size -= this->payloadLength;
this->size -= size_t{ this->payloadPadding };
this->payloadLength = length;
this->payloadPadding = 0u;
this->size += length;
this->payloadLength = length;
this->size += this->payloadLength;

// Remove padding if present.
if (this->payloadPadding != 0u)
{
SetPayloadPaddingFlag(false);

SetPayloadPaddingFlag(false);
this->size -= size_t{ this->payloadPadding };
this->payloadPadding = 0u;
}
}

RtpPacket* RtpPacket::Clone() const
Expand Down Expand Up @@ -749,8 +756,12 @@ namespace RTC
return packet;
}

// NOTE: The caller must ensure that the buffer/memmory of the packet has
// space enough for adding 2 extra bytes.
/**
* NOTE: The caller must ensure that the buffer/memmory of the packet has
* space enough for adding 2 extra bytes.
*
* NOTE: This method automatically removes payload padding if present.
*/
void RtpPacket::RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq)
{
MS_TRACE();
Expand Down Expand Up @@ -784,6 +795,9 @@ namespace RTC
}
}

/**
* NOTE: This method automatically removes payload padding if present.
*/
bool RtpPacket::RtxDecode(uint8_t payloadType, uint32_t ssrc)
{
MS_TRACE();
Expand Down Expand Up @@ -849,6 +863,11 @@ namespace RTC
this->payloadDescriptorHandler->Restore(this->payload);
}

/**
* Shifts the payload given offset (to right or to left).
*
* NOTE: This method automatically removes payload padding if present.
*/
void RtpPacket::ShiftPayload(size_t payloadOffset, size_t shift, bool expand)
{
MS_TRACE();
Expand All @@ -870,7 +889,7 @@ namespace RTC

if (expand)
{
shiftedLen = this->payloadLength + size_t{ this->payloadPadding } - payloadOffset;
shiftedLen = this->payloadLength - payloadOffset;

std::memmove(payloadOffsetPtr + shift, payloadOffsetPtr, shiftedLen);

Expand All @@ -879,13 +898,22 @@ namespace RTC
}
else
{
shiftedLen = this->payloadLength + size_t{ this->payloadPadding } - payloadOffset - shift;
shiftedLen = this->payloadLength - payloadOffset - shift;

std::memmove(payloadOffsetPtr, payloadOffsetPtr + shift, shiftedLen);

this->payloadLength -= shift;
this->size -= shift;
}

// Remove padding if present.
if (this->payloadPadding != 0u)
{
SetPayloadPaddingFlag(false);

this->size -= size_t{ this->payloadPadding };
this->payloadPadding = 0u;
}
}

void RtpPacket::ParseExtensions()
Expand Down
32 changes: 23 additions & 9 deletions worker/test/src/RTC/TestRtpPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 8);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetSize() == 40);

auto* payload = packet->GetPayload();
Expand All @@ -448,11 +449,12 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(payload[6] == 0x06);
REQUIRE(payload[7] == 0x07);

// NOTE: This will remove padding.
packet->ShiftPayload(0, 2, true);

REQUIRE(packet->GetPayloadLength() == 10);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetSize() == 42);
REQUIRE(packet->GetPayloadPadding() == 0);
REQUIRE(packet->GetSize() == 38);
REQUIRE(payload[2] == 0x00);
REQUIRE(payload[3] == 0x01);
REQUIRE(payload[4] == 0x02);
Expand All @@ -465,8 +467,8 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
packet->ShiftPayload(0, 2, false);

REQUIRE(packet->GetPayloadLength() == 8);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetSize() == 40);
REQUIRE(packet->GetPayloadPadding() == 0);
REQUIRE(packet->GetSize() == 36);
REQUIRE(payload[0] == 0x00);
REQUIRE(payload[1] == 0x01);
REQUIRE(payload[2] == 0x02);
Expand All @@ -476,18 +478,18 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(payload[6] == 0x06);
REQUIRE(payload[7] == 0x07);

// NOTE: This will require padding to 4 bytes.
// NOTE: This will remove padding.
packet->SetPayloadLength(14);

REQUIRE(packet->GetPayloadLength() == 16);
REQUIRE(packet->GetPayloadLength() == 14);
REQUIRE(packet->GetPayloadPadding() == 0);
REQUIRE(packet->GetSize() == 44);
REQUIRE(packet->GetSize() == 42);

packet->ShiftPayload(4, 4, true);

REQUIRE(packet->GetPayloadLength() == 20);
REQUIRE(packet->GetPayloadLength() == 18);
REQUIRE(packet->GetPayloadPadding() == 0);
REQUIRE(packet->GetSize() == 48);
REQUIRE(packet->GetSize() == 46);
REQUIRE(payload[0] == 0x00);
REQUIRE(payload[1] == 0x01);
REQUIRE(payload[2] == 0x02);
Expand Down Expand Up @@ -545,6 +547,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);

Expand All @@ -560,6 +563,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);

Expand Down Expand Up @@ -612,6 +616,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);
REQUIRE(packet->GetExtension(0, extenLen) == nullptr);
Expand Down Expand Up @@ -647,6 +652,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);
REQUIRE(packet->GetExtension(1, extenLen) == nullptr);
Expand Down Expand Up @@ -689,6 +695,10 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
// clang-format on

Expand All @@ -710,6 +720,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == false);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);

Expand All @@ -725,6 +736,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == true);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);

Expand Down Expand Up @@ -763,6 +775,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == true);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);
REQUIRE(packet->GetExtension(0, extenLen) == nullptr);
Expand Down Expand Up @@ -806,6 +819,7 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
REQUIRE(packet->HasTwoBytesExtensions() == true);
REQUIRE(packet->GetPayloadLength() == 12);
REQUIRE(packet->GetPayloadPadding() == 4);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() + packet->GetPayloadPadding() - 1] == 4);
REQUIRE(packet->GetPayload()[0] == 0x11);
REQUIRE(packet->GetPayload()[packet->GetPayloadLength() - 1] == 0xCC);
REQUIRE(packet->GetExtension(1, extenLen) == nullptr);
Expand Down
Loading