rtp.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * Copyright (c) 2020 Staz M
  3. * Copyright (c) 2020 Paul-Louis Ageneau
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef RTC_RTPL_H
  20. #define RTC_RTPL_H
  21. #include "include.hpp"
  22. #include "log.hpp"
  23. #include "message.hpp"
  24. #include <cmath>
  25. #include <functional>
  26. #include <iostream>
  27. #include <utility>
  28. #ifdef _WIN32
  29. #include <winsock2.h>
  30. #else
  31. #include <arpa/inet.h>
  32. #endif
  33. #ifndef htonll
  34. #define htonll(x) \
  35. ((uint64_t)htonl(((uint64_t)(x)&0xFFFFFFFF) << 32) | (uint64_t)htonl((uint64_t)(x) >> 32))
  36. #endif
  37. #ifndef ntohll
  38. #define ntohll(x) htonll(x)
  39. #endif
  40. namespace rtc {
  41. using std::size_t;
  42. typedef uint32_t SSRC;
  43. #pragma pack(push, 1)
  44. struct RTCP_ReportBlock {
  45. SSRC ssrc;
  46. private:
  47. uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
  48. uint16_t _seqNoCycles;
  49. uint16_t _highestSeqNo;
  50. uint32_t _jitter;
  51. uint32_t _lastReport;
  52. uint32_t _delaySinceLastReport;
  53. public:
  54. void print() {
  55. std::cout << " ssrc:" << ntohl(ssrc) <<
  56. // TODO Implement these reports
  57. // " fractionLost: " << fractionLost <<
  58. // " packetsLost: " << packetsLost <<
  59. " highestSeqNo:" << highestSeqNo() << " seqNoCycles:" << seqNoCycles()
  60. << " jitter:" << jitter() << " lastSR:" << getNTPOfSR()
  61. << " lastSRDelay:" << getDelaySinceSR();
  62. }
  63. void preparePacket(SSRC ssrc, [[maybe_unused]] unsigned int packetsLost,
  64. [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
  65. uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
  66. uint64_t lastSR_DELAY) {
  67. setSeqNo(highestSeqNo, seqNoCycles);
  68. setJitter(jitter);
  69. setSSRC(ssrc);
  70. // Middle 32 bits of NTP Timestamp
  71. // this->lastReport = lastSR_NTP >> 16u;
  72. setNTPOfSR(uint32_t(lastSR_NTP));
  73. setDelaySinceSR(uint32_t(lastSR_DELAY));
  74. // The delay, expressed in units of 1/65536 seconds
  75. // this->delaySinceLastReport = lastSR_DELAY;
  76. }
  77. void inline setSSRC(SSRC ssrc) { this->ssrc = htonl(ssrc); }
  78. SSRC inline getSSRC() const { return ntohl(ssrc); }
  79. void inline setPacketsLost([[maybe_unused]] unsigned int packetsLost,
  80. [[maybe_unused]] unsigned int totalPackets) {
  81. // TODO Implement loss percentages.
  82. _fractionLostAndPacketsLost = 0;
  83. }
  84. unsigned int inline getLossPercentage() const {
  85. // TODO Implement loss percentages.
  86. return 0;
  87. }
  88. unsigned int inline getPacketLostCount() const {
  89. // TODO Implement total packets lost.
  90. return 0;
  91. }
  92. uint16_t inline seqNoCycles() const { return ntohs(_seqNoCycles); }
  93. uint16_t inline highestSeqNo() const { return ntohs(_highestSeqNo); }
  94. uint32_t inline jitter() const { return ntohl(_jitter); }
  95. void inline setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
  96. _highestSeqNo = htons(highestSeqNo);
  97. _seqNoCycles = htons(seqNoCycles);
  98. }
  99. void inline setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
  100. void inline setNTPOfSR(uint32_t ntp) { _lastReport = htonl(ntp >> 16u); }
  101. inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
  102. inline void setDelaySinceSR(uint32_t sr) {
  103. // The delay, expressed in units of 1/65536 seconds
  104. _delaySinceLastReport = htonl(sr);
  105. }
  106. inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
  107. };
  108. struct RTCP_HEADER {
  109. private:
  110. uint8_t _first;
  111. uint8_t _payloadType;
  112. uint16_t _length;
  113. public:
  114. inline uint8_t version() const { return _first >> 6; }
  115. inline bool padding() const { return (_first >> 5) & 0x01; }
  116. inline uint8_t reportCount() const { return _first & 0x0F; }
  117. inline uint8_t payloadType() const { return _payloadType; }
  118. inline uint16_t length() const { return ntohs(_length); }
  119. inline void setPayloadType(uint8_t type) { _payloadType = type; }
  120. inline void setReportCount(uint8_t count) { _first = (_first & 0xF0) | (count & 0x0F); }
  121. inline void setLength(uint16_t length) { _length = htons(length); }
  122. void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
  123. _first = 0x02 << 6; // version 2, no padding
  124. setReportCount(reportCount);
  125. setPayloadType(payloadType);
  126. setLength(length);
  127. }
  128. void print() {
  129. std::cout << "version:" << unsigned(version()) << " padding:" << (padding() ? "T" : "F")
  130. << " reportCount: " << unsigned(reportCount())
  131. << " payloadType:" << unsigned(payloadType()) << " length: " << length();
  132. }
  133. };
  134. struct RTCP_SR {
  135. RTCP_HEADER header;
  136. SSRC senderSSRC;
  137. private:
  138. uint64_t _ntpTimestamp;
  139. uint32_t _rtpTimestamp;
  140. uint32_t _packetCount;
  141. uint32_t _octetCount;
  142. RTCP_ReportBlock _reportBlocks;
  143. public:
  144. void print() {
  145. std::cout << "SR ";
  146. header.print();
  147. std::cout << " SSRC:" << ntohl(senderSSRC) << " NTP TS: " << ntpTimestamp()
  148. << " RTP TS: " << rtpTimestamp() << " packetCount: " << packetCount()
  149. << " octetCount: " << octetCount() << std::endl;
  150. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  151. getReportBlock(i)->print();
  152. std::cout << std::endl;
  153. }
  154. }
  155. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  156. unsigned int length =
  157. ((sizeof(header) + 24 + reportCount * sizeof(RTCP_ReportBlock)) / 4) - 1;
  158. header.prepareHeader(200, reportCount, uint16_t(length));
  159. this->senderSSRC = htonl(senderSSRC);
  160. }
  161. RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  162. [[nodiscard]] size_t getSize() const {
  163. // "length" in packet is one less than the number of 32 bit words in the packet.
  164. return sizeof(uint32_t) * (1 + size_t(header.length()));
  165. }
  166. inline uint32_t ntpTimestamp() const { return ntohll(_ntpTimestamp); }
  167. inline uint32_t rtpTimestamp() const { return ntohl(_rtpTimestamp); }
  168. inline uint32_t packetCount() const { return ntohl(_packetCount); }
  169. inline uint32_t octetCount() const { return ntohl(_octetCount); }
  170. inline void setNtpTimestamp(uint32_t ts) { _ntpTimestamp = htonll(ts); }
  171. inline void setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
  172. };
  173. struct RTCP_RR {
  174. RTCP_HEADER header;
  175. SSRC senderSSRC;
  176. private:
  177. RTCP_ReportBlock _reportBlocks;
  178. public:
  179. void print() {
  180. std::cout << "RR ";
  181. header.print();
  182. std::cout << " SSRC:" << ntohl(senderSSRC) << std::endl;
  183. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  184. getReportBlock(i)->print();
  185. std::cout << std::endl;
  186. }
  187. }
  188. RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  189. inline SSRC getSenderSSRC() const { return ntohl(senderSSRC); }
  190. inline void setSenderSSRC(SSRC ssrc) { this->senderSSRC = htonl(ssrc); }
  191. [[nodiscard]] inline size_t getSize() const {
  192. // "length" in packet is one less than the number of 32 bit words in the packet.
  193. return sizeof(uint32_t) * (1 + size_t(header.length()));
  194. }
  195. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  196. // "length" in packet is one less than the number of 32 bit words in the packet.
  197. size_t length = (sizeWithReportBlocks(reportCount) / 4) - 1;
  198. header.prepareHeader(201, reportCount, uint16_t(length));
  199. this->senderSSRC = htonl(senderSSRC);
  200. }
  201. inline static size_t sizeWithReportBlocks(uint8_t reportCount) {
  202. return sizeof(header) + 4 + size_t(reportCount) * sizeof(RTCP_ReportBlock);
  203. }
  204. };
  205. struct RTP
  206. {
  207. private:
  208. uint8_t _first;
  209. uint8_t _payloadType;
  210. uint16_t _seqNumber;
  211. uint32_t _timestamp;
  212. public:
  213. SSRC ssrc;
  214. SSRC csrc[16];
  215. inline uint8_t version() const { return _first >> 6; }
  216. inline bool padding() const { return (_first >> 5) & 0x01; }
  217. inline uint8_t csrcCount() const { return _first & 0x0F; }
  218. inline uint8_t payloadType() const { return _payloadType; }
  219. inline uint16_t seqNumber() const { return ntohs(_seqNumber); }
  220. inline uint32_t timestamp() const { return ntohl(_timestamp); }
  221. };
  222. struct RTCP_REMB {
  223. RTCP_HEADER header;
  224. SSRC senderSSRC;
  225. SSRC mediaSourceSSRC;
  226. // Unique identifier
  227. const char id[4] = {'R', 'E', 'M', 'B'};
  228. // Num SSRC, Br Exp, Br Mantissa (bit mask)
  229. uint32_t bitrate;
  230. SSRC ssrc[1];
  231. [[nodiscard]] size_t getSize() const {
  232. // "length" in packet is one less than the number of 32 bit words in the packet.
  233. return sizeof(uint32_t) * (1 + size_t(header.length()));
  234. }
  235. void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int bitrate) {
  236. // Report Count becomes the format here.
  237. header.prepareHeader(206, 15, 0);
  238. // Always zero.
  239. mediaSourceSSRC = 0;
  240. this->senderSSRC = htonl(senderSSRC);
  241. setBitrate(numSSRC, bitrate);
  242. }
  243. void setBitrate(unsigned int numSSRC, unsigned int bitrate) {
  244. unsigned int exp = 0;
  245. while (bitrate > pow(2, 18) - 1) {
  246. exp++;
  247. bitrate /= 2;
  248. }
  249. // "length" in packet is one less than the number of 32 bit words in the packet.
  250. header.setLength(uint16_t((offsetof(RTCP_REMB, ssrc) / 4) - 1 + numSSRC));
  251. this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | bitrate);
  252. }
  253. // TODO Make this work
  254. // uint64_t getBitrate() const{
  255. // uint32_t ntohed = ntohl(this->bitrate);
  256. // uint64_t bitrate = ntohed & (unsigned int)(pow(2, 18)-1);
  257. // unsigned int exp = ntohed & ((unsigned int)( (pow(2, 6)-1)) << (32u-8u-6u));
  258. // return bitrate * pow(2,exp);
  259. // }
  260. //
  261. // uint8_t getNumSSRCS() const {
  262. // return ntohl(this->bitrate) & (((unsigned int) pow(2,8)-1) << (32u-8u));
  263. // }
  264. void print() {
  265. std::cout << "REMB ";
  266. header.print();
  267. std::cout << " SSRC:" << ntohl(senderSSRC);
  268. }
  269. void setSSRC(uint8_t iterator, SSRC ssrc) { this->ssrc[iterator] = htonl(ssrc); }
  270. static unsigned int sizeWithSSRCs(int numSSRC) {
  271. return (offsetof(RTCP_REMB, ssrc)) + sizeof(SSRC) * numSSRC;
  272. }
  273. };
  274. #pragma pack(pop)
  275. class RtcpHandler {
  276. public:
  277. virtual void onOutgoing(std::function<void(rtc::message_ptr)> cb) = 0;
  278. virtual std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) = 0;
  279. };
  280. class RtcpSession : public RtcpHandler {
  281. private:
  282. std::function<void(RTP)> onPacketCB;
  283. unsigned int requestedBitrate = 0;
  284. synchronized_callback<rtc::message_ptr> txCB;
  285. SSRC ssrc = 0;
  286. uint32_t greatestSeqNo = 0;
  287. uint64_t syncRTPTS, syncNTPTS;
  288. public:
  289. void onOutgoing(std::function<void(rtc::message_ptr)> cb) override { txCB = cb; }
  290. std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) override {
  291. if (ptr->type == rtc::Message::Type::Binary) {
  292. RTP *rtp = (RTP *)ptr->data();
  293. // https://tools.ietf.org/html/rfc3550#appendix-A.1
  294. if (rtp->version() != 2) {
  295. PLOG_WARNING << "RTP packet is not version 2";
  296. return std::nullopt;
  297. }
  298. if (rtp->payloadType() == 201 || rtp->payloadType() == 200) {
  299. PLOG_WARNING << "RTP packet has a payload type indicating RR/SR";
  300. return std::nullopt;
  301. }
  302. // TODO Implement the padding bit
  303. if (rtp->padding()) {
  304. PLOG_WARNING << "Padding processing not implemented";
  305. }
  306. ssrc = ntohl(rtp->ssrc);
  307. uint32_t seqNo = rtp->seqNumber();
  308. // uint32_t rtpTS = rtp->getTS();
  309. if (greatestSeqNo < seqNo)
  310. greatestSeqNo = seqNo;
  311. return ptr;
  312. }
  313. assert(ptr->type == rtc::Message::Type::Control);
  314. auto rr = (RTCP_RR *)ptr->data();
  315. if (rr->header.payloadType() == 201) {
  316. // RR
  317. ssrc = rr->getSenderSSRC();
  318. rr->print();
  319. std::cout << std::endl;
  320. } else if (rr->header.payloadType() == 200) {
  321. // SR
  322. ssrc = rr->getSenderSSRC();
  323. auto sr = (RTCP_SR *)ptr->data();
  324. syncRTPTS = sr->rtpTimestamp();
  325. syncNTPTS = sr->ntpTimestamp();
  326. sr->print();
  327. std::cout << std::endl;
  328. // TODO For the time being, we will send RR's/REMB's when we get an SR
  329. pushRR(0);
  330. if (requestedBitrate > 0)
  331. pushREMB(requestedBitrate);
  332. }
  333. return std::nullopt;
  334. }
  335. void requestBitrate(unsigned int newBitrate) {
  336. this->requestedBitrate = newBitrate;
  337. PLOG_DEBUG << "[GOOG-REMB] Requesting bitrate: " << newBitrate << std::endl;
  338. pushREMB(newBitrate);
  339. }
  340. private:
  341. void pushREMB(unsigned int bitrate) {
  342. rtc::message_ptr msg =
  343. rtc::make_message(RTCP_REMB::sizeWithSSRCs(1), rtc::Message::Type::Control);
  344. auto remb = (RTCP_REMB *)msg->data();
  345. remb->preparePacket(ssrc, 1, bitrate);
  346. remb->setSSRC(0, ssrc);
  347. remb->print();
  348. std::cout << std::endl;
  349. tx(msg);
  350. }
  351. void pushRR(unsigned int lastSR_delay) {
  352. // std::cout << "size " << RTCP_RR::sizeWithReportBlocks(1) << std::endl;
  353. auto msg = rtc::make_message(RTCP_RR::sizeWithReportBlocks(1), rtc::Message::Type::Control);
  354. auto rr = (RTCP_RR *)msg->data();
  355. rr->preparePacket(ssrc, 1);
  356. rr->getReportBlock(0)->preparePacket(ssrc, 0, 0, greatestSeqNo, 0, 0, syncNTPTS,
  357. lastSR_delay);
  358. rr->print();
  359. std::cout << std::endl;
  360. tx(msg);
  361. }
  362. void tx(message_ptr msg) {
  363. try {
  364. txCB(msg);
  365. } catch (const std::exception &e) {
  366. LOG_DEBUG << "RTCP tx failed: " << e.what();
  367. }
  368. }
  369. };
  370. } // namespace rtc
  371. #endif // RTC_RTPL_H