#define MS_CLASS "RTC::RTP::ProbationGenerator"
#include "RTC/RTP/ProbationGenerator.hpp"
#include "Logger.hpp"
#include "RTC/RtpDictionaries.hpp"
#include "Utils.hpp"
#include <cstring>
#include <vector>
namespace RTC
{
namespace RTP
{
static thread_local uint8_t ProbationPacketBuffer[ProbationGenerator::ProbationPacketMaxLength];
static constexpr size_t ProbationPacketExtensionsBufferLength{ 200 };
alignas(4) static thread_local uint8_t
ProbationPacketExtensionsBuffer[ProbationPacketExtensionsBufferLength];
static const std::string MidValue{ "probator" };
ProbationGenerator::ProbationGenerator()
{
MS_TRACE();
static thread_local bool mustInitializePayload{ true };
if (mustInitializePayload)
{
std::memset(ProbationPacketBuffer, 0x00, sizeof(ProbationPacketBuffer));
mustInitializePayload = false;
}
this->probationPacket.reset(
RTP::Packet::Factory(ProbationPacketBuffer, sizeof(ProbationPacketBuffer)));
this->probationPacket->SetPayloadType(ProbationGenerator::PayloadType);
this->probationPacket->SetSsrc(ProbationGenerator::Ssrc);
this->probationPacket->SetSequenceNumber(Utils::Crypto::GetRandomUInt<uint16_t>(0, 65535));
this->probationPacket->SetTimestamp(Utils::Crypto::GetRandomUInt<uint32_t>(0, 4294967295));
std::vector<RTP::Packet::Extension> extensions;
uint8_t extenLen;
uint8_t* bufferPtr{ ProbationPacketExtensionsBuffer };
{
extenLen = MidValue.size();
extensions.emplace_back(
RTC::RtpHeaderExtensionUri::Type::MID,
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::MID),
extenLen,
bufferPtr);
std::memcpy(bufferPtr, MidValue.c_str(), extenLen);
bufferPtr += extenLen;
}
{
extenLen = 3u;
extensions.emplace_back(
RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME,
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME),
extenLen,
bufferPtr);
bufferPtr += extenLen;
}
{
extenLen = 2u;
extensions.emplace_back(
RTC::RtpHeaderExtensionUri::Type::TRANSPORT_WIDE_CC_01,
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::TRANSPORT_WIDE_CC_01),
extenLen,
bufferPtr);
}
this->probationPacket->SetExtensions(RTP::Packet::ExtensionsType::OneByte, extensions);
this->probationPacketMinLength = this->probationPacket->GetLength();
}
ProbationGenerator::~ProbationGenerator()
{
MS_TRACE();
}
RTP::Packet* ProbationGenerator::GetNextPacket(size_t len)
{
MS_TRACE();
len = Utils::Byte::PadTo4Bytes(len);
if (len > ProbationGenerator::ProbationPacketMaxLength)
{
len = ProbationGenerator::ProbationPacketMaxLength;
}
else if (len < this->probationPacketMinLength)
{
len = this->probationPacketMinLength;
}
const auto seq = this->probationPacket->GetSequenceNumber() + 1;
const auto timestamp = this->probationPacket->GetTimestamp() + 20;
const size_t payloadLength = len - this->probationPacketMinLength;
this->probationPacket->SetSequenceNumber(seq);
this->probationPacket->SetTimestamp(timestamp);
this->probationPacket->SetPayloadLength(payloadLength);
return this->probationPacket.get();
}
} }