rtp.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /**
  2. * Copyright (c) 2020 Staz Modrzynski
  3. * Copyright (c) 2020 Paul-Louis Ageneau
  4. * Copyright (c) 2020 Filip Klembara (in2core)
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  9. */
  10. #include "rtp.hpp"
  11. #include "impl/internals.hpp"
  12. #include <cmath>
  13. #include <cstring>
  14. #ifdef _WIN32
  15. #include <winsock2.h>
  16. #else
  17. #include <arpa/inet.h>
  18. #endif
  19. #ifndef htonll
  20. #define htonll(x) \
  21. ((uint64_t)(((uint64_t)htonl((uint32_t)(x))) << 32) | (uint64_t)htonl((uint32_t)((x) >> 32)))
  22. #endif
  23. #ifndef ntohll
  24. #define ntohll(x) htonll(x)
  25. #endif
  26. namespace rtc {
  27. bool IsRtcp(const binary &data) {
  28. if (data.size() < 8)
  29. return false;
  30. uint8_t payloadType = std::to_integer<uint8_t>(data[1]) & 0x7F;
  31. PLOG_VERBOSE << "Demultiplexing RTCP and RTP with payload type, value=" << int(payloadType);
  32. // RFC 5761 Multiplexing RTP and RTCP 4. Distinguishable RTP and RTCP Packets
  33. // https://www.rfc-editor.org/rfc/rfc5761.html#section-4
  34. // It is RECOMMENDED to follow the guidelines in the RTP/AVP profile for the choice of RTP
  35. // payload type values, with the additional restriction that payload type values in the
  36. // range 64-95 MUST NOT be used. Specifically, dynamic RTP payload types SHOULD be chosen in
  37. // the range 96-127 where possible. Values below 64 MAY be used if that is insufficient
  38. // [...]
  39. return (payloadType >= 64 && payloadType <= 95); // Range 64-95 (inclusive) MUST be RTCP
  40. }
  41. uint8_t RtpHeader::version() const { return _first >> 6; }
  42. bool RtpHeader::padding() const { return (_first >> 5) & 0x01; }
  43. bool RtpHeader::extension() const { return (_first >> 4) & 0x01; }
  44. uint8_t RtpHeader::csrcCount() const { return _first & 0x0F; }
  45. uint8_t RtpHeader::marker() const { return _payloadType & 0b10000000; }
  46. uint8_t RtpHeader::payloadType() const { return _payloadType & 0b01111111; }
  47. uint16_t RtpHeader::seqNumber() const { return ntohs(_seqNumber); }
  48. uint32_t RtpHeader::timestamp() const { return ntohl(_timestamp); }
  49. uint32_t RtpHeader::ssrc() const { return ntohl(_ssrc); }
  50. size_t RtpHeader::getSize() const {
  51. return reinterpret_cast<const char *>(&_ssrc + 1 + csrcCount()) -
  52. reinterpret_cast<const char *>(this);
  53. }
  54. size_t RtpHeader::getExtensionHeaderSize() const {
  55. auto header = getExtensionHeader();
  56. return header ? header->getSize() + sizeof(RtpExtensionHeader) : 0;
  57. }
  58. const RtpExtensionHeader *RtpHeader::getExtensionHeader() const {
  59. return extension() ? reinterpret_cast<const RtpExtensionHeader *>(&_ssrc + 1 + csrcCount())
  60. : nullptr;
  61. }
  62. RtpExtensionHeader *RtpHeader::getExtensionHeader() {
  63. return extension() ? reinterpret_cast<RtpExtensionHeader *>(&_ssrc + 1 + csrcCount()) : nullptr;
  64. }
  65. const char *RtpHeader::getBody() const {
  66. return reinterpret_cast<const char *>(&_ssrc + 1 + csrcCount()) + getExtensionHeaderSize();
  67. }
  68. char *RtpHeader::getBody() {
  69. return reinterpret_cast<char *>(&_ssrc + 1 + csrcCount()) + getExtensionHeaderSize();
  70. }
  71. void RtpHeader::preparePacket() { _first |= (1 << 7); }
  72. void RtpHeader::setSeqNumber(uint16_t newSeqNo) { _seqNumber = htons(newSeqNo); }
  73. void RtpHeader::setPayloadType(uint8_t newPayloadType) {
  74. _payloadType = (_payloadType & 0b10000000u) | (0b01111111u & newPayloadType);
  75. }
  76. void RtpHeader::setSsrc(uint32_t in_ssrc) { _ssrc = htonl(in_ssrc); }
  77. void RtpHeader::setMarker(bool marker) { _payloadType = (_payloadType & 0x7F) | (marker << 7); };
  78. void RtpHeader::setTimestamp(uint32_t i) { _timestamp = htonl(i); }
  79. void RtpHeader::setExtension(bool extension) { _first = (_first & ~0x10) | ((extension & 1) << 4); }
  80. void RtpHeader::log() const {
  81. PLOG_VERBOSE << "RtpHeader V: " << (int)version() << " P: " << (padding() ? "P" : " ")
  82. << " X: " << (extension() ? "X" : " ") << " CC: " << (int)csrcCount()
  83. << " M: " << (marker() ? "M" : " ") << " PT: " << (int)payloadType()
  84. << " SEQNO: " << seqNumber() << " TS: " << timestamp();
  85. }
  86. uint16_t RtpExtensionHeader::profileSpecificId() const { return ntohs(_profileSpecificId); }
  87. uint16_t RtpExtensionHeader::headerLength() const { return ntohs(_headerLength); }
  88. size_t RtpExtensionHeader::getSize() const { return headerLength() * 4; }
  89. const char *RtpExtensionHeader::getBody() const {
  90. return reinterpret_cast<const char *>((&_headerLength) + 1);
  91. }
  92. char *RtpExtensionHeader::getBody() { return reinterpret_cast<char *>((&_headerLength) + 1); }
  93. void RtpExtensionHeader::setProfileSpecificId(uint16_t profileSpecificId) {
  94. _profileSpecificId = htons(profileSpecificId);
  95. }
  96. void RtpExtensionHeader::setHeaderLength(uint16_t headerLength) {
  97. _headerLength = htons(headerLength);
  98. }
  99. void RtpExtensionHeader::clearBody() { std::memset(getBody(), 0, getSize()); }
  100. void RtpExtensionHeader::writeOneByteHeader(size_t offset, uint8_t id, const byte *value,
  101. size_t size) {
  102. if ((id == 0) || (id > 14) || (size == 0) || (size > 16) || ((offset + 1 + size) > getSize()))
  103. return;
  104. auto buf = getBody() + offset;
  105. buf[0] = id << 4;
  106. if (size != 1) {
  107. buf[0] |= (uint8_t(size) - 1);
  108. }
  109. std::memcpy(buf + 1, value, size);
  110. }
  111. void RtpExtensionHeader::writeCurrentVideoOrientation(size_t offset, const uint8_t id,
  112. uint8_t value) {
  113. auto v = std::byte{value};
  114. writeOneByteHeader(offset, id, &v, 1);
  115. }
  116. SSRC RtcpReportBlock::getSSRC() const { return ntohl(_ssrc); }
  117. void RtcpReportBlock::preparePacket(SSRC in_ssrc, [[maybe_unused]] unsigned int packetsLost,
  118. [[maybe_unused]] unsigned int totalPackets,
  119. uint16_t highestSeqNo, uint16_t seqNoCycles, uint32_t jitter,
  120. uint64_t lastSR_NTP, uint64_t lastSR_DELAY) {
  121. setSeqNo(highestSeqNo, seqNoCycles);
  122. setJitter(jitter);
  123. setSSRC(in_ssrc);
  124. // Middle 32 bits of NTP Timestamp
  125. // _lastReport = lastSR_NTP >> 16u;
  126. setNTPOfSR(uint64_t(lastSR_NTP));
  127. setDelaySinceSR(uint32_t(lastSR_DELAY));
  128. // The delay, expressed in units of 1/65536 seconds
  129. // _delaySinceLastReport = lastSR_DELAY;
  130. }
  131. void RtcpReportBlock::setSSRC(SSRC in_ssrc) { _ssrc = htonl(in_ssrc); }
  132. void RtcpReportBlock::setPacketsLost(uint8_t fractionLost,
  133. unsigned int packetsLostCount) {
  134. _fractionLostAndPacketsLost = htonl((uint32_t(fractionLost) << 24) | (packetsLostCount & 0xFFFFFF));
  135. }
  136. uint8_t RtcpReportBlock::getFractionLost() const {
  137. // Fraction lost is expressed as 8-bit fixed point number
  138. // In order to get actual lost percentage divide the result by 256
  139. return _fractionLostAndPacketsLost & 0xFF;
  140. }
  141. unsigned int RtcpReportBlock::getPacketsLostCount() const {
  142. return ntohl(_fractionLostAndPacketsLost & 0xFFFFFF00);
  143. }
  144. uint16_t RtcpReportBlock::seqNoCycles() const { return ntohs(_seqNoCycles); }
  145. uint16_t RtcpReportBlock::highestSeqNo() const { return ntohs(_highestSeqNo); }
  146. uint32_t RtcpReportBlock::extendedHighestSeqNo() const { return (seqNoCycles() << 16) | highestSeqNo(); }
  147. uint32_t RtcpReportBlock::jitter() const { return ntohl(_jitter); }
  148. uint32_t RtcpReportBlock::delaySinceSR() const { return ntohl(_delaySinceLastReport); }
  149. void RtcpReportBlock::setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
  150. _highestSeqNo = htons(highestSeqNo);
  151. _seqNoCycles = htons(seqNoCycles);
  152. }
  153. void RtcpReportBlock::setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
  154. void RtcpReportBlock::setNTPOfSR(uint64_t ntp) { _lastReport = htonl((uint32_t)(ntp >> 16)); }
  155. uint32_t RtcpReportBlock::getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
  156. void RtcpReportBlock::setDelaySinceSR(uint32_t sr) {
  157. // The delay, expressed in units of 1/65536 seconds
  158. _delaySinceLastReport = htonl(sr);
  159. }
  160. void RtcpReportBlock::log() const {
  161. PLOG_VERBOSE << "RTCP report block: "
  162. << "ssrc="
  163. << ntohl(_ssrc)
  164. // TODO: Implement these reports
  165. // << ", fractionLost=" << fractionLost
  166. // << ", packetsLost=" << packetsLost
  167. << ", highestSeqNo=" << highestSeqNo() << ", seqNoCycles=" << seqNoCycles()
  168. << ", jitter=" << jitter() << ", lastSR=" << getNTPOfSR()
  169. << ", lastSRDelay=" << delaySinceSR();
  170. }
  171. uint8_t RtcpHeader::version() const { return _first >> 6; }
  172. bool RtcpHeader::padding() const { return (_first >> 5) & 0x01; }
  173. uint8_t RtcpHeader::reportCount() const { return _first & 0x1F; }
  174. uint8_t RtcpHeader::payloadType() const { return _payloadType; }
  175. uint16_t RtcpHeader::length() const { return ntohs(_length); }
  176. size_t RtcpHeader::lengthInBytes() const { return (1 + length()) * 4; }
  177. void RtcpHeader::setPayloadType(uint8_t type) { _payloadType = type; }
  178. void RtcpHeader::setReportCount(uint8_t count) {
  179. _first = (_first & 0b11100000u) | (count & 0b00011111u);
  180. }
  181. void RtcpHeader::setLength(uint16_t length) { _length = htons(length); }
  182. void RtcpHeader::prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
  183. _first = 0b10000000; // version 2, no padding
  184. setReportCount(reportCount);
  185. setPayloadType(payloadType);
  186. setLength(length);
  187. }
  188. void RtcpHeader::log() const {
  189. PLOG_VERBOSE << "RTCP header: "
  190. << "version=" << unsigned(version()) << ", padding=" << padding()
  191. << ", reportCount=" << unsigned(reportCount())
  192. << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
  193. }
  194. SSRC RtcpFbHeader::packetSenderSSRC() const { return ntohl(_packetSender); }
  195. SSRC RtcpFbHeader::mediaSourceSSRC() const { return ntohl(_mediaSource); }
  196. void RtcpFbHeader::setPacketSenderSSRC(SSRC ssrc) { _packetSender = htonl(ssrc); }
  197. void RtcpFbHeader::setMediaSourceSSRC(SSRC ssrc) { _mediaSource = htonl(ssrc); }
  198. void RtcpFbHeader::log() const {
  199. header.log();
  200. PLOG_VERBOSE << "FB: "
  201. << " packet sender: " << packetSenderSSRC()
  202. << " media source: " << mediaSourceSSRC();
  203. }
  204. unsigned int RtcpSr::Size(unsigned int reportCount) {
  205. return sizeof(RtcpHeader) + 24 + reportCount * sizeof(RtcpReportBlock);
  206. }
  207. void RtcpSr::preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  208. unsigned int length = ((sizeof(header) + 24 + reportCount * sizeof(RtcpReportBlock)) / 4) - 1;
  209. header.prepareHeader(200, reportCount, uint16_t(length));
  210. this->_senderSSRC = htonl(senderSSRC);
  211. }
  212. const RtcpReportBlock *RtcpSr::getReportBlock(int num) const { return &_reportBlocks + num; }
  213. RtcpReportBlock *RtcpSr::getReportBlock(int num) { return &_reportBlocks + num; }
  214. size_t RtcpSr::getSize() const {
  215. // "length" in packet is one less than the number of 32 bit words in the packet.
  216. return sizeof(uint32_t) * (1 + size_t(header.length()));
  217. }
  218. uint64_t RtcpSr::ntpTimestamp() const { return ntohll(_ntpTimestamp); }
  219. uint32_t RtcpSr::rtpTimestamp() const { return ntohl(_rtpTimestamp); }
  220. uint32_t RtcpSr::packetCount() const { return ntohl(_packetCount); }
  221. uint32_t RtcpSr::octetCount() const { return ntohl(_octetCount); }
  222. uint32_t RtcpSr::senderSSRC() const { return ntohl(_senderSSRC); }
  223. void RtcpSr::setNtpTimestamp(uint64_t ts) { _ntpTimestamp = htonll(ts); }
  224. void RtcpSr::setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
  225. void RtcpSr::setOctetCount(uint32_t ts) { _octetCount = htonl(ts); }
  226. void RtcpSr::setPacketCount(uint32_t ts) { _packetCount = htonl(ts); }
  227. void RtcpSr::log() const {
  228. header.log();
  229. PLOG_VERBOSE << "RTCP SR: "
  230. << " SSRC=" << senderSSRC() << ", NTP_TS=" << ntpTimestamp()
  231. << ", RtpTS=" << rtpTimestamp() << ", packetCount=" << packetCount()
  232. << ", octetCount=" << octetCount();
  233. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  234. getReportBlock(i)->log();
  235. }
  236. }
  237. unsigned int RtcpSdesItem::Size(uint8_t textLength) { return textLength + 2; }
  238. std::string RtcpSdesItem::text() const { return std::string(_text, _length); }
  239. void RtcpSdesItem::setText(std::string text) {
  240. if (text.size() > 0xFF)
  241. throw std::invalid_argument("text is too long");
  242. _length = uint8_t(text.size());
  243. memcpy(_text, text.data(), text.size());
  244. }
  245. uint8_t RtcpSdesItem::length() const { return _length; }
  246. unsigned int RtcpSdesChunk::Size(const std::vector<uint8_t> textLengths) {
  247. unsigned int itemsSize = 0;
  248. for (auto length : textLengths) {
  249. itemsSize += RtcpSdesItem::Size(length);
  250. }
  251. auto nullTerminatedItemsSize = itemsSize + 1;
  252. auto words = uint8_t(std::ceil(double(nullTerminatedItemsSize) / 4)) + 1;
  253. return words * 4;
  254. }
  255. SSRC RtcpSdesChunk::ssrc() const { return ntohl(_ssrc); }
  256. void RtcpSdesChunk::setSSRC(SSRC ssrc) { _ssrc = htonl(ssrc); }
  257. const RtcpSdesItem *RtcpSdesChunk::getItem(int num) const {
  258. auto base = &_items;
  259. while (num-- > 0) {
  260. auto itemSize = RtcpSdesItem::Size(base->length());
  261. base = reinterpret_cast<const RtcpSdesItem *>(reinterpret_cast<const uint8_t *>(base) +
  262. itemSize);
  263. }
  264. return reinterpret_cast<const RtcpSdesItem *>(base);
  265. }
  266. RtcpSdesItem *RtcpSdesChunk::getItem(int num) {
  267. auto base = &_items;
  268. while (num-- > 0) {
  269. auto itemSize = RtcpSdesItem::Size(base->length());
  270. base = reinterpret_cast<RtcpSdesItem *>(reinterpret_cast<uint8_t *>(base) + itemSize);
  271. }
  272. return reinterpret_cast<RtcpSdesItem *>(base);
  273. }
  274. unsigned int RtcpSdesChunk::getSize() const {
  275. std::vector<uint8_t> textLengths{};
  276. unsigned int i = 0;
  277. auto item = getItem(i);
  278. while (item->type != 0) {
  279. textLengths.push_back(item->length());
  280. item = getItem(++i);
  281. }
  282. return Size(textLengths);
  283. }
  284. long RtcpSdesChunk::safelyCountChunkSize(size_t maxChunkSize) const {
  285. if (maxChunkSize < RtcpSdesChunk::Size({})) {
  286. // chunk is truncated
  287. return -1;
  288. }
  289. size_t size = sizeof(SSRC);
  290. unsigned int i = 0;
  291. // We can always access first 4 bytes of first item (in case of no items there will be 4
  292. // null bytes)
  293. auto item = getItem(i);
  294. std::vector<uint8_t> textsLength{};
  295. while (item->type != 0) {
  296. if (size + RtcpSdesItem::Size(0) > maxChunkSize) {
  297. // item is too short
  298. return -1;
  299. }
  300. auto itemLength = item->length();
  301. if (size + RtcpSdesItem::Size(itemLength) >= maxChunkSize) {
  302. // item is too large (it can't be equal to chunk size because after item there
  303. // must be 1-4 null bytes as padding)
  304. return -1;
  305. }
  306. textsLength.push_back(itemLength);
  307. // safely to access next item
  308. item = getItem(++i);
  309. }
  310. auto realSize = RtcpSdesChunk::Size(textsLength);
  311. if (realSize > maxChunkSize) {
  312. // Chunk is too large
  313. return -1;
  314. }
  315. return realSize;
  316. }
  317. unsigned int RtcpSdes::Size(const std::vector<std::vector<uint8_t>> lengths) {
  318. unsigned int chunks_size = 0;
  319. for (auto length : lengths)
  320. chunks_size += RtcpSdesChunk::Size(length);
  321. return 4 + chunks_size;
  322. }
  323. bool RtcpSdes::isValid() const {
  324. auto chunksSize = header.lengthInBytes() - sizeof(header);
  325. if (chunksSize == 0) {
  326. return true;
  327. }
  328. // there is at least one chunk
  329. unsigned int i = 0;
  330. unsigned int size = 0;
  331. while (size < chunksSize) {
  332. if (chunksSize < size + RtcpSdesChunk::Size({})) {
  333. // chunk is truncated
  334. return false;
  335. }
  336. auto chunk = getChunk(i++);
  337. auto chunkSize = chunk->safelyCountChunkSize(chunksSize - size);
  338. if (chunkSize < 0) {
  339. // chunk is invalid
  340. return false;
  341. }
  342. size += chunkSize;
  343. }
  344. return size == chunksSize;
  345. }
  346. unsigned int RtcpSdes::chunksCount() const {
  347. if (!isValid()) {
  348. return 0;
  349. }
  350. uint16_t chunksSize = 4 * (header.length() + 1) - sizeof(header);
  351. unsigned int size = 0;
  352. unsigned int i = 0;
  353. while (size < chunksSize) {
  354. size += getChunk(i++)->getSize();
  355. }
  356. return i;
  357. }
  358. const RtcpSdesChunk *RtcpSdes::getChunk(int num) const {
  359. auto base = &_chunks;
  360. while (num-- > 0) {
  361. auto chunkSize = base->getSize();
  362. base = reinterpret_cast<const RtcpSdesChunk *>(reinterpret_cast<const uint8_t *>(base) +
  363. chunkSize);
  364. }
  365. return reinterpret_cast<const RtcpSdesChunk *>(base);
  366. }
  367. RtcpSdesChunk *RtcpSdes::getChunk(int num) {
  368. auto base = &_chunks;
  369. while (num-- > 0) {
  370. auto chunkSize = base->getSize();
  371. base = reinterpret_cast<RtcpSdesChunk *>(reinterpret_cast<uint8_t *>(base) + chunkSize);
  372. }
  373. return reinterpret_cast<RtcpSdesChunk *>(base);
  374. }
  375. void RtcpSdes::preparePacket(uint8_t chunkCount) {
  376. unsigned int chunkSize = 0;
  377. for (uint8_t i = 0; i < chunkCount; i++) {
  378. auto chunk = getChunk(i);
  379. chunkSize += chunk->getSize();
  380. }
  381. uint16_t length = uint16_t((sizeof(header) + chunkSize) / 4 - 1);
  382. header.prepareHeader(202, chunkCount, length);
  383. }
  384. const RtcpReportBlock *RtcpRr::getReportBlock(int num) const { return &_reportBlocks + num; }
  385. RtcpReportBlock *RtcpRr::getReportBlock(int num) { return &_reportBlocks + num; }
  386. size_t RtcpRr::SizeWithReportBlocks(uint8_t reportCount) {
  387. return sizeof(header) + 4 + size_t(reportCount) * sizeof(RtcpReportBlock);
  388. }
  389. SSRC RtcpRr::senderSSRC() const { return ntohl(_senderSSRC); }
  390. bool RtcpRr::isSenderReport() { return header.payloadType() == 200; }
  391. bool RtcpRr::isReceiverReport() { return header.payloadType() == 201; }
  392. size_t RtcpRr::getSize() const {
  393. // "length" in packet is one less than the number of 32 bit words in the packet.
  394. return sizeof(uint32_t) * (1 + size_t(header.length()));
  395. }
  396. void RtcpRr::preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  397. // "length" in packet is one less than the number of 32 bit words in the packet.
  398. size_t length = (SizeWithReportBlocks(reportCount) / 4) - 1;
  399. header.prepareHeader(201, reportCount, uint16_t(length));
  400. this->_senderSSRC = htonl(senderSSRC);
  401. }
  402. void RtcpRr::setSenderSSRC(SSRC ssrc) { this->_senderSSRC = htonl(ssrc); }
  403. void RtcpRr::log() const {
  404. header.log();
  405. PLOG_VERBOSE << "RTCP RR: "
  406. << " SSRC=" << ntohl(_senderSSRC);
  407. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  408. getReportBlock(i)->log();
  409. }
  410. }
  411. size_t RtcpRemb::SizeWithSSRCs(int count) { return sizeof(RtcpRemb) + (count - 1) * sizeof(SSRC); }
  412. unsigned int RtcpRemb::getSize() const {
  413. // "length" in packet is one less than the number of 32 bit words in the packet.
  414. return sizeof(uint32_t) * (1 + header.header.length());
  415. }
  416. void RtcpRemb::preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate) {
  417. // Report Count becomes the format here.
  418. header.header.prepareHeader(206, 15, 0);
  419. // Always zero.
  420. header.setMediaSourceSSRC(0);
  421. header.setPacketSenderSSRC(senderSSRC);
  422. _id[0] = 'R';
  423. _id[1] = 'E';
  424. _id[2] = 'M';
  425. _id[3] = 'B';
  426. setBitrate(numSSRC, in_bitrate);
  427. }
  428. void RtcpRemb::setBitrate(unsigned int numSSRC, unsigned int in_bitrate) {
  429. unsigned int exp = 0;
  430. while (in_bitrate > 0x3FFFF) {
  431. exp++;
  432. in_bitrate /= 2;
  433. }
  434. // "length" in packet is one less than the number of 32 bit words in the packet.
  435. header.header.setLength(uint16_t((offsetof(RtcpRemb, _ssrc) / sizeof(uint32_t)) - 1 + numSSRC));
  436. _bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | in_bitrate);
  437. }
  438. void RtcpRemb::setSsrc(int iterator, SSRC newSssrc) { _ssrc[iterator] = htonl(newSssrc); }
  439. unsigned int RtcpRemb::getNumSSRC() { return ntohl(_bitrate) >> 24u; }
  440. unsigned int RtcpRemb::getBitrate() {
  441. uint32_t br = ntohl(_bitrate);
  442. uint8_t exp = (br << 8u) >> 26u;
  443. return (br & 0x3FFFF) * static_cast<unsigned int>(pow(exp, 2));
  444. }
  445. unsigned int RtcpPli::Size() { return sizeof(RtcpFbHeader); }
  446. void RtcpPli::preparePacket(SSRC messageSSRC) {
  447. header.header.prepareHeader(206, 1, 2);
  448. header.setPacketSenderSSRC(messageSSRC);
  449. header.setMediaSourceSSRC(messageSSRC);
  450. }
  451. void RtcpPli::log() const { header.log(); }
  452. unsigned int RtcpFir::Size() { return sizeof(RtcpFbHeader) + sizeof(RtcpFirPart); }
  453. void RtcpFir::preparePacket(SSRC messageSSRC, uint8_t seqNo) {
  454. header.header.prepareHeader(206, 4, 2 + 2 * 1);
  455. header.setPacketSenderSSRC(messageSSRC);
  456. header.setMediaSourceSSRC(messageSSRC);
  457. parts[0].ssrc = htonl(messageSSRC);
  458. parts[0].seqNo = seqNo;
  459. }
  460. void RtcpFir::log() const { header.log(); }
  461. uint16_t RtcpNackPart::pid() { return ntohs(_pid); }
  462. uint16_t RtcpNackPart::blp() { return ntohs(_blp); }
  463. void RtcpNackPart::setPid(uint16_t pid) { _pid = htons(pid); }
  464. void RtcpNackPart::setBlp(uint16_t blp) { _blp = htons(blp); }
  465. std::vector<uint16_t> RtcpNackPart::getSequenceNumbers() {
  466. std::vector<uint16_t> result{};
  467. result.reserve(17);
  468. uint16_t p = pid();
  469. result.push_back(p);
  470. uint16_t bitmask = blp();
  471. uint16_t i = p + 1;
  472. while (bitmask > 0) {
  473. if (bitmask & 0x1) {
  474. result.push_back(i);
  475. }
  476. i += 1;
  477. bitmask >>= 1;
  478. }
  479. return result;
  480. }
  481. unsigned int RtcpNack::Size(unsigned int discreteSeqNoCount) {
  482. return offsetof(RtcpNack, parts) + sizeof(RtcpNackPart) * discreteSeqNoCount;
  483. }
  484. unsigned int RtcpNack::getSeqNoCount() { return header.header.length() - 2; }
  485. void RtcpNack::preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount) {
  486. header.header.prepareHeader(205, 1, 2 + uint16_t(discreteSeqNoCount));
  487. header.setMediaSourceSSRC(ssrc);
  488. header.setPacketSenderSSRC(ssrc);
  489. }
  490. bool RtcpNack::addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket) {
  491. if (*fciCount == 0 || missingPacket < *fciPID || missingPacket > (*fciPID + 16)) {
  492. parts[*fciCount].setPid(missingPacket);
  493. parts[*fciCount].setBlp(0);
  494. *fciPID = missingPacket;
  495. (*fciCount)++;
  496. return true;
  497. } else {
  498. // TODO SPEED!
  499. uint16_t blp = parts[(*fciCount) - 1].blp();
  500. uint16_t newBit = uint16_t(1u << (missingPacket - (1 + *fciPID)));
  501. parts[(*fciCount) - 1].setBlp(blp | newBit);
  502. return false;
  503. }
  504. }
  505. uint16_t RtpRtx::getOriginalSeqNo() const { return ntohs(*(uint16_t *)(header.getBody())); }
  506. const char *RtpRtx::getBody() const { return header.getBody() + sizeof(uint16_t); }
  507. char *RtpRtx::getBody() { return header.getBody() + sizeof(uint16_t); }
  508. size_t RtpRtx::getBodySize(size_t totalSize) const {
  509. return totalSize - (getBody() - reinterpret_cast<const char *>(this));
  510. }
  511. size_t RtpRtx::getSize() const { return header.getSize() + sizeof(uint16_t); }
  512. size_t RtpRtx::normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType) {
  513. header.setSeqNumber(getOriginalSeqNo());
  514. header.setSsrc(originalSSRC);
  515. header.setPayloadType(originalPayloadType);
  516. // TODO, the -12 is the size of the header (which is variable!)
  517. memmove(header.getBody(), getBody(), totalSize - getSize());
  518. return totalSize - 2;
  519. }
  520. size_t RtpRtx::copyTo(RtpHeader *dest, size_t totalSize, uint8_t originalPayloadType) {
  521. memmove((char *)dest, (char *)this, header.getSize());
  522. dest->setSeqNumber(getOriginalSeqNo());
  523. dest->setPayloadType(originalPayloadType);
  524. memmove(dest->getBody(), getBody(), getBodySize(totalSize));
  525. return totalSize;
  526. }
  527. }; // namespace rtc