rtp.cpp 19 KB

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