rtp.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /**
  2. * Copyright (c) 2020 Staz Modrzynski
  3. * Copyright (c) 2020 Paul-Louis Ageneau
  4. * Copyright (c) 2020 Filip Klembara (in2core)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef RTC_RTP_HPP
  21. #define RTC_RTP_HPP
  22. #include "log.hpp"
  23. #include <cmath>
  24. #ifdef _WIN32
  25. #include <winsock2.h>
  26. #else
  27. #include <arpa/inet.h>
  28. #endif
  29. #ifndef htonll
  30. #define htonll(x) \
  31. ((uint64_t)htonl(((uint64_t)(x)&0xFFFFFFFF) << 32) | (uint64_t)htonl((uint64_t)(x) >> 32))
  32. #endif
  33. #ifndef ntohll
  34. #define ntohll(x) htonll(x)
  35. #endif
  36. namespace rtc {
  37. typedef uint32_t SSRC;
  38. #pragma pack(push, 1)
  39. struct RTP {
  40. private:
  41. uint8_t _first;
  42. uint8_t _payloadType;
  43. uint16_t _seqNumber;
  44. uint32_t _timestamp;
  45. SSRC _ssrc;
  46. public:
  47. SSRC csrc[16];
  48. inline uint8_t version() const { return _first >> 6; }
  49. inline bool padding() const { return (_first >> 5) & 0x01; }
  50. inline bool extension() const { return (_first >> 4) & 0x01; }
  51. inline uint8_t csrcCount() const { return _first & 0x0F; }
  52. inline uint8_t marker() const { return _payloadType & 0b10000000; }
  53. inline uint8_t payloadType() const { return _payloadType & 0b01111111; }
  54. inline uint16_t seqNumber() const { return ntohs(_seqNumber); }
  55. inline uint32_t timestamp() const { return ntohl(_timestamp); }
  56. inline uint32_t ssrc() const { return ntohl(_ssrc); }
  57. inline size_t getSize() const {
  58. return reinterpret_cast<const char *>(&csrc) - reinterpret_cast<const char *>(this) +
  59. sizeof(SSRC) * csrcCount();
  60. }
  61. [[nodiscard]] char *getBody() {
  62. return reinterpret_cast<char *>(&csrc) + sizeof(SSRC) * csrcCount();
  63. }
  64. [[nodiscard]] const char *getBody() const {
  65. return reinterpret_cast<const char *>(&csrc) + sizeof(SSRC) * csrcCount();
  66. }
  67. inline void preparePacket() {
  68. _first |= (1 << 7);
  69. }
  70. inline void setSeqNumber(uint16_t newSeqNo) { _seqNumber = htons(newSeqNo); }
  71. inline void setPayloadType(uint8_t newPayloadType) {
  72. _payloadType = (_payloadType & 0b10000000u) | (0b01111111u & newPayloadType);
  73. }
  74. inline void setSsrc(uint32_t in_ssrc) { _ssrc = htonl(in_ssrc); }
  75. inline void setMarker(bool marker) { _payloadType = (_payloadType & 0x7F) | (marker << 7); };
  76. void setTimestamp(uint32_t i) { _timestamp = htonl(i); }
  77. void log() {
  78. PLOG_VERBOSE << "RTP V: " << (int)version() << " P: " << (padding() ? "P" : " ")
  79. << " X: " << (extension() ? "X" : " ") << " CC: " << (int)csrcCount()
  80. << " M: " << (marker() ? "M" : " ") << " PT: " << (int)payloadType()
  81. << " SEQNO: " << seqNumber() << " TS: " << timestamp();
  82. }
  83. };
  84. struct RTCP_ReportBlock {
  85. SSRC ssrc;
  86. private:
  87. uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
  88. uint16_t _seqNoCycles;
  89. uint16_t _highestSeqNo;
  90. uint32_t _jitter;
  91. uint32_t _lastReport;
  92. uint32_t _delaySinceLastReport;
  93. public:
  94. inline void preparePacket(SSRC in_ssrc, [[maybe_unused]] unsigned int packetsLost,
  95. [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
  96. uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
  97. uint64_t lastSR_DELAY) {
  98. setSeqNo(highestSeqNo, seqNoCycles);
  99. setJitter(jitter);
  100. setSSRC(in_ssrc);
  101. // Middle 32 bits of NTP Timestamp
  102. // this->lastReport = lastSR_NTP >> 16u;
  103. setNTPOfSR(uint64_t(lastSR_NTP));
  104. setDelaySinceSR(uint32_t(lastSR_DELAY));
  105. // The delay, expressed in units of 1/65536 seconds
  106. // this->delaySinceLastReport = lastSR_DELAY;
  107. }
  108. inline void setSSRC(SSRC in_ssrc) { this->ssrc = htonl(in_ssrc); }
  109. [[nodiscard]] inline SSRC getSSRC() const { return ntohl(ssrc); }
  110. inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost,
  111. [[maybe_unused]] unsigned int totalPackets) {
  112. // TODO Implement loss percentages.
  113. _fractionLostAndPacketsLost = 0;
  114. }
  115. [[nodiscard]] inline unsigned int getLossPercentage() const {
  116. // TODO Implement loss percentages.
  117. return 0;
  118. }
  119. [[nodiscard]] inline unsigned int getPacketLostCount() const {
  120. // TODO Implement total packets lost.
  121. return 0;
  122. }
  123. inline uint16_t seqNoCycles() const { return ntohs(_seqNoCycles); }
  124. inline uint16_t highestSeqNo() const { return ntohs(_highestSeqNo); }
  125. inline uint32_t jitter() const { return ntohl(_jitter); }
  126. inline void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
  127. _highestSeqNo = htons(highestSeqNo);
  128. _seqNoCycles = htons(seqNoCycles);
  129. }
  130. inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
  131. inline void setNTPOfSR(uint64_t ntp) { _lastReport = htonll(ntp >> 16u); }
  132. [[nodiscard]] inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
  133. inline void setDelaySinceSR(uint32_t sr) {
  134. // The delay, expressed in units of 1/65536 seconds
  135. _delaySinceLastReport = htonl(sr);
  136. }
  137. [[nodiscard]] inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
  138. inline void log() const {
  139. PLOG_VERBOSE << "RTCP report block: "
  140. << "ssrc="
  141. << ntohl(ssrc)
  142. // TODO: Implement these reports
  143. // << ", fractionLost=" << fractionLost
  144. // << ", packetsLost=" << packetsLost
  145. << ", highestSeqNo=" << highestSeqNo() << ", seqNoCycles=" << seqNoCycles()
  146. << ", jitter=" << jitter() << ", lastSR=" << getNTPOfSR()
  147. << ", lastSRDelay=" << getDelaySinceSR();
  148. }
  149. };
  150. struct RTCP_HEADER {
  151. private:
  152. uint8_t _first;
  153. uint8_t _payloadType;
  154. uint16_t _length;
  155. public:
  156. inline uint8_t version() const { return _first >> 6; }
  157. inline bool padding() const { return (_first >> 5) & 0x01; }
  158. inline uint8_t reportCount() const { return _first & 0x0F; }
  159. inline uint8_t payloadType() const { return _payloadType; }
  160. inline uint16_t length() const { return ntohs(_length); }
  161. inline size_t lengthInBytes() const { return (1 + length()) * 4; }
  162. inline void setPayloadType(uint8_t type) { _payloadType = type; }
  163. inline void setReportCount(uint8_t count) {
  164. _first = (_first & 0b11100000u) | (count & 0b00011111u);
  165. }
  166. inline void setLength(uint16_t length) { _length = htons(length); }
  167. inline void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
  168. _first = 0b10000000; // version 2, no padding
  169. setReportCount(reportCount);
  170. setPayloadType(payloadType);
  171. setLength(length);
  172. }
  173. inline void log() const {
  174. PLOG_VERBOSE << "RTCP header: "
  175. << "version=" << unsigned(version()) << ", padding=" << padding()
  176. << ", reportCount=" << unsigned(reportCount())
  177. << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
  178. }
  179. };
  180. struct RTCP_FB_HEADER {
  181. RTCP_HEADER header;
  182. SSRC packetSender;
  183. SSRC mediaSource;
  184. [[nodiscard]] SSRC getPacketSenderSSRC() const { return ntohl(packetSender); }
  185. [[nodiscard]] SSRC getMediaSourceSSRC() const { return ntohl(mediaSource); }
  186. void setPacketSenderSSRC(SSRC ssrc) { this->packetSender = htonl(ssrc); }
  187. void setMediaSourceSSRC(SSRC ssrc) { this->mediaSource = htonl(ssrc); }
  188. void log() {
  189. header.log();
  190. PLOG_VERBOSE << "FB: "
  191. << " packet sender: " << getPacketSenderSSRC()
  192. << " media source: " << getMediaSourceSSRC();
  193. }
  194. };
  195. struct RTCP_SR {
  196. RTCP_HEADER header;
  197. SSRC _senderSSRC;
  198. private:
  199. uint64_t _ntpTimestamp;
  200. uint32_t _rtpTimestamp;
  201. uint32_t _packetCount;
  202. uint32_t _octetCount;
  203. RTCP_ReportBlock _reportBlocks;
  204. public:
  205. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  206. unsigned int length =
  207. ((sizeof(header) + 24 + reportCount * sizeof(RTCP_ReportBlock)) / 4) - 1;
  208. header.prepareHeader(200, reportCount, uint16_t(length));
  209. this->_senderSSRC = htonl(senderSSRC);
  210. }
  211. [[nodiscard]] inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  212. [[nodiscard]] inline const RTCP_ReportBlock *getReportBlock(int num) const {
  213. return &_reportBlocks + num;
  214. }
  215. [[nodiscard]] static unsigned int size(unsigned int reportCount) {
  216. return sizeof(RTCP_HEADER) + 24 + reportCount * sizeof(RTCP_ReportBlock);
  217. }
  218. [[nodiscard]] inline size_t getSize() const {
  219. // "length" in packet is one less than the number of 32 bit words in the packet.
  220. return sizeof(uint32_t) * (1 + size_t(header.length()));
  221. }
  222. inline uint64_t ntpTimestamp() const { return ntohll(_ntpTimestamp); }
  223. inline uint32_t rtpTimestamp() const { return ntohl(_rtpTimestamp); }
  224. inline uint32_t packetCount() const { return ntohl(_packetCount); }
  225. inline uint32_t octetCount() const { return ntohl(_octetCount); }
  226. inline uint32_t senderSSRC() const { return ntohl(_senderSSRC); }
  227. inline void setNtpTimestamp(uint64_t ts) { _ntpTimestamp = htonll(ts); }
  228. inline void setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
  229. inline void setOctetCount(uint32_t ts) { _octetCount = htonl(ts); }
  230. inline void setPacketCount(uint32_t ts) { _packetCount = htonl(ts); }
  231. inline void log() const {
  232. header.log();
  233. PLOG_VERBOSE << "RTCP SR: "
  234. << " SSRC=" << senderSSRC() << ", NTP_TS=" << ntpTimestamp()
  235. << ", RTP_TS=" << rtpTimestamp() << ", packetCount=" << packetCount()
  236. << ", octetCount=" << octetCount();
  237. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  238. getReportBlock(i)->log();
  239. }
  240. }
  241. };
  242. struct RTCP_SDES_ITEM {
  243. public:
  244. uint8_t type;
  245. private:
  246. uint8_t _length;
  247. char _text;
  248. public:
  249. inline std::string text() const {
  250. return std::string(&_text, _length);
  251. }
  252. inline void setText(std::string text) {
  253. _length = text.length();
  254. memcpy(&_text, text.data(), _length);
  255. }
  256. inline uint8_t length() {
  257. return _length;
  258. }
  259. [[nodiscard]] static unsigned int size(uint8_t textLength) {
  260. return textLength + 2;
  261. }
  262. };
  263. struct RTCP_SDES_CHUNK {
  264. private:
  265. SSRC _ssrc;
  266. RTCP_SDES_ITEM _items;
  267. public:
  268. inline SSRC ssrc() const { return ntohl(_ssrc); }
  269. inline void setSSRC(SSRC ssrc) { _ssrc = htonl(ssrc); }
  270. /// Get item at given index
  271. /// @note All items with index < `num` must be valid, otherwise this function has undefined behaviour (use `safelyCountChunkSize` to check if chunk is valid)
  272. /// @param num Index of item to return
  273. inline RTCP_SDES_ITEM *getItem(int num) {
  274. auto base = &_items;
  275. while (num-- > 0) {
  276. auto itemSize = RTCP_SDES_ITEM::size(base->length());
  277. base = reinterpret_cast<RTCP_SDES_ITEM *>(reinterpret_cast<uint8_t *>(base) + itemSize);
  278. }
  279. return reinterpret_cast<RTCP_SDES_ITEM *>(base);
  280. }
  281. long safelyCountChunkSize(unsigned int maxChunkSize) {
  282. if (maxChunkSize < RTCP_SDES_CHUNK::size({})) {
  283. // chunk is truncated
  284. return -1;
  285. } else {
  286. unsigned int size = sizeof(SSRC);
  287. unsigned int i = 0;
  288. // We can always access first 4 bytes of first item (in case of no items there will be 4 null bytes)
  289. auto item = getItem(i);
  290. std::vector<uint8_t> textsLength{};
  291. while (item->type != 0) {
  292. if (size + RTCP_SDES_ITEM::size(0) > maxChunkSize) {
  293. // item is too short
  294. return -1;
  295. }
  296. auto itemLength = item->length();
  297. if (size + RTCP_SDES_ITEM::size(itemLength) >= maxChunkSize) {
  298. // item is too large (it can't be equal to chunk size because after item there must be 1-4 null bytes as padding)
  299. return -1;
  300. }
  301. textsLength.push_back(itemLength);
  302. // safely to access next item
  303. item = getItem(++i);
  304. }
  305. auto realSize = RTCP_SDES_CHUNK::size(textsLength);
  306. if (realSize > maxChunkSize) {
  307. // Chunk is too large
  308. return -1;
  309. }
  310. return realSize;
  311. }
  312. }
  313. [[nodiscard]] static unsigned int size(const std::vector<uint8_t> textLengths) {
  314. unsigned int itemsSize = 0;
  315. for (auto length: textLengths) {
  316. itemsSize += RTCP_SDES_ITEM::size(length);
  317. }
  318. auto nullTerminatedItemsSize = itemsSize + 1;
  319. auto words = uint8_t(std::ceil(double(nullTerminatedItemsSize) / 4)) + 1;
  320. return words * 4;
  321. }
  322. /// Get size of chunk
  323. /// @note All items must be valid, otherwise this function has undefined behaviour (use `safelyCountChunkSize` to check if chunk is valid)
  324. [[nodiscard]] unsigned int getSize() {
  325. std::vector<uint8_t> textLengths{};
  326. unsigned int i = 0;
  327. auto item = getItem(i);
  328. while (item->type != 0) {
  329. textLengths.push_back(item->length());
  330. item = getItem(++i);
  331. }
  332. return size(textLengths);
  333. }
  334. };
  335. struct RTCP_SDES {
  336. RTCP_HEADER header;
  337. private:
  338. RTCP_SDES_CHUNK _chunks;
  339. public:
  340. inline void preparePacket(uint8_t chunkCount) {
  341. unsigned int chunkSize = 0;
  342. for(uint8_t i = 0; i < chunkCount; i++) {
  343. auto chunk = getChunk(i);
  344. chunkSize += chunk->getSize();
  345. }
  346. uint16_t length = (sizeof(header) + chunkSize) / 4 - 1;
  347. header.prepareHeader(202, chunkCount, length);
  348. }
  349. bool isValid() {
  350. auto chunksSize = header.lengthInBytes() - sizeof(header);
  351. if (chunksSize == 0) {
  352. return true;
  353. } else {
  354. // there is at least one chunk
  355. unsigned int i = 0;
  356. unsigned int size = 0;
  357. while (size < chunksSize) {
  358. if (chunksSize < size + RTCP_SDES_CHUNK::size({})) {
  359. // chunk is truncated
  360. return false;
  361. }
  362. auto chunk = getChunk(i++);
  363. auto chunkSize = chunk->safelyCountChunkSize(chunksSize - size);
  364. if (chunkSize < 0) {
  365. // chunk is invalid
  366. return false;
  367. }
  368. size += chunkSize;
  369. }
  370. return size == chunksSize;
  371. }
  372. }
  373. /// Returns number of chunks in this packet
  374. /// @note Returns 0 if packet is invalid
  375. inline unsigned int chunksCount() {
  376. if (!isValid()) {
  377. return 0;
  378. }
  379. uint16_t chunksSize = 4 * (header.length() + 1) - sizeof(header);
  380. unsigned int size = 0;
  381. unsigned int i = 0;
  382. while (size < chunksSize) {
  383. size += getChunk(i++)->getSize();
  384. }
  385. return i;
  386. }
  387. /// Get chunk at given index
  388. /// @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)
  389. /// @param num Index of chunk to return
  390. inline RTCP_SDES_CHUNK *getChunk(int num) {
  391. auto base = &_chunks;
  392. while (num-- > 0) {
  393. auto chunkSize = base->getSize();
  394. base = reinterpret_cast<RTCP_SDES_CHUNK *>(reinterpret_cast<uint8_t *>(base) + chunkSize);
  395. }
  396. return reinterpret_cast<RTCP_SDES_CHUNK *>(base);
  397. }
  398. [[nodiscard]] static unsigned int size(const std::vector<std::vector<uint8_t>> lengths) {
  399. unsigned int chunks_size = 0;
  400. for (auto length: lengths) {
  401. chunks_size += RTCP_SDES_CHUNK::size(length);
  402. }
  403. return 4 + chunks_size;
  404. }
  405. };
  406. struct RTCP_RR {
  407. RTCP_HEADER header;
  408. SSRC _senderSSRC;
  409. private:
  410. RTCP_ReportBlock _reportBlocks;
  411. public:
  412. [[nodiscard]] inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  413. [[nodiscard]] inline const RTCP_ReportBlock *getReportBlock(int num) const {
  414. return &_reportBlocks + num;
  415. }
  416. inline SSRC senderSSRC() const { return ntohl(_senderSSRC); }
  417. inline void setSenderSSRC(SSRC ssrc) { this->_senderSSRC = htonl(ssrc); }
  418. [[nodiscard]] inline size_t getSize() const {
  419. // "length" in packet is one less than the number of 32 bit words in the packet.
  420. return sizeof(uint32_t) * (1 + size_t(header.length()));
  421. }
  422. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  423. // "length" in packet is one less than the number of 32 bit words in the packet.
  424. size_t length = (sizeWithReportBlocks(reportCount) / 4) - 1;
  425. header.prepareHeader(201, reportCount, uint16_t(length));
  426. this->_senderSSRC = htonl(senderSSRC);
  427. }
  428. inline static size_t sizeWithReportBlocks(uint8_t reportCount) {
  429. return sizeof(header) + 4 + size_t(reportCount) * sizeof(RTCP_ReportBlock);
  430. }
  431. inline bool isSenderReport() { return header.payloadType() == 200; }
  432. inline bool isReceiverReport() { return header.payloadType() == 201; }
  433. inline void log() const {
  434. header.log();
  435. PLOG_VERBOSE << "RTCP RR: "
  436. << " SSRC=" << ntohl(_senderSSRC);
  437. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  438. getReportBlock(i)->log();
  439. }
  440. }
  441. };
  442. struct RTCP_REMB {
  443. RTCP_FB_HEADER header;
  444. /*! \brief Unique identifier ('R' 'E' 'M' 'B') */
  445. char id[4];
  446. /*! \brief Num SSRC, Br Exp, Br Mantissa (bit mask) */
  447. uint32_t bitrate;
  448. SSRC ssrc[1];
  449. [[nodiscard]] unsigned int getSize() const {
  450. // "length" in packet is one less than the number of 32 bit words in the packet.
  451. return sizeof(uint32_t) * (1 + header.header.length());
  452. }
  453. void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate) {
  454. // Report Count becomes the format here.
  455. header.header.prepareHeader(206, 15, 0);
  456. // Always zero.
  457. header.setMediaSourceSSRC(0);
  458. header.setPacketSenderSSRC(senderSSRC);
  459. id[0] = 'R';
  460. id[1] = 'E';
  461. id[2] = 'M';
  462. id[3] = 'B';
  463. setBitrate(numSSRC, in_bitrate);
  464. }
  465. void setBitrate(unsigned int numSSRC, unsigned int in_bitrate) {
  466. unsigned int exp = 0;
  467. while (in_bitrate > pow(2, 18) - 1) {
  468. exp++;
  469. in_bitrate /= 2;
  470. }
  471. // "length" in packet is one less than the number of 32 bit words in the packet.
  472. header.header.setLength(
  473. uint16_t((offsetof(RTCP_REMB, ssrc) / sizeof(uint32_t)) - 1 + numSSRC));
  474. this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | in_bitrate);
  475. }
  476. void setSsrc(int iterator, SSRC newSssrc) { ssrc[iterator] = htonl(newSssrc); }
  477. size_t static inline sizeWithSSRCs(int count) {
  478. return sizeof(RTCP_REMB) + (count - 1) * sizeof(SSRC);
  479. }
  480. };
  481. struct RTCP_PLI {
  482. RTCP_FB_HEADER header;
  483. void preparePacket(SSRC messageSSRC) {
  484. header.header.prepareHeader(206, 1, 2);
  485. header.setPacketSenderSSRC(messageSSRC);
  486. header.setMediaSourceSSRC(messageSSRC);
  487. }
  488. void print() { header.log(); }
  489. [[nodiscard]] static unsigned int size() { return sizeof(RTCP_FB_HEADER); }
  490. };
  491. struct RTCP_FIR_PART {
  492. uint32_t ssrc;
  493. uint8_t seqNo;
  494. uint8_t dummy1;
  495. uint16_t dummy2;
  496. };
  497. struct RTCP_FIR {
  498. RTCP_FB_HEADER header;
  499. RTCP_FIR_PART parts[1];
  500. void preparePacket(SSRC messageSSRC, uint8_t seqNo) {
  501. header.header.prepareHeader(206, 4, 2 + 2 * 1);
  502. header.setPacketSenderSSRC(messageSSRC);
  503. header.setMediaSourceSSRC(messageSSRC);
  504. parts[0].ssrc = htonl(messageSSRC);
  505. parts[0].seqNo = seqNo;
  506. }
  507. void print() { header.log(); }
  508. [[nodiscard]] static unsigned int size() {
  509. return sizeof(RTCP_FB_HEADER) + sizeof(RTCP_FIR_PART);
  510. }
  511. };
  512. struct RTCP_NACK_PART {
  513. uint16_t pid;
  514. uint16_t blp;
  515. };
  516. class RTCP_NACK {
  517. public:
  518. RTCP_FB_HEADER header;
  519. RTCP_NACK_PART parts[1];
  520. public:
  521. void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount) {
  522. header.header.prepareHeader(205, 1, 2 + uint16_t(discreteSeqNoCount));
  523. header.setMediaSourceSSRC(ssrc);
  524. header.setPacketSenderSSRC(ssrc);
  525. }
  526. /**
  527. * Add a packet to the list of missing packets.
  528. * @param fciCount The number of FCI fields that are present in this packet.
  529. * Let the number start at zero and let this function grow the number.
  530. * @param fciPID The seq no of the active FCI. It will be initialized automatically, and will
  531. * change automatically.
  532. * @param missingPacket The seq no of the missing packet. This will be added to the queue.
  533. * @return true if the packet has grown, false otherwise.
  534. */
  535. bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket) {
  536. if (*fciCount == 0 || missingPacket < *fciPID || missingPacket > (*fciPID + 16)) {
  537. parts[*fciCount].pid = htons(missingPacket);
  538. parts[*fciCount].blp = 0;
  539. *fciPID = missingPacket;
  540. (*fciCount)++;
  541. return true;
  542. } else {
  543. // TODO SPEEED!
  544. auto blp = ntohs(parts[(*fciCount) - 1].blp);
  545. auto newBit = 1u << (unsigned int)(missingPacket - (1 + *fciPID));
  546. parts[(*fciCount) - 1].blp = htons(blp | newBit);
  547. return false;
  548. }
  549. }
  550. [[nodiscard]] static unsigned int getSize(unsigned int discreteSeqNoCount) {
  551. return offsetof(RTCP_NACK, parts) + sizeof(RTCP_NACK_PART) * discreteSeqNoCount;
  552. }
  553. [[nodiscard]] unsigned int getSeqNoCount() { return header.header.length() - 2; }
  554. };
  555. class RTP_RTX {
  556. private:
  557. RTP header;
  558. public:
  559. size_t copyTo(RTP *dest, size_t totalSize, uint8_t originalPayloadType) {
  560. memmove((char *)dest, (char *)this, header.getSize());
  561. dest->setSeqNumber(getOriginalSeqNo());
  562. dest->setPayloadType(originalPayloadType);
  563. memmove(dest->getBody(), getBody(), getBodySize(totalSize));
  564. return totalSize;
  565. }
  566. [[nodiscard]] uint16_t getOriginalSeqNo() const {
  567. return ntohs(*(uint16_t *)(header.getBody()));
  568. }
  569. [[nodiscard]] char *getBody() { return header.getBody() + sizeof(uint16_t); }
  570. [[nodiscard]] const char *getBody() const { return header.getBody() + sizeof(uint16_t); }
  571. [[nodiscard]] size_t getBodySize(size_t totalSize) {
  572. return totalSize - (getBody() - reinterpret_cast<char *>(this));
  573. }
  574. [[nodiscard]] RTP &getHeader() { return header; }
  575. size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType) {
  576. header.setSeqNumber(getOriginalSeqNo());
  577. header.setSsrc(originalSSRC);
  578. header.setPayloadType(originalPayloadType);
  579. // TODO, the -12 is the size of the header (which is variable!)
  580. memmove(header.getBody(), header.getBody() + sizeof(uint16_t),
  581. totalSize - 12 - sizeof(uint16_t));
  582. return totalSize - sizeof(uint16_t);
  583. }
  584. };
  585. #pragma pack(pop)
  586. }; // namespace rtc
  587. #endif