datachannel-sys 0.11.3

Native bindings to libdatachannel.
Documentation
/**
 * Copyright (c) 2020 Filip Klembara (in2core)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 */

#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;
}

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */