#include "common.hpp"
#include "Utils.hpp"
#include "testHelpers.hpp"
#include "RTC/RTP/HeaderExtensionIds.hpp"
#include "RTC/RTP/Packet.hpp"
#include "RTC/RTP/rtpCommon.hpp"
#include "RTC/RtpDictionaries.hpp"
#include <catch2/catch_test_macros.hpp>
#include <cstring>
#include <string>
SCENARIO("RTP Packet", "[serializable][rtp][packet]")
{
rtpCommon::ResetBuffers();
SECTION("alignof() RTP structs")
{
REQUIRE(alignof(RTC::RTP::Packet::FixedHeader) == 4);
REQUIRE(alignof(RTC::RTP::Packet::HeaderExtension) == 2);
REQUIRE(alignof(RTC::RTP::Packet::OneByteExtension) == 1);
REQUIRE(alignof(RTC::RTP::Packet::TwoBytesExtension) == 1);
}
SECTION("Packet::Parse() packet1.raw succeeds")
{
alignas(4) uint8_t buffer[65536];
size_t bufferLength;
if (!helpers::readBinaryFile("data/packet1.raw", buffer, std::addressof(bufferLength)))
{
FAIL("cannot open file");
}
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, bufferLength) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
bufferLength,
bufferLength,
111,
false,
23617,
1660241882,
2674985186,
false,
true,
4,
true,
false,
true,
33,
false,
0);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
bufferLength,
111,
false,
23617,
1660241882,
2674985186,
false,
true,
4,
true,
false,
true,
33,
false,
0);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
bufferLength,
111,
false,
23617,
1660241882,
2674985186,
false,
true,
4,
true,
false,
true,
33,
false,
0);
packet->SetPayload(rtpCommon::DataBuffer, 16);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
bufferLength - 33 + 16,
111,
false,
23617,
1660241882,
2674985186,
false,
true,
4,
true,
false,
true,
16,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 16) == true);
}
SECTION("Packet::Parse() packet2.raw succeeds")
{
alignas(4) uint8_t buffer[65536];
size_t bufferLength;
if (!helpers::readBinaryFile("data/packet2.raw", buffer, std::addressof(bufferLength)))
{
FAIL("cannot open file");
}
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, bufferLength) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
bufferLength,
bufferLength,
100,
false,
28478,
172320136,
3316375386,
false,
false,
0,
false,
false,
true,
78,
true,
149);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
bufferLength,
100,
false,
28478,
172320136,
3316375386,
false,
false,
0,
false,
false,
true,
78,
true,
149);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
bufferLength,
100,
false,
28478,
172320136,
3316375386,
false,
false,
0,
false,
false,
true,
78,
true,
149);
packet->SetPayload(rtpCommon::DataBuffer, 16);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
bufferLength - 78 + 16 - 149,
100,
false,
28478,
172320136,
3316375386,
false,
false,
0,
false,
false,
true,
16,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 16) == true);
}
SECTION("Packet::Parse() packet3.raw succeeds")
{
alignas(4) uint8_t buffer[65536];
size_t bufferLength;
if (!helpers::readBinaryFile("data/packet3.raw", buffer, std::addressof(bufferLength)))
{
FAIL("cannot open file");
}
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, bufferLength) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
bufferLength,
bufferLength,
111,
false,
19354,
863466045,
235797202,
false,
true,
8,
true,
false,
true,
77,
false,
0);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
bufferLength,
111,
false,
19354,
863466045,
235797202,
false,
true,
8,
true,
false,
true,
77,
false,
0);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
bufferLength,
111,
false,
19354,
863466045,
235797202,
false,
true,
8,
true,
false,
true,
77,
false,
0);
packet->SetPayload(rtpCommon::DataBuffer, 16);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
bufferLength - 77 + 16,
111,
false,
19354,
863466045,
235797202,
false,
true,
8,
true,
false,
true,
16,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 16) == true);
}
SECTION("Packet::Parse() without extensions or payload succeeds")
{
alignas(4) uint8_t buffer[] =
{
0x80, 0x01, 0x00, 0x08,
0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05
};
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, sizeof(buffer)) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
sizeof(buffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
false,
0,
false,
false,
false,
0,
false,
0);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
false,
0,
false,
false,
false,
0,
false,
0);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
false,
0,
false,
false,
false,
0,
false,
0);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetPayload(rtpCommon::DataBuffer, 16);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer) + 16,
1,
false,
8,
4,
5,
false,
false,
0,
false,
false,
true,
16,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 16) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
}
SECTION("Packet::Parse() with One-Byte extensions succeeds")
{
alignas(4) uint8_t buffer[] =
{
0x90, 0x01, 0x00, 0x08,
0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05,
0xbe, 0xde, 0x00, 0x03, 0x10, 0xaa, 0x21, 0xbb, 0xff, 0x00, 0x00, 0x33, 0xff, 0xff, 0xff, 0xff, 0x12, 0x23
};
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, sizeof(buffer)) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
sizeof(buffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
true,
12,
true,
false,
true,
2,
false,
0);
uint8_t* extensionValue;
uint8_t extensionLen;
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionLen == 1);
REQUIRE(helpers::areBuffersEqual(extensionValue, 1, buffer + 17, 1) == true);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionLen == 2);
REQUIRE(helpers::areBuffersEqual(extensionValue, 2, buffer + 19, 2) == true);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionLen == 4);
REQUIRE(helpers::areBuffersEqual(extensionValue, 4, buffer + 24, 4) == true);
REQUIRE(packet->HasExtension(4) == false);
REQUIRE(packet->GetExtensionValue(4, extensionLen) == nullptr);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
true,
12,
true,
false,
true,
2,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionLen == 1);
REQUIRE(helpers::areBuffersEqual(extensionValue, 1, rtpCommon::SerializeBuffer + 17, 1) == true);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionLen == 2);
REQUIRE(helpers::areBuffersEqual(extensionValue, 2, rtpCommon::SerializeBuffer + 19, 2) == true);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionLen == 4);
REQUIRE(helpers::areBuffersEqual(extensionValue, 4, rtpCommon::SerializeBuffer + 24, 4) == true);
REQUIRE(packet->HasExtension(4) == false);
REQUIRE(packet->GetExtensionValue(4, extensionLen) == nullptr);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
true,
12,
true,
false,
true,
2,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionLen == 1);
REQUIRE(helpers::areBuffersEqual(extensionValue, 1, rtpCommon::CloneBuffer + 17, 1) == true);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionLen == 2);
REQUIRE(helpers::areBuffersEqual(extensionValue, 2, rtpCommon::CloneBuffer + 19, 2) == true);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionLen == 4);
REQUIRE(helpers::areBuffersEqual(extensionValue, 4, rtpCommon::CloneBuffer + 24, 4) == true);
REQUIRE(packet->HasExtension(4) == false);
REQUIRE(packet->GetExtensionValue(4, extensionLen) == nullptr);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
packet->SetPayload(rtpCommon::DataBuffer, 16);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer) - 2 + 16,
1,
false,
8,
4,
5,
false,
true,
12,
true,
false,
true,
16,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 16) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
}
SECTION("Packet::Parse() with Two-Bytes extensions succeeds")
{
alignas(4) uint8_t buffer[] =
{
0x90, 0x01, 0x00, 0x08,
0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05,
0x10, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x02, 0x01, 0x42, 0x00, 0x03, 0x02, 0x11, 0x22, 0x00, 0x00, 0x04, 0x00 };
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, sizeof(buffer)) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
sizeof(buffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
true,
16,
false,
true,
false,
0,
false,
0);
uint8_t* extensionValue;
uint8_t extensionLen;
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionLen == 0);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionLen == 1);
REQUIRE(helpers::areBuffersEqual(extensionValue, 1, buffer + 22, 1) == true);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionLen == 2);
REQUIRE(helpers::areBuffersEqual(extensionValue, 2, buffer + 26, 2) == true);
REQUIRE(packet->HasExtension(4) == true);
extensionValue = packet->GetExtensionValue(4, extensionLen);
REQUIRE(extensionLen == 0);
REQUIRE(packet->HasExtension(5) == false);
REQUIRE(packet->GetExtensionValue(5, extensionLen) == nullptr);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
true,
16,
false,
true,
false,
0,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionLen == 0);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionLen == 1);
REQUIRE(helpers::areBuffersEqual(extensionValue, 1, rtpCommon::SerializeBuffer + 22, 1) == true);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionLen == 2);
REQUIRE(helpers::areBuffersEqual(extensionValue, 2, rtpCommon::SerializeBuffer + 26, 2) == true);
REQUIRE(packet->HasExtension(4) == true);
extensionValue = packet->GetExtensionValue(4, extensionLen);
REQUIRE(extensionLen == 0);
REQUIRE(packet->HasExtension(5) == false);
REQUIRE(packet->GetExtensionValue(5, extensionLen) == nullptr);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer),
1,
false,
8,
4,
5,
false,
true,
16,
false,
true,
false,
0,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionLen == 0);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionLen == 1);
REQUIRE(helpers::areBuffersEqual(extensionValue, 1, rtpCommon::CloneBuffer + 22, 1) == true);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionLen == 2);
REQUIRE(helpers::areBuffersEqual(extensionValue, 2, rtpCommon::CloneBuffer + 26, 2) == true);
REQUIRE(packet->HasExtension(4) == true);
extensionValue = packet->GetExtensionValue(4, extensionLen);
REQUIRE(extensionLen == 0);
REQUIRE(packet->HasExtension(5) == false);
REQUIRE(packet->GetExtensionValue(5, extensionLen) == nullptr);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetPayload(rtpCommon::DataBuffer, 15);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer) + 15,
1,
false,
8,
4,
5,
false,
true,
16,
false,
true,
true,
15,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 15) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
}
SECTION("Packet::Parse() padding-only packet succeeds")
{
alignas(4) uint8_t buffer[] =
{
0xA0, 0x01, 0x00, 0x09,
0x00, 0x00, 0x00, 0x05,
0x00, 0x00, 0x00, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08
};
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Parse(buffer, sizeof(buffer)) };
CHECK_RTP_PACKET(
packet.get(),
buffer,
sizeof(buffer),
sizeof(buffer),
1,
false,
9,
5,
6,
false,
false,
0,
false,
false,
false,
0,
true,
8);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
std::memset(buffer, 0x00, sizeof(buffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
sizeof(buffer),
1,
false,
9,
5,
6,
false,
false,
0,
false,
false,
false,
0,
true,
8);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
sizeof(buffer),
1,
false,
9,
5,
6,
false,
false,
0,
false,
false,
false,
0,
true,
8);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetPayload(rtpCommon::DataBuffer, 1);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
packet->GetLength(),
1,
false,
9,
5,
6,
false,
false,
0,
false,
false,
true,
1,
false,
0);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 1) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
packet->PadTo4Bytes();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
packet->GetLength(),
1,
false,
9,
5,
6,
false,
false,
0,
false,
false,
true,
1,
true,
3);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 1) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
}
SECTION("Packet::Parse() with wrong arguments fails")
{
alignas(4) uint8_t buffer[] =
{
0x90, 0x01, 0x00, 0x08,
0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x05,
0xbe, 0xde, 0x00, 0x03, 0x10, 0xaa, 0x21, 0xbb, 0xff, 0x00, 0x00, 0x33, 0xff, 0xff, 0xff, 0xff, 0x12, 0x23
};
std::unique_ptr<RTC::RTP::Packet> packet{ nullptr };
REQUIRE_THROWS_AS(
packet.reset(RTC::RTP::Packet::Parse(buffer, sizeof(buffer), sizeof(buffer) - 1)),
MediaSoupTypeError);
REQUIRE(!packet);
}
SECTION("Packet::Factory() succeeds")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength,
0,
false,
0,
0,
0,
false,
false,
0,
false,
false,
false,
0,
false,
0);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetPayloadType(100);
packet->SetMarker(true);
packet->SetSequenceNumber(12345);
packet->SetTimestamp(987654321);
packet->SetSsrc(1234567890);
std::vector<RTC::RTP::Packet::Extension> extensions;
rtpCommon::DataBuffer[0] = 11;
rtpCommon::DataBuffer[1] = 22;
rtpCommon::DataBuffer[2] = 0xAA;
rtpCommon::DataBuffer[3] = 14;
rtpCommon::DataBuffer[4] = 0xBB;
rtpCommon::DataBuffer[5] = 0xCC;
extensions.emplace_back(
RTC::RtpHeaderExtensionUri::Type::MID,
1,
1,
rtpCommon::DataBuffer + 0);
extensions.emplace_back(
RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID,
2,
2,
rtpCommon::DataBuffer + 1);
extensions.emplace_back(
RTC::RtpHeaderExtensionUri::Type::REPAIRED_RTP_STREAM_ID,
14,
3,
rtpCommon::DataBuffer + 3);
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions);
packet->SetPayload(rtpCommon::DataBuffer, 10);
packet->PadTo4Bytes();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 10 + 2,
100,
true,
12345,
987654321,
1234567890,
false,
true,
12,
true,
false,
true,
10,
true,
2);
const uint8_t* extensionValue;
uint8_t extensionLen;
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[1]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[2]);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == true);
extensionValue = packet->GetExtensionValue(14, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[3]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[4]);
REQUIRE(extensionValue[2] == rtpCommon::DataBuffer[5]);
REQUIRE(extensionLen == 3);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::SerializeBuffer,
sizeof(rtpCommon::SerializeBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 10 + 2,
100,
true,
12345,
987654321,
1234567890,
false,
true,
12,
true,
false,
true,
10,
true,
2);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[1]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[2]);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == true);
extensionValue = packet->GetExtensionValue(14, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[3]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[4]);
REQUIRE(extensionValue[2] == rtpCommon::DataBuffer[5]);
REQUIRE(extensionLen == 3);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet.reset(packet->Clone(rtpCommon::CloneBuffer, sizeof(rtpCommon::CloneBuffer)));
std::memset(rtpCommon::SerializeBuffer, 0x00, sizeof(rtpCommon::SerializeBuffer));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 10 + 2,
100,
true,
12345,
987654321,
1234567890,
false,
true,
12,
true,
false,
true,
10,
true,
2);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[1]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[2]);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == true);
extensionValue = packet->GetExtensionValue(14, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[3]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[4]);
REQUIRE(extensionValue[2] == rtpCommon::DataBuffer[5]);
REQUIRE(extensionLen == 3);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetPayload(rtpCommon::DataBuffer, 1);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 1,
100,
true,
12345,
987654321,
1234567890,
false,
true,
12,
true,
false,
true,
1,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[1]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[2]);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == true);
extensionValue = packet->GetExtensionValue(14, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[3]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[4]);
REQUIRE(extensionValue[2] == rtpCommon::DataBuffer[5]);
REQUIRE(extensionLen == 3);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 1) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
packet->PadTo4Bytes();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 1 + 3,
100,
true,
12345,
987654321,
1234567890,
false,
true,
12,
true,
false,
true,
1,
true,
3);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[1]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[2]);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == true);
extensionValue = packet->GetExtensionValue(14, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[3]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[4]);
REQUIRE(extensionValue[2] == rtpCommon::DataBuffer[5]);
REQUIRE(extensionLen == 3);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 1) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->RemoveHeaderExtension();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 1 + 3,
100,
true,
12345,
987654321,
1234567890,
false,
false,
0,
false,
false,
true,
1,
true,
3);
REQUIRE(packet->HasExtension(1) == false);
REQUIRE(packet->HasExtension(2) == false);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == false);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 1) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::TwoBytes, extensions);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::CloneBuffer,
sizeof(rtpCommon::CloneBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 1 + 3,
100,
true,
12345,
987654321,
1234567890,
false,
true,
12,
false,
true,
true,
1,
true,
3);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[1]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[2]);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == false);
REQUIRE(packet->HasExtension(14) == true);
extensionValue = packet->GetExtensionValue(14, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[3]);
REQUIRE(extensionValue[1] == rtpCommon::DataBuffer[4]);
REQUIRE(extensionValue[2] == rtpCommon::DataBuffer[5]);
REQUIRE(extensionLen == 3);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 1) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
}
SECTION("Packet::SetExtensions() with ExtensionsType::Auto selects best type")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
std::vector<RTC::RTP::Packet::Extension> extensions;
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::MID, 1, 1, rtpCommon::DataBuffer },
{ RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID, 14, 16, rtpCommon::DataBuffer }
});
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::Auto, extensions);
REQUIRE(packet->HasOneByteExtensions());
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME, 15, 2, rtpCommon::DataBuffer }
});
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::Auto, extensions);
REQUIRE(packet->HasTwoBytesExtensions());
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::REPAIRED_RTP_STREAM_ID, 1, 0, rtpCommon::DataBuffer }
});
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::Auto, extensions);
REQUIRE(packet->HasTwoBytesExtensions());
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::TIME_OFFSET, 1, 17, rtpCommon::DataBuffer }
});
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::Auto, extensions);
REQUIRE(packet->HasTwoBytesExtensions());
}
SECTION("Packet::SetExtensions() with supported extensions succeeds")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
std::vector<RTC::RTP::Packet::Extension> extensions;
std::string mid{ "mid-€1" };
std::string rid{ "r1-ß" };
uint32_t absSendtime{ 12345678 };
uint16_t wideSeqNumber{ 5555 };
uint8_t absSendtimeValue[100]{};
uint8_t wideSeqNumberValue[100]{};
Utils::Byte::Set3Bytes(absSendtimeValue, 0, absSendtime);
Utils::Byte::Set2Bytes(wideSeqNumberValue, 0, wideSeqNumber);
extensions.assign(
{
{
RTC::RtpHeaderExtensionUri::Type::MID,
1,
static_cast<uint8_t>(mid.size()),
reinterpret_cast<uint8_t*>(mid.data())
},
{
RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID,
2,
static_cast<uint8_t>(rid.size()),
reinterpret_cast<uint8_t*>(rid.data())
},
{
RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME,
3,
3,
absSendtimeValue
},
{
RTC::RtpHeaderExtensionUri::Type::TRANSPORT_WIDE_CC_01,
4,
2,
wideSeqNumberValue
}
}
);
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions);
REQUIRE(packet->HasOneByteExtensions());
std::string readMid;
std::string readRid;
uint32_t readAbsSendtime;
uint16_t readWideSeqNumber;
REQUIRE(packet->ReadMid(readMid));
REQUIRE(readMid == mid);
REQUIRE(packet->ReadRid(readRid));
REQUIRE(readRid == rid);
REQUIRE(packet->ReadAbsSendTime(readAbsSendtime));
REQUIRE(readAbsSendtime == absSendtime);
REQUIRE(packet->ReadTransportWideCc01(readWideSeqNumber));
REQUIRE(readWideSeqNumber == wideSeqNumber);
std::string newMid{ "mid-®2" };
uint64_t newAbsSendtimeMs{ 999999 };
uint16_t newWideSeqNumber{ 5556 };
REQUIRE(packet->UpdateMid(newMid));
REQUIRE(packet->UpdateAbsSendTime(newAbsSendtimeMs));
REQUIRE(packet->UpdateTransportWideCc01(newWideSeqNumber));
REQUIRE(packet->ReadMid(readMid));
REQUIRE(readMid == newMid);
REQUIRE(packet->ReadRid(readRid));
REQUIRE(readRid == rid);
REQUIRE(packet->ReadAbsSendTime(readAbsSendtime));
REQUIRE(readAbsSendtime == Utils::Time::TimeMsToAbsSendTime(newAbsSendtimeMs));
REQUIRE(packet->ReadTransportWideCc01(readWideSeqNumber));
REQUIRE(readWideSeqNumber == newWideSeqNumber);
std::unique_ptr<RTC::RTP::Packet> packet2{ RTC::RTP::Packet::Parse(
packet->GetBuffer(), packet->GetLength()) };
REQUIRE(packet2);
REQUIRE(packet2->Validate( false));
packet2->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
RTC::RTP::HeaderExtensionIds headerExtensionIds{};
headerExtensionIds.mid = 1;
headerExtensionIds.rid = 2;
headerExtensionIds.absSendTime = 3;
headerExtensionIds.transportWideCc01 = 4;
packet2->AssignExtensionIds(headerExtensionIds);
REQUIRE(packet2->HasOneByteExtensions());
REQUIRE(packet2->ReadMid(readMid));
REQUIRE(readMid == newMid);
REQUIRE(packet2->ReadRid(readRid));
REQUIRE(readRid == rid);
REQUIRE(packet2->ReadAbsSendTime(readAbsSendtime));
REQUIRE(readAbsSendtime == Utils::Time::TimeMsToAbsSendTime(newAbsSendtimeMs));
REQUIRE(packet2->ReadTransportWideCc01(readWideSeqNumber));
REQUIRE(readWideSeqNumber == newWideSeqNumber);
}
SECTION("Packet::SetExtensions() fails if wrong extensions are given")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
packet->SetPayload(rtpCommon::DataBuffer, 10);
packet->PadTo4Bytes();
std::vector<RTC::RTP::Packet::Extension> extensions;
auto* d = rtpCommon::DataBuffer;
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::MID, 0, 4, d },
{ RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID, 1, 1, d }
});
REQUIRE_THROWS_AS(
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions),
MediaSoupTypeError);
REQUIRE_THROWS_AS(
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::TwoBytes, extensions),
MediaSoupTypeError);
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::VIDEO_ORIENTATION, 15, 2, d },
{ RTC::RtpHeaderExtensionUri::Type::MID, 6, 6, d },
{ RTC::RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL, 7, 7, d }
});
REQUIRE_THROWS_AS(
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions),
MediaSoupTypeError);
REQUIRE_NOTHROW(packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::TwoBytes, extensions));
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::MID, 3, 0, d },
{ RTC::RtpHeaderExtensionUri::Type::REPAIRED_RTP_STREAM_ID, 6, 6, d },
{ RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID, 7, 7, d },
{ RTC::RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL, 8, 8, d }
});
REQUIRE_THROWS_AS(
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions),
MediaSoupTypeError);
REQUIRE_NOTHROW(packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::TwoBytes, extensions));
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::MEDIASOUP_PACKET_ID, 3, 17, d },
{ RTC::RtpHeaderExtensionUri::Type::MID, 6, 6, d },
{ RTC::RtpHeaderExtensionUri::Type::VIDEO_ORIENTATION, 7, 7, d },
{ RTC::RtpHeaderExtensionUri::Type::DEPENDENCY_DESCRIPTOR, 8, 8, d },
{ RTC::RtpHeaderExtensionUri::Type::PLAYOUT_DELAY, 9, 9, d },
{ RTC::RtpHeaderExtensionUri::Type::ABS_CAPTURE_TIME, 100, 10, d }
});
REQUIRE_THROWS_AS(
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions),
MediaSoupTypeError);
REQUIRE_NOTHROW(packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::TwoBytes, extensions));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 4 + 72 + 10 + 2,
0,
false,
0,
0,
0,
false,
true,
72,
false,
true,
true,
10,
true,
2);
const uint8_t* extensionValue;
uint8_t extensionLen;
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 17);
REQUIRE(packet->HasExtension(1) == false);
REQUIRE(packet->HasExtension(6) == true);
extensionValue = packet->GetExtensionValue(6, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 6);
REQUIRE(packet->HasExtension(7) == true);
extensionValue = packet->GetExtensionValue(7, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 7);
REQUIRE(packet->HasExtension(8) == true);
extensionValue = packet->GetExtensionValue(8, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 8);
REQUIRE(packet->HasExtension(9) == true);
extensionValue = packet->GetExtensionValue(9, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 9);
REQUIRE(packet->HasExtension(100) == true);
extensionValue = packet->GetExtensionValue(100, extensionLen);
REQUIRE(extensionValue[0] == rtpCommon::DataBuffer[0]);
REQUIRE(extensionLen == 10);
REQUIRE(packet->HasExtension(101) == false);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), rtpCommon::DataBuffer, 10) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
}
SECTION("Packet::SetPayload(), SetPayloadLength() and packet::RemovePayload() succeed")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
uint8_t payload[] =
{
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x88,
0x99, 0xAA
};
packet->SetPayload(payload, 10);
packet->PadTo4Bytes();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 10 + 2,
0,
false,
0,
0,
0,
false,
false,
0,
false,
false,
true,
10,
true,
2);
packet->SetPayloadLength(501);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 501,
0,
false,
0,
0,
0,
false,
false,
0,
false,
false,
true,
501,
false,
0);
packet->RemovePayload();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength,
0,
false,
0,
0,
0,
false,
false,
0,
false,
false,
false,
0,
false,
0);
REQUIRE_THROWS_AS(packet->SetPayload(nullptr, 2), MediaSoupTypeError);
}
SECTION("Packet::ShiftPayload() succeeds")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
packet->SetSsrc(12344321);
uint8_t payload[] =
{
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x88,
0x99, 0xAA
};
packet->SetPayload(payload, 10);
packet->PadTo4Bytes();
std::vector<RTC::RTP::Packet::Extension> extensions;
uint8_t extension1[] =
{
0x12
};
uint8_t extension2[] =
{
0x34, 0x56
};
uint8_t extension3[] =
{
0x78, 0x9A, 0xBC
};
extensions.assign(
{
{ RTC::RtpHeaderExtensionUri::Type::MID, 1, 1, extension1 },
{ RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME, 2, 2, extension2 },
{ RTC::RtpHeaderExtensionUri::Type::TRANSPORT_WIDE_CC_01, 3, 3, extension3 }
});
packet->SetExtensions(RTC::RTP::Packet::ExtensionsType::OneByte, extensions);
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 10 + 2,
0,
false,
0,
0,
12344321,
false,
true,
12,
true,
false,
true,
10,
true,
2);
const uint8_t* extensionValue;
uint8_t extensionLen;
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension1, 1) == true);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension2, 2) == true);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension3, 3) == true);
REQUIRE(extensionLen == 3);
REQUIRE(
helpers::areBuffersEqual(packet->GetPayload(), packet->GetPayloadLength(), payload, 10) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->ShiftPayload( 2, 1);
packet->GetPayload()[2] = 0xFF;
uint8_t shiftedPayload[] =
{
0x11, 0x22, 0xFF, 0x33,
0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xAA
};
REQUIRE(packet->Validate( false));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 10 + 1,
0,
false,
0,
0,
12344321,
false,
true,
12,
true,
false,
true,
11,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension1, 1) == true);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension2, 2) == true);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension3, 3) == true);
REQUIRE(extensionLen == 3);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), shiftedPayload, 11) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
packet->SetPayload(payload, 10);
packet->PadTo4Bytes();
packet->ShiftPayload( 4, -2);
uint8_t unshiftedPayload[] =
{
0x11, 0x22, 0x33, 0x44,
0x77, 0x88, 0x99, 0xAA
};
REQUIRE(packet->Validate( false));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 16 + 10 - 2,
0,
false,
0,
0,
12344321,
false,
true,
12,
true,
false,
true,
8,
false,
0);
REQUIRE(packet->HasExtension(1) == true);
extensionValue = packet->GetExtensionValue(1, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension1, 1) == true);
REQUIRE(extensionLen == 1);
REQUIRE(packet->HasExtension(2) == true);
extensionValue = packet->GetExtensionValue(2, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension2, 2) == true);
REQUIRE(extensionLen == 2);
REQUIRE(packet->HasExtension(3) == true);
extensionValue = packet->GetExtensionValue(3, extensionLen);
REQUIRE(helpers::areBuffersEqual(extensionValue, extensionLen, extension3, 3) == true);
REQUIRE(extensionLen == 3);
REQUIRE(
helpers::areBuffersEqual(
packet->GetPayload(), packet->GetPayloadLength(), unshiftedPayload, 8) == true);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->SetPayload(payload, 10);
packet->PadTo4Bytes();
packet->ShiftPayload( 3, 5);
packet->ShiftPayload( 3, -5);
REQUIRE(
helpers::areBuffersEqual(packet->GetPayload(), packet->GetPayloadLength(), payload, 10) == true);
}
SECTION("Packet::ShiftPayload() fails if wrong values are given")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
uint8_t payload[] =
{
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x88,
0x99, 0xAA
};
packet->SetPayload(payload, 10);
REQUIRE_THROWS_AS(packet->ShiftPayload( 10, 2), MediaSoupTypeError);
REQUIRE_THROWS_AS(packet->ShiftPayload( 2, -9), MediaSoupTypeError);
REQUIRE_THROWS_AS(
packet->ShiftPayload( 2, packet->GetBufferLength()),
MediaSoupTypeError);
}
SECTION("Packet::RtxEncode() and packet::RtxDecode() succeed")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
packet->SetPayloadType(100);
packet->SetSequenceNumber(12345);
packet->SetTimestamp(987654321);
packet->SetSsrc(1234567890);
uint8_t payload[] =
{
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x88,
0x99, 0xAA
};
packet->SetPayload(payload, 10);
packet->PadTo4Bytes();
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 10 + 2,
100,
false,
12345,
987654321,
1234567890,
false,
false,
0,
false,
false,
true,
10,
true,
2);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->RtxEncode( 111, 999999, 666);
REQUIRE(packet->Validate( false));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 10 + 2,
111,
false,
666,
987654321,
999999,
false,
false,
0,
false,
false,
true,
12,
false,
0);
REQUIRE(packet->IsPaddedTo4Bytes() == true);
packet->RtxDecode( 100, 1234567890);
REQUIRE(packet->Validate( false));
CHECK_RTP_PACKET(
packet.get(),
rtpCommon::FactoryBuffer,
sizeof(rtpCommon::FactoryBuffer),
RTC::RTP::Packet::FixedHeaderMinLength + 10,
100,
false,
12345,
987654321,
1234567890,
false,
false,
0,
false,
false,
true,
10,
false,
0);
REQUIRE(packet->IsPaddedTo4Bytes() == false);
}
SECTION("Packet::SetBufferReleasedListener() when Packet is destroyed succeeds")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
REQUIRE(packet);
bool packetBufferReleased{ false };
RTC::Serializable::BufferReleasedListener packetBufferReleasedListener =
[&packetBufferReleased](const RTC::Serializable* serializable, const uint8_t* serializableBuffer)
{
if (serializable->GetBuffer() == rtpCommon::FactoryBuffer && serializable->GetBuffer() == serializableBuffer)
{
packetBufferReleased = true;
}
};
packet->SetBufferReleasedListener(std::addressof(packetBufferReleasedListener));
packet.reset(nullptr);
REQUIRE(packetBufferReleased == true);
}
SECTION("Packet::SetBufferReleasedListener() when Packet is serialized into another buffer succeeds")
{
std::unique_ptr<RTC::RTP::Packet> packet{ RTC::RTP::Packet::Factory(
rtpCommon::FactoryBuffer, sizeof(rtpCommon::FactoryBuffer)) };
REQUIRE(packet);
bool packetBufferReleased{ false };
RTC::Serializable::BufferReleasedListener packetBufferReleasedListener =
[&packetBufferReleased](const RTC::Serializable* serializable, const uint8_t* serializableBuffer)
{
if (serializable->GetBuffer() == rtpCommon::FactoryBuffer && serializable->GetBuffer() == serializableBuffer)
{
packetBufferReleased = true;
}
};
packet->SetBufferReleasedListener(std::addressof(packetBufferReleasedListener));
packet->Serialize(rtpCommon::SerializeBuffer, sizeof(rtpCommon::SerializeBuffer));
REQUIRE(packetBufferReleased == true);
packet->SetBufferReleasedListener(nullptr);
}
}