#define MS_CLASS "RTC::RtpProbationGenerator"
#include "RTC/RtpProbationGenerator.hpp"
#include "Logger.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp"
#include <cstring>
#include <vector>
namespace RTC
{
static const uint8_t ProbationPacketHeader[] =
{
0b10010000, 0b01111111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xBE, 0xDE, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0 };
static constexpr size_t ProbationPacketHeaderSize{ 32 };
static constexpr size_t MaxProbationPacketSize{ 1400u };
static const std::string MidValue{ "probator" };
RtpProbationGenerator::RtpProbationGenerator()
{
MS_TRACE();
this->probationPacketBuffer = new uint8_t[MaxProbationPacketSize];
std::memcpy(this->probationPacketBuffer, ProbationPacketHeader, ProbationPacketHeaderSize);
this->probationPacket =
RTC::RtpPacket::Parse(this->probationPacketBuffer, MaxProbationPacketSize);
this->probationPacket->SetPayloadType(RTC::RtpProbationCodecPayloadType);
this->probationPacket->SetSsrc(RTC::RtpProbationSsrc);
this->probationPacket->SetSequenceNumber(
static_cast<uint16_t>(Utils::Crypto::GetRandomUInt(0, 65535)));
this->probationPacket->SetTimestamp(Utils::Crypto::GetRandomUInt(0, 4294967295));
thread_local static uint8_t buffer[4096];
std::vector<RTC::RtpPacket::GenericExtension> extensions;
uint8_t extenLen;
uint8_t* bufferPtr{ buffer };
{
extenLen = MidValue.size();
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::MID), extenLen, bufferPtr);
std::memcpy(bufferPtr, MidValue.c_str(), extenLen);
bufferPtr += extenLen;
}
{
extenLen = 3u;
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME), extenLen, bufferPtr);
bufferPtr += extenLen;
}
{
extenLen = 2u;
extensions.emplace_back(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::TRANSPORT_WIDE_CC_01),
extenLen,
bufferPtr);
}
this->probationPacket->SetExtensions(1, extensions);
this->probationPacket->SetMidExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::MID));
this->probationPacket->SetAbsSendTimeExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::ABS_SEND_TIME));
this->probationPacket->SetTransportWideCc01ExtensionId(
static_cast<uint8_t>(RTC::RtpHeaderExtensionUri::Type::TRANSPORT_WIDE_CC_01));
}
RtpProbationGenerator::~RtpProbationGenerator()
{
MS_TRACE();
delete[] this->probationPacketBuffer;
delete this->probationPacket;
}
RTC::RtpPacket* RtpProbationGenerator::GetNextPacket(size_t size)
{
MS_TRACE();
if (size > MaxProbationPacketSize)
{
size = MaxProbationPacketSize;
}
else if (size < ProbationPacketHeaderSize)
{
size = ProbationPacketHeaderSize;
}
auto seq = this->probationPacket->GetSequenceNumber();
auto timestamp = this->probationPacket->GetTimestamp();
++seq;
timestamp += 20u;
this->probationPacket->SetSequenceNumber(seq);
this->probationPacket->SetTimestamp(timestamp);
this->probationPacket->SetPayloadLength(size - ProbationPacketHeaderSize);
return this->probationPacket;
}
}