#if RTC_ENABLE_MEDIA
#include "rtcpsrreporter.hpp"
namespace rtc {
ChainedOutgoingProduct RtcpSrReporter::processOutgoingBinaryMessage(ChainedMessagesProduct messages, message_ptr control) {
if (needsToReport) {
auto timestamp = rtpConfig->timestamp;
auto sr = getSenderReport(timestamp);
if (control) {
control->insert(control->end(), sr->begin(), sr->end());
} else {
control = sr;
}
needsToReport = false;
}
for (auto message: *messages) {
auto rtp = reinterpret_cast<RTP *>(message->data());
addToReport(rtp, message->size());
}
return {messages, control};
}
void RtcpSrReporter::startRecording() {
_previousReportedTimestamp = rtpConfig->timestamp;
timeOffset = rtpConfig->startTime_s - rtpConfig->timestampToSeconds(rtpConfig->timestamp);
}
void RtcpSrReporter::addToReport(RTP *rtp, uint32_t rtpSize) {
packetCount += 1;
assert(!rtp->padding());
payloadOctets += rtpSize - rtp->getSize();
}
RtcpSrReporter::RtcpSrReporter(std::shared_ptr<RtpPacketizationConfig> rtpConfig)
: MediaHandlerElement(), rtpConfig(rtpConfig) {}
uint64_t RtcpSrReporter::secondsToNTP(double seconds) {
return std::round(seconds * double(uint64_t(1) << 32));
}
void RtcpSrReporter::setNeedsToReport() { needsToReport = true; }
message_ptr RtcpSrReporter::getSenderReport(uint32_t timestamp) {
auto srSize = RTCP_SR::size(0);
auto msg = make_message(srSize + RTCP_SDES::size({{uint8_t(rtpConfig->cname.size())}}),
Message::Type::Control);
auto sr = reinterpret_cast<RTCP_SR *>(msg->data());
auto timestamp_s = rtpConfig->timestampToSeconds(timestamp);
auto currentTime = timeOffset + timestamp_s;
sr->setNtpTimestamp(secondsToNTP(currentTime));
sr->setRtpTimestamp(timestamp);
sr->setPacketCount(packetCount);
sr->setOctetCount(payloadOctets);
sr->preparePacket(rtpConfig->ssrc, 0);
auto sdes = reinterpret_cast<RTCP_SDES *>(msg->data() + srSize);
auto chunk = sdes->getChunk(0);
chunk->setSSRC(rtpConfig->ssrc);
auto item = chunk->getItem(0);
item->type = 1;
item->setText(rtpConfig->cname);
sdes->preparePacket(1);
_previousReportedTimestamp = timestamp;
return msg;
}
}
#endif