#define MS_CLASS "RTC::RtpCodecParameters"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp"
namespace RTC
{
RtpCodecParameters::RtpCodecParameters(json& data)
{
MS_TRACE();
if (!data.is_object())
MS_THROW_TYPE_ERROR("data is not an object");
auto jsonMimeTypeIt = data.find("mimeType");
auto jsonPayloadTypeIt = data.find("payloadType");
auto jsonClockRateIt = data.find("clockRate");
auto jsonChannelsIt = data.find("channels");
auto jsonParametersIt = data.find("parameters");
auto jsonRtcpFeedbackIt = data.find("rtcpFeedback");
if (jsonMimeTypeIt == data.end() || !jsonMimeTypeIt->is_string())
MS_THROW_TYPE_ERROR("missing mimeType");
this->mimeType.SetMimeType(jsonMimeTypeIt->get<std::string>());
if (
jsonPayloadTypeIt == data.end() ||
!Utils::Json::IsPositiveInteger(*jsonPayloadTypeIt)
)
{
MS_THROW_TYPE_ERROR("missing payloadType");
}
this->payloadType = jsonPayloadTypeIt->get<uint8_t>();
if (
jsonClockRateIt == data.end() ||
!Utils::Json::IsPositiveInteger(*jsonClockRateIt)
)
{
MS_THROW_TYPE_ERROR("missing clockRate");
}
this->clockRate = jsonClockRateIt->get<uint32_t>();
if (
jsonChannelsIt != data.end() &&
Utils::Json::IsPositiveInteger(*jsonChannelsIt)
)
{
this->channels = jsonChannelsIt->get<uint8_t>();
}
if (jsonParametersIt != data.end() && jsonParametersIt->is_object())
this->parameters.Set(*jsonParametersIt);
if (jsonRtcpFeedbackIt != data.end() && jsonRtcpFeedbackIt->is_array())
{
this->rtcpFeedback.reserve(jsonRtcpFeedbackIt->size());
for (auto& entry : *jsonRtcpFeedbackIt)
{
this->rtcpFeedback.emplace_back(entry);
}
}
CheckCodec();
}
void RtpCodecParameters::FillJson(json& jsonObject) const
{
MS_TRACE();
jsonObject["mimeType"] = this->mimeType.ToString();
jsonObject["payloadType"] = this->payloadType;
jsonObject["clockRate"] = this->clockRate;
if (this->channels > 1)
jsonObject["channels"] = this->channels;
this->parameters.FillJson(jsonObject["parameters"]);
jsonObject["rtcpFeedback"] = json::array();
auto jsonRtcpFeedbackIt = jsonObject.find("rtcpFeedback");
for (size_t i{ 0 }; i < this->rtcpFeedback.size(); ++i)
{
jsonRtcpFeedbackIt->emplace_back(json::value_t::object);
auto& jsonEntry = (*jsonRtcpFeedbackIt)[i];
auto& fb = this->rtcpFeedback[i];
fb.FillJson(jsonEntry);
}
}
inline void RtpCodecParameters::CheckCodec()
{
MS_TRACE();
static std::string aptString{ "apt" };
switch (this->mimeType.subtype)
{
case RTC::RtpCodecMimeType::Subtype::RTX:
{
if (!this->parameters.HasPositiveInteger(aptString))
MS_THROW_TYPE_ERROR("missing apt parameter in RTX codec");
break;
}
default:;
}
}
}