rtp.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * Copyright (c) 2020 Staz Modrzynski
  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 WEBRTC_SERVER_RTP_HPP
  20. #define WEBRTC_SERVER_RTP_HPP
  21. #include <cmath>
  22. #include <netinet/in.h>
  23. #ifndef htonll
  24. #define htonll(x) \
  25. ((uint64_t)htonl(((uint64_t)(x)&0xFFFFFFFF) << 32) | (uint64_t)htonl((uint64_t)(x) >> 32))
  26. #endif
  27. #ifndef ntohll
  28. #define ntohll(x) htonll(x)
  29. #endif
  30. namespace rtc {
  31. typedef uint32_t SSRC;
  32. #pragma pack(push, 1)
  33. struct RTP {
  34. private:
  35. uint8_t _first;
  36. uint8_t _payloadType;
  37. uint16_t _seqNumber;
  38. uint32_t _timestamp;
  39. public:
  40. SSRC ssrc;
  41. SSRC csrc[16];
  42. inline uint8_t version() const { return _first >> 6; }
  43. inline bool padding() const { return (_first >> 5) & 0x01; }
  44. inline uint8_t csrcCount() const { return _first & 0x0F; }
  45. inline uint8_t payloadType() const { return _payloadType; }
  46. inline uint16_t seqNumber() const { return ntohs(_seqNumber); }
  47. inline uint32_t timestamp() const { return ntohl(_timestamp); }
  48. };
  49. struct RTCP_ReportBlock {
  50. SSRC ssrc;
  51. private:
  52. uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
  53. uint16_t _seqNoCycles;
  54. uint16_t _highestSeqNo;
  55. uint32_t _jitter;
  56. uint32_t _lastReport;
  57. uint32_t _delaySinceLastReport;
  58. public:
  59. inline void preparePacket(SSRC ssrc, [[maybe_unused]] unsigned int packetsLost,
  60. [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
  61. uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
  62. uint64_t lastSR_DELAY) {
  63. setSeqNo(highestSeqNo, seqNoCycles);
  64. setJitter(jitter);
  65. setSSRC(ssrc);
  66. // Middle 32 bits of NTP Timestamp
  67. // this->lastReport = lastSR_NTP >> 16u;
  68. setNTPOfSR(uint32_t(lastSR_NTP));
  69. setDelaySinceSR(uint32_t(lastSR_DELAY));
  70. // The delay, expressed in units of 1/65536 seconds
  71. // this->delaySinceLastReport = lastSR_DELAY;
  72. }
  73. inline void setSSRC(SSRC ssrc) { this->ssrc = htonl(ssrc); }
  74. inline SSRC getSSRC() const { return ntohl(ssrc); }
  75. inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost,
  76. [[maybe_unused]] unsigned int totalPackets) {
  77. // TODO Implement loss percentages.
  78. _fractionLostAndPacketsLost = 0;
  79. }
  80. inline unsigned int getLossPercentage() const {
  81. // TODO Implement loss percentages.
  82. return 0;
  83. }
  84. inline unsigned int getPacketLostCount() const {
  85. // TODO Implement total packets lost.
  86. return 0;
  87. }
  88. inline uint16_t seqNoCycles() const { return ntohs(_seqNoCycles); }
  89. inline uint16_t highestSeqNo() const { return ntohs(_highestSeqNo); }
  90. inline uint32_t jitter() const { return ntohl(_jitter); }
  91. inline void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
  92. _highestSeqNo = htons(highestSeqNo);
  93. _seqNoCycles = htons(seqNoCycles);
  94. }
  95. inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
  96. inline void setNTPOfSR(uint32_t ntp) { _lastReport = htonl(ntp >> 16u); }
  97. inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
  98. inline void setDelaySinceSR(uint32_t sr) {
  99. // The delay, expressed in units of 1/65536 seconds
  100. _delaySinceLastReport = htonl(sr);
  101. }
  102. inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
  103. inline void log() const {
  104. PLOG_DEBUG << "RTCP report block: "
  105. << "ssrc="
  106. << ntohl(ssrc)
  107. // TODO: Implement these reports
  108. // << ", fractionLost=" << fractionLost
  109. // << ", packetsLost=" << packetsLost
  110. << ", highestSeqNo=" << highestSeqNo() << ", seqNoCycles=" << seqNoCycles()
  111. << ", jitter=" << jitter() << ", lastSR=" << getNTPOfSR()
  112. << ", lastSRDelay=" << getDelaySinceSR();
  113. }
  114. };
  115. struct RTCP_HEADER {
  116. private:
  117. uint8_t _first;
  118. uint8_t _payloadType;
  119. uint16_t _length;
  120. public:
  121. inline uint8_t version() const { return _first >> 6; }
  122. inline bool padding() const { return (_first >> 5) & 0x01; }
  123. inline uint8_t reportCount() const { return _first & 0x0F; }
  124. inline uint8_t payloadType() const { return _payloadType; }
  125. inline uint16_t length() const { return ntohs(_length); }
  126. inline void setPayloadType(uint8_t type) { _payloadType = type; }
  127. inline void setReportCount(uint8_t count) { _first = (_first & 0xF0) | (count & 0x0F); }
  128. inline void setLength(uint16_t length) { _length = htons(length); }
  129. inline void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
  130. _first = 0x02 << 6; // version 2, no padding
  131. setReportCount(reportCount);
  132. setPayloadType(payloadType);
  133. setLength(length);
  134. }
  135. inline void log() const {
  136. PLOG_DEBUG << "RTCP header: "
  137. << "version=" << unsigned(version()) << ", padding=" << padding()
  138. << ", reportCount=" << unsigned(reportCount())
  139. << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
  140. }
  141. };
  142. struct RTCP_SR {
  143. RTCP_HEADER header;
  144. SSRC senderSSRC;
  145. private:
  146. uint64_t _ntpTimestamp;
  147. uint32_t _rtpTimestamp;
  148. uint32_t _packetCount;
  149. uint32_t _octetCount;
  150. RTCP_ReportBlock _reportBlocks;
  151. public:
  152. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  153. unsigned int length =
  154. ((sizeof(header) + 24 + reportCount * sizeof(RTCP_ReportBlock)) / 4) - 1;
  155. header.prepareHeader(200, reportCount, uint16_t(length));
  156. this->senderSSRC = htonl(senderSSRC);
  157. }
  158. inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  159. inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; }
  160. [[nodiscard]] inline size_t getSize() const {
  161. // "length" in packet is one less than the number of 32 bit words in the packet.
  162. return sizeof(uint32_t) * (1 + size_t(header.length()));
  163. }
  164. inline uint32_t ntpTimestamp() const { return ntohll(_ntpTimestamp); }
  165. inline uint32_t rtpTimestamp() const { return ntohl(_rtpTimestamp); }
  166. inline uint32_t packetCount() const { return ntohl(_packetCount); }
  167. inline uint32_t octetCount() const { return ntohl(_octetCount); }
  168. inline void setNtpTimestamp(uint32_t ts) { _ntpTimestamp = htonll(ts); }
  169. inline void setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
  170. inline void log() const {
  171. header.log();
  172. PLOG_DEBUG << "RTCP SR: "
  173. << " SSRC=" << ntohl(senderSSRC) << ", NTP_TS=" << ntpTimestamp()
  174. << ", RTP_TS=" << rtpTimestamp() << ", packetCount=" << packetCount()
  175. << ", octetCount=" << octetCount();
  176. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  177. getReportBlock(i)->log();
  178. }
  179. }
  180. };
  181. struct RTCP_RR {
  182. RTCP_HEADER header;
  183. SSRC senderSSRC;
  184. private:
  185. RTCP_ReportBlock _reportBlocks;
  186. public:
  187. inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  188. inline const RTCP_ReportBlock *getReportBlock(int num) const { 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. inline void log() const {
  205. header.log();
  206. PLOG_DEBUG << "RTCP RR: "
  207. << " SSRC=" << ntohl(senderSSRC);
  208. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  209. getReportBlock(i)->log();
  210. }
  211. }
  212. };
  213. struct RTCP_REMB {
  214. RTCP_HEADER header;
  215. SSRC senderSSRC;
  216. SSRC mediaSourceSSRC;
  217. // Unique identifier
  218. const char id[4] = {'R', 'E', 'M', 'B'};
  219. // Num SSRC, Br Exp, Br Mantissa (bit mask)
  220. uint32_t bitrate;
  221. SSRC ssrc[1];
  222. [[nodiscard]] inline size_t getSize() const {
  223. // "length" in packet is one less than the number of 32 bit words in the packet.
  224. return sizeof(uint32_t) * (1 + size_t(header.length()));
  225. }
  226. inline void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int bitrate) {
  227. // Report Count becomes the format here.
  228. header.prepareHeader(206, 15, 0);
  229. // Always zero.
  230. mediaSourceSSRC = 0;
  231. this->senderSSRC = htonl(senderSSRC);
  232. setBitrate(numSSRC, bitrate);
  233. }
  234. inline void setBitrate(unsigned int numSSRC, unsigned int bitrate) {
  235. unsigned int exp = 0;
  236. while (bitrate > pow(2, 18) - 1) {
  237. exp++;
  238. bitrate /= 2;
  239. }
  240. // "length" in packet is one less than the number of 32 bit words in the packet.
  241. header.setLength(uint16_t(((sizeof(header) + 4 * 2 + 4 + 4) / 4) - 1 + numSSRC));
  242. this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | bitrate);
  243. }
  244. // TODO Make this work
  245. // uint64_t getBitrate() const{
  246. // uint32_t ntohed = ntohl(this->bitrate);
  247. // uint64_t bitrate = ntohed & (unsigned int)(pow(2, 18)-1);
  248. // unsigned int exp = ntohed & ((unsigned int)( (pow(2, 6)-1)) << (32u-8u-6u));
  249. // return bitrate * pow(2,exp);
  250. // }
  251. //
  252. // uint8_t getNumSSRCS() const {
  253. // return ntohl(this->bitrate) & (((unsigned int) pow(2,8)-1) << (32u-8u));
  254. // }
  255. inline void setSSRC(uint8_t iterator, SSRC ssrc) { this->ssrc[iterator] = htonl(ssrc); }
  256. inline void log() const {
  257. header.log();
  258. PLOG_DEBUG << "RTCP REMB: "
  259. << " SSRC=" << ntohl(senderSSRC);
  260. }
  261. static unsigned int sizeWithSSRCs(int numSSRC) {
  262. return (sizeof(header) + 4 * 2 + 4 + 4) + sizeof(SSRC) * numSSRC;
  263. }
  264. };
  265. #pragma pack(pop)
  266. };
  267. #endif //WEBRTC_SERVER_RTP_HPP