|
@@ -21,23 +21,9 @@
|
|
|
#ifndef RTC_RTP_HPP
|
|
|
#define RTC_RTP_HPP
|
|
|
|
|
|
-#include "log.hpp"
|
|
|
+#include "common.hpp"
|
|
|
|
|
|
-#include <cmath>
|
|
|
-
|
|
|
-#ifdef _WIN32
|
|
|
-#include <winsock2.h>
|
|
|
-#else
|
|
|
-#include <arpa/inet.h>
|
|
|
-#endif
|
|
|
-
|
|
|
-#ifndef htonll
|
|
|
-#define htonll(x) \
|
|
|
- ((uint64_t)(((uint64_t)htonl((uint32_t)(x))) << 32) | (uint64_t)htonl((uint32_t)((x) >> 32)))
|
|
|
-#endif
|
|
|
-#ifndef ntohll
|
|
|
-#define ntohll(x) htonll(x)
|
|
|
-#endif
|
|
|
+#include <vector>
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
@@ -45,63 +31,40 @@ typedef uint32_t SSRC;
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
-struct RTP {
|
|
|
-private:
|
|
|
+struct RTC_CPP_EXPORT RTP {
|
|
|
uint8_t _first;
|
|
|
uint8_t _payloadType;
|
|
|
uint16_t _seqNumber;
|
|
|
uint32_t _timestamp;
|
|
|
SSRC _ssrc;
|
|
|
-
|
|
|
-public:
|
|
|
- SSRC csrc[16];
|
|
|
-
|
|
|
- inline uint8_t version() const { return _first >> 6; }
|
|
|
- inline bool padding() const { return (_first >> 5) & 0x01; }
|
|
|
- inline bool extension() const { return (_first >> 4) & 0x01; }
|
|
|
- inline uint8_t csrcCount() const { return _first & 0x0F; }
|
|
|
- inline uint8_t marker() const { return _payloadType & 0b10000000; }
|
|
|
- inline uint8_t payloadType() const { return _payloadType & 0b01111111; }
|
|
|
- inline uint16_t seqNumber() const { return ntohs(_seqNumber); }
|
|
|
- inline uint32_t timestamp() const { return ntohl(_timestamp); }
|
|
|
- inline uint32_t ssrc() const { return ntohl(_ssrc); }
|
|
|
-
|
|
|
- inline size_t getSize() const {
|
|
|
- return reinterpret_cast<const char *>(&csrc) - reinterpret_cast<const char *>(this) +
|
|
|
- sizeof(SSRC) * csrcCount();
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] char *getBody() {
|
|
|
- return reinterpret_cast<char *>(&csrc) + sizeof(SSRC) * csrcCount();
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] const char *getBody() const {
|
|
|
- return reinterpret_cast<const char *>(&csrc) + sizeof(SSRC) * csrcCount();
|
|
|
- }
|
|
|
-
|
|
|
- inline void preparePacket() { _first |= (1 << 7); }
|
|
|
-
|
|
|
- inline void setSeqNumber(uint16_t newSeqNo) { _seqNumber = htons(newSeqNo); }
|
|
|
- inline void setPayloadType(uint8_t newPayloadType) {
|
|
|
- _payloadType = (_payloadType & 0b10000000u) | (0b01111111u & newPayloadType);
|
|
|
- }
|
|
|
- inline void setSsrc(uint32_t in_ssrc) { _ssrc = htonl(in_ssrc); }
|
|
|
- inline void setMarker(bool marker) { _payloadType = (_payloadType & 0x7F) | (marker << 7); };
|
|
|
-
|
|
|
- void setTimestamp(uint32_t i) { _timestamp = htonl(i); }
|
|
|
-
|
|
|
- void log() {
|
|
|
- PLOG_VERBOSE << "RTP V: " << (int)version() << " P: " << (padding() ? "P" : " ")
|
|
|
- << " X: " << (extension() ? "X" : " ") << " CC: " << (int)csrcCount()
|
|
|
- << " M: " << (marker() ? "M" : " ") << " PT: " << (int)payloadType()
|
|
|
- << " SEQNO: " << seqNumber() << " TS: " << timestamp();
|
|
|
- }
|
|
|
+ SSRC _csrc[16];
|
|
|
+
|
|
|
+ [[nodiscard]] uint8_t version() const;
|
|
|
+ [[nodiscard]] bool padding() const;
|
|
|
+ [[nodiscard]] bool extension() const;
|
|
|
+ [[nodiscard]] uint8_t csrcCount() const;
|
|
|
+ [[nodiscard]] uint8_t marker() const;
|
|
|
+ [[nodiscard]] uint8_t payloadType() const;
|
|
|
+ [[nodiscard]] uint16_t seqNumber() const;
|
|
|
+ [[nodiscard]] uint32_t timestamp() const;
|
|
|
+ [[nodiscard]] uint32_t ssrc() const;
|
|
|
+
|
|
|
+ [[nodiscard]] size_t getSize() const;
|
|
|
+ [[nodiscard]] const char *getBody() const;
|
|
|
+ [[nodiscard]] char *getBody();
|
|
|
+
|
|
|
+ void log() const;
|
|
|
+
|
|
|
+ void preparePacket();
|
|
|
+ void setSeqNumber(uint16_t newSeqNo);
|
|
|
+ void setPayloadType(uint8_t newPayloadType);
|
|
|
+ void setSsrc(uint32_t in_ssrc);
|
|
|
+ void setMarker(bool marker);
|
|
|
+ void setTimestamp(uint32_t i);
|
|
|
};
|
|
|
|
|
|
-struct RTCP_ReportBlock {
|
|
|
- SSRC ssrc;
|
|
|
-
|
|
|
-private:
|
|
|
+struct RTC_CPP_EXPORT RTCP_ReportBlock {
|
|
|
+ SSRC _ssrc;
|
|
|
uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
|
|
|
uint16_t _seqNoCycles;
|
|
|
uint16_t _highestSeqNo;
|
|
@@ -109,136 +72,68 @@ private:
|
|
|
uint32_t _lastReport;
|
|
|
uint32_t _delaySinceLastReport;
|
|
|
|
|
|
-public:
|
|
|
- inline void preparePacket(SSRC in_ssrc, [[maybe_unused]] unsigned int packetsLost,
|
|
|
- [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
|
|
|
- uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
|
|
|
- uint64_t lastSR_DELAY) {
|
|
|
- setSeqNo(highestSeqNo, seqNoCycles);
|
|
|
- setJitter(jitter);
|
|
|
- setSSRC(in_ssrc);
|
|
|
-
|
|
|
- // Middle 32 bits of NTP Timestamp
|
|
|
- // this->lastReport = lastSR_NTP >> 16u;
|
|
|
- setNTPOfSR(uint64_t(lastSR_NTP));
|
|
|
- setDelaySinceSR(uint32_t(lastSR_DELAY));
|
|
|
-
|
|
|
- // The delay, expressed in units of 1/65536 seconds
|
|
|
- // this->delaySinceLastReport = lastSR_DELAY;
|
|
|
- }
|
|
|
-
|
|
|
- inline void setSSRC(SSRC in_ssrc) { this->ssrc = htonl(in_ssrc); }
|
|
|
- [[nodiscard]] inline SSRC getSSRC() const { return ntohl(ssrc); }
|
|
|
-
|
|
|
- inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost,
|
|
|
- [[maybe_unused]] unsigned int totalPackets) {
|
|
|
- // TODO Implement loss percentages.
|
|
|
- _fractionLostAndPacketsLost = 0;
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] inline unsigned int getLossPercentage() const {
|
|
|
- // TODO Implement loss percentages.
|
|
|
- return 0;
|
|
|
- }
|
|
|
- [[nodiscard]] inline unsigned int getPacketLostCount() const {
|
|
|
- // TODO Implement total packets lost.
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- inline uint16_t seqNoCycles() const { return ntohs(_seqNoCycles); }
|
|
|
- inline uint16_t highestSeqNo() const { return ntohs(_highestSeqNo); }
|
|
|
- inline uint32_t jitter() const { return ntohl(_jitter); }
|
|
|
-
|
|
|
- inline void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
|
|
|
- _highestSeqNo = htons(highestSeqNo);
|
|
|
- _seqNoCycles = htons(seqNoCycles);
|
|
|
- }
|
|
|
-
|
|
|
- inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
|
|
|
-
|
|
|
- inline void setNTPOfSR(uint64_t ntp) { _lastReport = htonll(ntp >> 16u); }
|
|
|
- [[nodiscard]] inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
|
|
|
-
|
|
|
- inline void setDelaySinceSR(uint32_t sr) {
|
|
|
- // The delay, expressed in units of 1/65536 seconds
|
|
|
- _delaySinceLastReport = htonl(sr);
|
|
|
- }
|
|
|
- [[nodiscard]] inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
|
|
|
-
|
|
|
- inline void log() const {
|
|
|
- PLOG_VERBOSE << "RTCP report block: "
|
|
|
- << "ssrc="
|
|
|
- << ntohl(ssrc)
|
|
|
- // TODO: Implement these reports
|
|
|
- // << ", fractionLost=" << fractionLost
|
|
|
- // << ", packetsLost=" << packetsLost
|
|
|
- << ", highestSeqNo=" << highestSeqNo() << ", seqNoCycles=" << seqNoCycles()
|
|
|
- << ", jitter=" << jitter() << ", lastSR=" << getNTPOfSR()
|
|
|
- << ", lastSRDelay=" << getDelaySinceSR();
|
|
|
- }
|
|
|
+ [[nodiscard]] uint16_t seqNoCycles() const;
|
|
|
+ [[nodiscard]] uint16_t highestSeqNo() const;
|
|
|
+ [[nodiscard]] uint32_t jitter() const;
|
|
|
+ [[nodiscard]] uint32_t delaySinceSR() const;
|
|
|
+
|
|
|
+ [[nodiscard]] SSRC getSSRC() const;
|
|
|
+ [[nodiscard]] uint32_t getNTPOfSR() const;
|
|
|
+ [[nodiscard]] unsigned int getLossPercentage() const;
|
|
|
+ [[nodiscard]] unsigned int getPacketLostCount() const;
|
|
|
+
|
|
|
+ void preparePacket(SSRC in_ssrc, unsigned int packetsLost, unsigned int totalPackets,
|
|
|
+ uint16_t highestSeqNo, uint16_t seqNoCycles, uint32_t jitter,
|
|
|
+ uint64_t lastSR_NTP, uint64_t lastSR_DELAY);
|
|
|
+ void setSSRC(SSRC in_ssrc);
|
|
|
+ void setPacketsLost(unsigned int packetsLost, unsigned int totalPackets);
|
|
|
+ void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles);
|
|
|
+ void setJitter(uint32_t jitter);
|
|
|
+ void setNTPOfSR(uint64_t ntp);
|
|
|
+ void setDelaySinceSR(uint32_t sr);
|
|
|
+
|
|
|
+ void log() const;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_HEADER {
|
|
|
-private:
|
|
|
+struct RTC_CPP_EXPORT RTCP_HEADER {
|
|
|
uint8_t _first;
|
|
|
uint8_t _payloadType;
|
|
|
uint16_t _length;
|
|
|
|
|
|
-public:
|
|
|
- inline uint8_t version() const { return _first >> 6; }
|
|
|
- inline bool padding() const { return (_first >> 5) & 0x01; }
|
|
|
- inline uint8_t reportCount() const { return _first & 0x0F; }
|
|
|
- inline uint8_t payloadType() const { return _payloadType; }
|
|
|
- inline uint16_t length() const { return ntohs(_length); }
|
|
|
- inline size_t lengthInBytes() const { return (1 + length()) * 4; }
|
|
|
-
|
|
|
- inline void setPayloadType(uint8_t type) { _payloadType = type; }
|
|
|
- inline void setReportCount(uint8_t count) {
|
|
|
- _first = (_first & 0b11100000u) | (count & 0b00011111u);
|
|
|
- }
|
|
|
- inline void setLength(uint16_t length) { _length = htons(length); }
|
|
|
-
|
|
|
- inline void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
|
|
|
- _first = 0b10000000; // version 2, no padding
|
|
|
- setReportCount(reportCount);
|
|
|
- setPayloadType(payloadType);
|
|
|
- setLength(length);
|
|
|
- }
|
|
|
-
|
|
|
- inline void log() const {
|
|
|
- PLOG_VERBOSE << "RTCP header: "
|
|
|
- << "version=" << unsigned(version()) << ", padding=" << padding()
|
|
|
- << ", reportCount=" << unsigned(reportCount())
|
|
|
- << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
|
|
|
- }
|
|
|
+ [[nodiscard]] uint8_t version() const;
|
|
|
+ [[nodiscard]] bool padding() const;
|
|
|
+ [[nodiscard]] uint8_t reportCount() const;
|
|
|
+ [[nodiscard]] uint8_t payloadType() const;
|
|
|
+ [[nodiscard]] uint16_t length() const;
|
|
|
+ [[nodiscard]] size_t lengthInBytes() const;
|
|
|
+
|
|
|
+ void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length);
|
|
|
+ void setPayloadType(uint8_t type);
|
|
|
+ void setReportCount(uint8_t count);
|
|
|
+ void setLength(uint16_t length);
|
|
|
+
|
|
|
+ void log() const;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_FB_HEADER {
|
|
|
+struct RTC_CPP_EXPORT RTCP_FB_HEADER {
|
|
|
RTCP_HEADER header;
|
|
|
- SSRC packetSender;
|
|
|
- SSRC mediaSource;
|
|
|
|
|
|
- [[nodiscard]] SSRC getPacketSenderSSRC() const { return ntohl(packetSender); }
|
|
|
+ SSRC _packetSender;
|
|
|
+ SSRC _mediaSource;
|
|
|
|
|
|
- [[nodiscard]] SSRC getMediaSourceSSRC() const { return ntohl(mediaSource); }
|
|
|
+ [[nodiscard]] SSRC packetSenderSSRC() const;
|
|
|
+ [[nodiscard]] SSRC mediaSourceSSRC() const;
|
|
|
|
|
|
- void setPacketSenderSSRC(SSRC ssrc) { this->packetSender = htonl(ssrc); }
|
|
|
+ void setPacketSenderSSRC(SSRC ssrc);
|
|
|
+ void setMediaSourceSSRC(SSRC ssrc);
|
|
|
|
|
|
- void setMediaSourceSSRC(SSRC ssrc) { this->mediaSource = htonl(ssrc); }
|
|
|
-
|
|
|
- void log() {
|
|
|
- header.log();
|
|
|
- PLOG_VERBOSE << "FB: "
|
|
|
- << " packet sender: " << getPacketSenderSSRC()
|
|
|
- << " media source: " << getMediaSourceSSRC();
|
|
|
- }
|
|
|
+ void log() const;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_SR {
|
|
|
+struct RTC_CPP_EXPORT RTCP_SR {
|
|
|
RTCP_HEADER header;
|
|
|
- SSRC _senderSSRC;
|
|
|
|
|
|
-private:
|
|
|
+ SSRC _senderSSRC;
|
|
|
uint64_t _ntpTimestamp;
|
|
|
uint32_t _rtpTimestamp;
|
|
|
uint32_t _packetCount;
|
|
@@ -246,418 +141,175 @@ private:
|
|
|
|
|
|
RTCP_ReportBlock _reportBlocks;
|
|
|
|
|
|
-public:
|
|
|
- inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
|
|
|
- unsigned int length =
|
|
|
- ((sizeof(header) + 24 + reportCount * sizeof(RTCP_ReportBlock)) / 4) - 1;
|
|
|
- header.prepareHeader(200, reportCount, uint16_t(length));
|
|
|
- this->_senderSSRC = htonl(senderSSRC);
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
|
|
|
- [[nodiscard]] inline const RTCP_ReportBlock *getReportBlock(int num) const {
|
|
|
- return &_reportBlocks + num;
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] static unsigned int size(unsigned int reportCount) {
|
|
|
- return sizeof(RTCP_HEADER) + 24 + reportCount * sizeof(RTCP_ReportBlock);
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] inline size_t getSize() const {
|
|
|
- // "length" in packet is one less than the number of 32 bit words in the packet.
|
|
|
- return sizeof(uint32_t) * (1 + size_t(header.length()));
|
|
|
- }
|
|
|
-
|
|
|
- inline uint64_t ntpTimestamp() const { return ntohll(_ntpTimestamp); }
|
|
|
- inline uint32_t rtpTimestamp() const { return ntohl(_rtpTimestamp); }
|
|
|
- inline uint32_t packetCount() const { return ntohl(_packetCount); }
|
|
|
- inline uint32_t octetCount() const { return ntohl(_octetCount); }
|
|
|
- inline uint32_t senderSSRC() const { return ntohl(_senderSSRC); }
|
|
|
-
|
|
|
- inline void setNtpTimestamp(uint64_t ts) { _ntpTimestamp = htonll(ts); }
|
|
|
- inline void setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
|
|
|
- inline void setOctetCount(uint32_t ts) { _octetCount = htonl(ts); }
|
|
|
- inline void setPacketCount(uint32_t ts) { _packetCount = htonl(ts); }
|
|
|
-
|
|
|
- inline void log() const {
|
|
|
- header.log();
|
|
|
- PLOG_VERBOSE << "RTCP SR: "
|
|
|
- << " SSRC=" << senderSSRC() << ", NTP_TS=" << ntpTimestamp()
|
|
|
- << ", RTP_TS=" << rtpTimestamp() << ", packetCount=" << packetCount()
|
|
|
- << ", octetCount=" << octetCount();
|
|
|
-
|
|
|
- for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
|
|
|
- getReportBlock(i)->log();
|
|
|
- }
|
|
|
- }
|
|
|
+ [[nodiscard]] static unsigned int Size(unsigned int reportCount);
|
|
|
+
|
|
|
+ [[nodiscard]] uint64_t ntpTimestamp() const;
|
|
|
+ [[nodiscard]] uint32_t rtpTimestamp() const;
|
|
|
+ [[nodiscard]] uint32_t packetCount() const;
|
|
|
+ [[nodiscard]] uint32_t octetCount() const;
|
|
|
+ [[nodiscard]] uint32_t senderSSRC() const;
|
|
|
+
|
|
|
+ [[nodiscard]] const RTCP_ReportBlock *getReportBlock(int num) const;
|
|
|
+ [[nodiscard]] RTCP_ReportBlock *getReportBlock(int num);
|
|
|
+ [[nodiscard]] unsigned int size(unsigned int reportCount);
|
|
|
+ [[nodiscard]] size_t getSize() const;
|
|
|
+
|
|
|
+ void preparePacket(SSRC senderSSRC, uint8_t reportCount);
|
|
|
+ void setNtpTimestamp(uint64_t ts);
|
|
|
+ void setRtpTimestamp(uint32_t ts);
|
|
|
+ void setOctetCount(uint32_t ts);
|
|
|
+ void setPacketCount(uint32_t ts);
|
|
|
+
|
|
|
+ void log() const;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_SDES_ITEM {
|
|
|
-public:
|
|
|
+struct RTC_CPP_EXPORT RTCP_SDES_ITEM {
|
|
|
uint8_t type;
|
|
|
|
|
|
-private:
|
|
|
uint8_t _length;
|
|
|
char _text[1];
|
|
|
|
|
|
-public:
|
|
|
- inline std::string text() const { return std::string(_text, _length); }
|
|
|
- inline void setText(std::string text) {
|
|
|
- if(text.size() > 0xFF)
|
|
|
- throw std::invalid_argument("text is too long");
|
|
|
+ [[nodiscard]] static unsigned int Size(uint8_t textLength);
|
|
|
|
|
|
- _length = uint8_t(text.size());
|
|
|
- memcpy(_text, text.data(), text.size());
|
|
|
- }
|
|
|
+ [[nodiscard]] string text() const;
|
|
|
+ [[nodiscard]] uint8_t length() const;
|
|
|
|
|
|
- inline uint8_t length() { return _length; }
|
|
|
-
|
|
|
- [[nodiscard]] static unsigned int size(uint8_t textLength) { return textLength + 2; }
|
|
|
+ void setText(string text);
|
|
|
};
|
|
|
|
|
|
struct RTCP_SDES_CHUNK {
|
|
|
-private:
|
|
|
SSRC _ssrc;
|
|
|
RTCP_SDES_ITEM _items;
|
|
|
|
|
|
-public:
|
|
|
- inline SSRC ssrc() const { return ntohl(_ssrc); }
|
|
|
- inline void setSSRC(SSRC ssrc) { _ssrc = htonl(ssrc); }
|
|
|
-
|
|
|
- /// Get item at given index
|
|
|
- /// @note All items with index < `num` must be valid, otherwise this function has undefined
|
|
|
- /// behaviour (use `safelyCountChunkSize` to check if chunk is valid)
|
|
|
- /// @param num Index of item to return
|
|
|
- inline RTCP_SDES_ITEM *getItem(int num) {
|
|
|
- auto base = &_items;
|
|
|
- while (num-- > 0) {
|
|
|
- auto itemSize = RTCP_SDES_ITEM::size(base->length());
|
|
|
- base = reinterpret_cast<RTCP_SDES_ITEM *>(reinterpret_cast<uint8_t *>(base) + itemSize);
|
|
|
- }
|
|
|
- return reinterpret_cast<RTCP_SDES_ITEM *>(base);
|
|
|
- }
|
|
|
-
|
|
|
- long safelyCountChunkSize(size_t maxChunkSize) {
|
|
|
- if (maxChunkSize < RTCP_SDES_CHUNK::size({})) {
|
|
|
- // chunk is truncated
|
|
|
- return -1;
|
|
|
- } else {
|
|
|
- size_t size = sizeof(SSRC);
|
|
|
- unsigned int i = 0;
|
|
|
- // We can always access first 4 bytes of first item (in case of no items there will be 4
|
|
|
- // null bytes)
|
|
|
- auto item = getItem(i);
|
|
|
- std::vector<uint8_t> textsLength{};
|
|
|
- while (item->type != 0) {
|
|
|
- if (size + RTCP_SDES_ITEM::size(0) > maxChunkSize) {
|
|
|
- // item is too short
|
|
|
- return -1;
|
|
|
- }
|
|
|
- auto itemLength = item->length();
|
|
|
- if (size + RTCP_SDES_ITEM::size(itemLength) >= maxChunkSize) {
|
|
|
- // item is too large (it can't be equal to chunk size because after item there
|
|
|
- // must be 1-4 null bytes as padding)
|
|
|
- return -1;
|
|
|
- }
|
|
|
- textsLength.push_back(itemLength);
|
|
|
- // safely to access next item
|
|
|
- item = getItem(++i);
|
|
|
- }
|
|
|
- auto realSize = RTCP_SDES_CHUNK::size(textsLength);
|
|
|
- if (realSize > maxChunkSize) {
|
|
|
- // Chunk is too large
|
|
|
- return -1;
|
|
|
- }
|
|
|
- return realSize;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] static unsigned int size(const std::vector<uint8_t> textLengths) {
|
|
|
- unsigned int itemsSize = 0;
|
|
|
- for (auto length : textLengths) {
|
|
|
- itemsSize += RTCP_SDES_ITEM::size(length);
|
|
|
- }
|
|
|
- auto nullTerminatedItemsSize = itemsSize + 1;
|
|
|
- auto words = uint8_t(std::ceil(double(nullTerminatedItemsSize) / 4)) + 1;
|
|
|
- return words * 4;
|
|
|
- }
|
|
|
-
|
|
|
- /// Get size of chunk
|
|
|
- /// @note All items must be valid, otherwise this function has undefined behaviour (use
|
|
|
- /// `safelyCountChunkSize` to check if chunk is valid)
|
|
|
- [[nodiscard]] unsigned int getSize() {
|
|
|
- std::vector<uint8_t> textLengths{};
|
|
|
- unsigned int i = 0;
|
|
|
- auto item = getItem(i);
|
|
|
- while (item->type != 0) {
|
|
|
- textLengths.push_back(item->length());
|
|
|
- item = getItem(++i);
|
|
|
- }
|
|
|
- return size(textLengths);
|
|
|
- }
|
|
|
-};
|
|
|
+ [[nodiscard]] static unsigned int Size(const std::vector<uint8_t> textLengths);
|
|
|
|
|
|
-struct RTCP_SDES {
|
|
|
- RTCP_HEADER header;
|
|
|
+ [[nodiscard]] SSRC ssrc() const;
|
|
|
|
|
|
-private:
|
|
|
- RTCP_SDES_CHUNK _chunks;
|
|
|
-
|
|
|
-public:
|
|
|
- inline void preparePacket(uint8_t chunkCount) {
|
|
|
- unsigned int chunkSize = 0;
|
|
|
- for (uint8_t i = 0; i < chunkCount; i++) {
|
|
|
- auto chunk = getChunk(i);
|
|
|
- chunkSize += chunk->getSize();
|
|
|
- }
|
|
|
- uint16_t length = uint16_t((sizeof(header) + chunkSize) / 4 - 1);
|
|
|
- header.prepareHeader(202, chunkCount, length);
|
|
|
- }
|
|
|
-
|
|
|
- bool isValid() {
|
|
|
- auto chunksSize = header.lengthInBytes() - sizeof(header);
|
|
|
- if (chunksSize == 0) {
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- // there is at least one chunk
|
|
|
- unsigned int i = 0;
|
|
|
- unsigned int size = 0;
|
|
|
- while (size < chunksSize) {
|
|
|
- if (chunksSize < size + RTCP_SDES_CHUNK::size({})) {
|
|
|
- // chunk is truncated
|
|
|
- return false;
|
|
|
- }
|
|
|
- auto chunk = getChunk(i++);
|
|
|
- auto chunkSize = chunk->safelyCountChunkSize(chunksSize - size);
|
|
|
- if (chunkSize < 0) {
|
|
|
- // chunk is invalid
|
|
|
- return false;
|
|
|
- }
|
|
|
- size += chunkSize;
|
|
|
- }
|
|
|
- return size == chunksSize;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// Returns number of chunks in this packet
|
|
|
- /// @note Returns 0 if packet is invalid
|
|
|
- inline unsigned int chunksCount() {
|
|
|
- if (!isValid()) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- uint16_t chunksSize = 4 * (header.length() + 1) - sizeof(header);
|
|
|
- unsigned int size = 0;
|
|
|
- unsigned int i = 0;
|
|
|
- while (size < chunksSize) {
|
|
|
- size += getChunk(i++)->getSize();
|
|
|
- }
|
|
|
- return i;
|
|
|
- }
|
|
|
-
|
|
|
- /// Get chunk at given index
|
|
|
- /// @note All chunks (and their items) with index < `num` must be valid, otherwise this function
|
|
|
- /// has undefined behaviour (use `isValid` to check if chunk is valid)
|
|
|
- /// @param num Index of chunk to return
|
|
|
- inline RTCP_SDES_CHUNK *getChunk(int num) {
|
|
|
- auto base = &_chunks;
|
|
|
- while (num-- > 0) {
|
|
|
- auto chunkSize = base->getSize();
|
|
|
- base =
|
|
|
- reinterpret_cast<RTCP_SDES_CHUNK *>(reinterpret_cast<uint8_t *>(base) + chunkSize);
|
|
|
- }
|
|
|
- return reinterpret_cast<RTCP_SDES_CHUNK *>(base);
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] static unsigned int size(const std::vector<std::vector<uint8_t>> lengths) {
|
|
|
- unsigned int chunks_size = 0;
|
|
|
- for (auto length : lengths) {
|
|
|
- chunks_size += RTCP_SDES_CHUNK::size(length);
|
|
|
- }
|
|
|
- return 4 + chunks_size;
|
|
|
- }
|
|
|
-};
|
|
|
+ void setSSRC(SSRC ssrc);
|
|
|
|
|
|
-struct RTCP_RR {
|
|
|
- RTCP_HEADER header;
|
|
|
- SSRC _senderSSRC;
|
|
|
+ // Get item at given index
|
|
|
+ // All items with index < num must be valid, otherwise this function has undefined behaviour
|
|
|
+ // (use safelyCountChunkSize() to check if chunk is valid).
|
|
|
+ [[nodiscard]] const RTCP_SDES_ITEM *getItem(int num) const;
|
|
|
+ [[nodiscard]] RTCP_SDES_ITEM *getItem(int num);
|
|
|
|
|
|
-private:
|
|
|
- RTCP_ReportBlock _reportBlocks;
|
|
|
+ // Get size of chunk
|
|
|
+ // All items must be valid, otherwise this function has undefined behaviour (use
|
|
|
+ // safelyCountChunkSize() to check if chunk is valid)
|
|
|
+ [[nodiscard]] unsigned int getSize() const;
|
|
|
|
|
|
-public:
|
|
|
- [[nodiscard]] inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
|
|
|
- [[nodiscard]] inline const RTCP_ReportBlock *getReportBlock(int num) const {
|
|
|
- return &_reportBlocks + num;
|
|
|
- }
|
|
|
-
|
|
|
- inline SSRC senderSSRC() const { return ntohl(_senderSSRC); }
|
|
|
- inline void setSenderSSRC(SSRC ssrc) { this->_senderSSRC = htonl(ssrc); }
|
|
|
-
|
|
|
- [[nodiscard]] inline size_t getSize() const {
|
|
|
- // "length" in packet is one less than the number of 32 bit words in the packet.
|
|
|
- return sizeof(uint32_t) * (1 + size_t(header.length()));
|
|
|
- }
|
|
|
+ long safelyCountChunkSize(size_t maxChunkSize) const;
|
|
|
+};
|
|
|
|
|
|
- inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
|
|
|
- // "length" in packet is one less than the number of 32 bit words in the packet.
|
|
|
- size_t length = (sizeWithReportBlocks(reportCount) / 4) - 1;
|
|
|
- header.prepareHeader(201, reportCount, uint16_t(length));
|
|
|
- this->_senderSSRC = htonl(senderSSRC);
|
|
|
- }
|
|
|
+struct RTC_CPP_EXPORT RTCP_SDES {
|
|
|
+ RTCP_HEADER header;
|
|
|
+ RTCP_SDES_CHUNK _chunks;
|
|
|
|
|
|
- inline static size_t sizeWithReportBlocks(uint8_t reportCount) {
|
|
|
- return sizeof(header) + 4 + size_t(reportCount) * sizeof(RTCP_ReportBlock);
|
|
|
- }
|
|
|
+ [[nodiscard]] static unsigned int Size(const std::vector<std::vector<uint8_t>> lengths);
|
|
|
|
|
|
- inline bool isSenderReport() { return header.payloadType() == 200; }
|
|
|
+ bool isValid() const;
|
|
|
|
|
|
- inline bool isReceiverReport() { return header.payloadType() == 201; }
|
|
|
+ // Returns number of chunks in this packet
|
|
|
+ // Returns 0 if packet is invalid
|
|
|
+ unsigned int chunksCount() const;
|
|
|
|
|
|
- inline void log() const {
|
|
|
- header.log();
|
|
|
- PLOG_VERBOSE << "RTCP RR: "
|
|
|
- << " SSRC=" << ntohl(_senderSSRC);
|
|
|
+ // Get chunk at given index
|
|
|
+ // All chunks (and their items) with index < `num` must be valid, otherwise this function has
|
|
|
+ // undefined behaviour (use `isValid` to check if chunk is valid).
|
|
|
+ const RTCP_SDES_CHUNK *getChunk(int num) const;
|
|
|
+ RTCP_SDES_CHUNK *getChunk(int num);
|
|
|
|
|
|
- for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
|
|
|
- getReportBlock(i)->log();
|
|
|
- }
|
|
|
- }
|
|
|
+ void preparePacket(uint8_t chunkCount);
|
|
|
};
|
|
|
|
|
|
-struct RTCP_REMB {
|
|
|
- RTCP_FB_HEADER header;
|
|
|
-
|
|
|
- /*! \brief Unique identifier ('R' 'E' 'M' 'B') */
|
|
|
- char id[4];
|
|
|
-
|
|
|
- /*! \brief Num SSRC, Br Exp, Br Mantissa (bit mask) */
|
|
|
- uint32_t bitrate;
|
|
|
-
|
|
|
- SSRC ssrc[1];
|
|
|
-
|
|
|
- [[nodiscard]] unsigned int getSize() const {
|
|
|
- // "length" in packet is one less than the number of 32 bit words in the packet.
|
|
|
- return sizeof(uint32_t) * (1 + header.header.length());
|
|
|
- }
|
|
|
+struct RTC_CPP_EXPORT RTCP_RR {
|
|
|
+ RTCP_HEADER header;
|
|
|
|
|
|
- void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate) {
|
|
|
+ SSRC _senderSSRC;
|
|
|
+ RTCP_ReportBlock _reportBlocks;
|
|
|
|
|
|
- // Report Count becomes the format here.
|
|
|
- header.header.prepareHeader(206, 15, 0);
|
|
|
+ [[nodiscard]] static size_t SizeWithReportBlocks(uint8_t reportCount);
|
|
|
|
|
|
- // Always zero.
|
|
|
- header.setMediaSourceSSRC(0);
|
|
|
+ SSRC senderSSRC() const;
|
|
|
+ bool isSenderReport();
|
|
|
+ bool isReceiverReport();
|
|
|
|
|
|
- header.setPacketSenderSSRC(senderSSRC);
|
|
|
+ [[nodiscard]] RTCP_ReportBlock *getReportBlock(int num);
|
|
|
+ [[nodiscard]] const RTCP_ReportBlock *getReportBlock(int num) const;
|
|
|
+ [[nodiscard]] size_t getSize() const;
|
|
|
|
|
|
- id[0] = 'R';
|
|
|
- id[1] = 'E';
|
|
|
- id[2] = 'M';
|
|
|
- id[3] = 'B';
|
|
|
+ void preparePacket(SSRC senderSSRC, uint8_t reportCount);
|
|
|
+ void setSenderSSRC(SSRC ssrc);
|
|
|
|
|
|
- setBitrate(numSSRC, in_bitrate);
|
|
|
- }
|
|
|
+ void log() const;
|
|
|
+};
|
|
|
|
|
|
- void setBitrate(unsigned int numSSRC, unsigned int in_bitrate) {
|
|
|
- unsigned int exp = 0;
|
|
|
- while (in_bitrate > pow(2, 18) - 1) {
|
|
|
- exp++;
|
|
|
- in_bitrate /= 2;
|
|
|
- }
|
|
|
+struct RTC_CPP_EXPORT RTCP_REMB {
|
|
|
+ RTCP_FB_HEADER header;
|
|
|
|
|
|
- // "length" in packet is one less than the number of 32 bit words in the packet.
|
|
|
- header.header.setLength(
|
|
|
- uint16_t((offsetof(RTCP_REMB, ssrc) / sizeof(uint32_t)) - 1 + numSSRC));
|
|
|
+ char _id[4]; // Unique identifier ('R' 'E' 'M' 'B')
|
|
|
+ uint32_t _bitrate; // Num SSRC, Br Exp, Br Mantissa (bit mask)
|
|
|
+ SSRC _ssrc[1];
|
|
|
|
|
|
- this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | in_bitrate);
|
|
|
- }
|
|
|
+ [[nodiscard]] static size_t SizeWithSSRCs(int count);
|
|
|
|
|
|
- void setSsrc(int iterator, SSRC newSssrc) { ssrc[iterator] = htonl(newSssrc); }
|
|
|
+ [[nodiscard]] unsigned int getSize() const;
|
|
|
|
|
|
- size_t static inline sizeWithSSRCs(int count) {
|
|
|
- return sizeof(RTCP_REMB) + (count - 1) * sizeof(SSRC);
|
|
|
- }
|
|
|
+ void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate);
|
|
|
+ void setBitrate(unsigned int numSSRC, unsigned int in_bitrate);
|
|
|
+ void setSsrc(int iterator, SSRC newSssrc);
|
|
|
};
|
|
|
|
|
|
-struct RTCP_PLI {
|
|
|
+struct RTC_CPP_EXPORT RTCP_PLI {
|
|
|
RTCP_FB_HEADER header;
|
|
|
|
|
|
- void preparePacket(SSRC messageSSRC) {
|
|
|
- header.header.prepareHeader(206, 1, 2);
|
|
|
- header.setPacketSenderSSRC(messageSSRC);
|
|
|
- header.setMediaSourceSSRC(messageSSRC);
|
|
|
- }
|
|
|
+ [[nodiscard]] static unsigned int Size();
|
|
|
|
|
|
- void print() { header.log(); }
|
|
|
+ void preparePacket(SSRC messageSSRC);
|
|
|
|
|
|
- [[nodiscard]] static unsigned int size() { return sizeof(RTCP_FB_HEADER); }
|
|
|
+ void log() const;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_FIR_PART {
|
|
|
+struct RTC_CPP_EXPORT RTCP_FIR_PART {
|
|
|
uint32_t ssrc;
|
|
|
uint8_t seqNo;
|
|
|
uint8_t dummy1;
|
|
|
uint16_t dummy2;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_FIR {
|
|
|
+struct RTC_CPP_EXPORT RTCP_FIR {
|
|
|
RTCP_FB_HEADER header;
|
|
|
RTCP_FIR_PART parts[1];
|
|
|
|
|
|
- void preparePacket(SSRC messageSSRC, uint8_t seqNo) {
|
|
|
- header.header.prepareHeader(206, 4, 2 + 2 * 1);
|
|
|
- header.setPacketSenderSSRC(messageSSRC);
|
|
|
- header.setMediaSourceSSRC(messageSSRC);
|
|
|
- parts[0].ssrc = htonl(messageSSRC);
|
|
|
- parts[0].seqNo = seqNo;
|
|
|
- }
|
|
|
+ static unsigned int Size();
|
|
|
|
|
|
- void print() { header.log(); }
|
|
|
+ void preparePacket(SSRC messageSSRC, uint8_t seqNo);
|
|
|
|
|
|
- [[nodiscard]] static unsigned int size() {
|
|
|
- return sizeof(RTCP_FB_HEADER) + sizeof(RTCP_FIR_PART);
|
|
|
- }
|
|
|
+ void log() const;
|
|
|
};
|
|
|
|
|
|
-struct RTCP_NACK_PART {
|
|
|
+struct RTC_CPP_EXPORT RTCP_NACK_PART {
|
|
|
uint16_t _pid;
|
|
|
uint16_t _blp;
|
|
|
|
|
|
- uint16_t getPID() { return ntohs(_pid); }
|
|
|
- uint16_t getBLP() { return ntohs(_blp); }
|
|
|
-
|
|
|
- void setPID(uint16_t pid) { _pid = htons(pid); }
|
|
|
- void setBLP(uint16_t blp) { _blp = htons(blp); }
|
|
|
-
|
|
|
- std::vector<uint16_t> getSequenceNumbers() {
|
|
|
- std::vector<uint16_t> result{};
|
|
|
- result.reserve(17);
|
|
|
- uint16_t pid = getPID();
|
|
|
- result.push_back(pid);
|
|
|
- uint16_t bitmask = getBLP();
|
|
|
- uint16_t i = pid + 1;
|
|
|
- while (bitmask > 0) {
|
|
|
- if (bitmask & 0x1) {
|
|
|
- result.push_back(i);
|
|
|
- }
|
|
|
- i += 1;
|
|
|
- bitmask >>= 1;
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ uint16_t pid();
|
|
|
+ uint16_t blp();
|
|
|
+
|
|
|
+ void setPid(uint16_t pid);
|
|
|
+ void setBlp(uint16_t blp);
|
|
|
+
|
|
|
+ std::vector<uint16_t> getSequenceNumbers();
|
|
|
};
|
|
|
|
|
|
-class RTCP_NACK {
|
|
|
-public:
|
|
|
+struct RTC_CPP_EXPORT RTCP_NACK {
|
|
|
RTCP_FB_HEADER header;
|
|
|
RTCP_NACK_PART parts[1];
|
|
|
|
|
|
-public:
|
|
|
- void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount) {
|
|
|
- header.header.prepareHeader(205, 1, 2 + uint16_t(discreteSeqNoCount));
|
|
|
- header.setMediaSourceSSRC(ssrc);
|
|
|
- header.setPacketSenderSSRC(ssrc);
|
|
|
- }
|
|
|
+ [[nodiscard]] static unsigned int Size(unsigned int discreteSeqNoCount);
|
|
|
+
|
|
|
+ [[nodiscard]] unsigned int getSeqNoCount();
|
|
|
+
|
|
|
+ void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount);
|
|
|
|
|
|
/**
|
|
|
* Add a packet to the list of missing packets.
|
|
@@ -668,71 +320,22 @@ public:
|
|
|
* @param missingPacket The seq no of the missing packet. This will be added to the queue.
|
|
|
* @return true if the packet has grown, false otherwise.
|
|
|
*/
|
|
|
- bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket) {
|
|
|
- if (*fciCount == 0 || missingPacket < *fciPID || missingPacket > (*fciPID + 16)) {
|
|
|
- parts[*fciCount].setPID(missingPacket);
|
|
|
- parts[*fciCount].setBLP(0);
|
|
|
- *fciPID = missingPacket;
|
|
|
- (*fciCount)++;
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- // TODO SPEED!
|
|
|
- uint16_t blp = parts[(*fciCount) - 1].getBLP();
|
|
|
- uint16_t newBit = uint16_t(1u << (missingPacket - (1 + *fciPID)));
|
|
|
- parts[(*fciCount) - 1].setBLP(blp | newBit);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] static unsigned int getSize(unsigned int discreteSeqNoCount) {
|
|
|
- return offsetof(RTCP_NACK, parts) + sizeof(RTCP_NACK_PART) * discreteSeqNoCount;
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] unsigned int getSeqNoCount() { return header.header.length() - 2; }
|
|
|
+ bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket);
|
|
|
};
|
|
|
|
|
|
-class RTP_RTX {
|
|
|
-private:
|
|
|
+struct RTC_CPP_EXPORT RTP_RTX {
|
|
|
RTP header;
|
|
|
|
|
|
-public:
|
|
|
- size_t copyTo(RTP *dest, size_t totalSize, uint8_t originalPayloadType) {
|
|
|
- memmove((char *)dest, (char *)this, header.getSize());
|
|
|
- dest->setSeqNumber(getOriginalSeqNo());
|
|
|
- dest->setPayloadType(originalPayloadType);
|
|
|
- memmove(dest->getBody(), getBody(), getBodySize(totalSize));
|
|
|
- return totalSize;
|
|
|
- }
|
|
|
+ [[nodiscard]] const char *getBody() const;
|
|
|
+ [[nodiscard]] char *getBody();
|
|
|
+ [[nodiscard]] size_t getBodySize(size_t totalSize) const;
|
|
|
+ [[nodiscard]] size_t getSize() const;
|
|
|
+ [[nodiscard]] uint16_t getOriginalSeqNo() const;
|
|
|
|
|
|
- [[nodiscard]] uint16_t getOriginalSeqNo() const {
|
|
|
- return ntohs(*(uint16_t *)(header.getBody()));
|
|
|
- }
|
|
|
+ // Returns the new size of the packet
|
|
|
+ size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType);
|
|
|
|
|
|
- [[nodiscard]] char *getBody() { return header.getBody() + sizeof(uint16_t); }
|
|
|
-
|
|
|
- [[nodiscard]] const char *getBody() const { return header.getBody() + sizeof(uint16_t); }
|
|
|
-
|
|
|
- [[nodiscard]] size_t getBodySize(size_t totalSize) {
|
|
|
- return totalSize - (getBody() - reinterpret_cast<char *>(this));
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] size_t getSize() const{
|
|
|
- return header.getSize() + sizeof(uint16_t);
|
|
|
- }
|
|
|
-
|
|
|
- [[nodiscard]] RTP &getHeader() { return header; }
|
|
|
-
|
|
|
- /*
|
|
|
- * Returns the new size of the packet
|
|
|
- */
|
|
|
- size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType) {
|
|
|
- header.setSeqNumber(getOriginalSeqNo());
|
|
|
- header.setSsrc(originalSSRC);
|
|
|
- header.setPayloadType(originalPayloadType);
|
|
|
- // TODO, the -12 is the size of the header (which is variable!)
|
|
|
- memmove(header.getBody(), getBody(), totalSize - getSize());
|
|
|
- return totalSize - 2;
|
|
|
- }
|
|
|
+ size_t copyTo(RTP *dest, size_t totalSize, uint8_t originalPayloadType);
|
|
|
};
|
|
|
|
|
|
#pragma pack(pop)
|