rtp.cpp 21 KB

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