rtp.hpp 13 KB

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