rtp.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. SSRC _ssrc;
  40. public:
  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. inline uint32_t ssrc() const { return ntohl(_ssrc);}
  49. inline size_t getSize() const {
  50. return ((char*)&_ssrc) - ((char*)this) + sizeof(SSRC)*csrcCount();
  51. }
  52. char * getBody() const {
  53. return ((char*) this) + getSize();
  54. }
  55. inline void setSeqNumber(uint16_t newSeqNo) {
  56. _seqNumber = htons(newSeqNo);
  57. }
  58. inline void setPayloadType(uint16_t newPayloadType) {
  59. _payloadType = newPayloadType;
  60. }
  61. inline void setSsrc(uint32_t ssrc) {
  62. _ssrc = htonl(ssrc);
  63. }
  64. };
  65. struct RTCP_ReportBlock {
  66. SSRC ssrc;
  67. private:
  68. uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
  69. uint16_t _seqNoCycles;
  70. uint16_t _highestSeqNo;
  71. uint32_t _jitter;
  72. uint32_t _lastReport;
  73. uint32_t _delaySinceLastReport;
  74. public:
  75. inline void preparePacket(SSRC ssrc, [[maybe_unused]] unsigned int packetsLost,
  76. [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
  77. uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
  78. uint64_t lastSR_DELAY) {
  79. setSeqNo(highestSeqNo, seqNoCycles);
  80. setJitter(jitter);
  81. setSSRC(ssrc);
  82. // Middle 32 bits of NTP Timestamp
  83. // this->lastReport = lastSR_NTP >> 16u;
  84. setNTPOfSR(uint32_t(lastSR_NTP));
  85. setDelaySinceSR(uint32_t(lastSR_DELAY));
  86. // The delay, expressed in units of 1/65536 seconds
  87. // this->delaySinceLastReport = lastSR_DELAY;
  88. }
  89. inline void setSSRC(SSRC ssrc) { this->ssrc = htonl(ssrc); }
  90. inline SSRC getSSRC() const { return ntohl(ssrc); }
  91. inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost,
  92. [[maybe_unused]] unsigned int totalPackets) {
  93. // TODO Implement loss percentages.
  94. _fractionLostAndPacketsLost = 0;
  95. }
  96. inline unsigned int getLossPercentage() const {
  97. // TODO Implement loss percentages.
  98. return 0;
  99. }
  100. inline unsigned int getPacketLostCount() const {
  101. // TODO Implement total packets lost.
  102. return 0;
  103. }
  104. inline uint16_t seqNoCycles() const { return ntohs(_seqNoCycles); }
  105. inline uint16_t highestSeqNo() const { return ntohs(_highestSeqNo); }
  106. inline uint32_t jitter() const { return ntohl(_jitter); }
  107. inline void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
  108. _highestSeqNo = htons(highestSeqNo);
  109. _seqNoCycles = htons(seqNoCycles);
  110. }
  111. inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
  112. inline void setNTPOfSR(uint32_t ntp) { _lastReport = htonl(ntp >> 16u); }
  113. inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
  114. inline void setDelaySinceSR(uint32_t sr) {
  115. // The delay, expressed in units of 1/65536 seconds
  116. _delaySinceLastReport = htonl(sr);
  117. }
  118. inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
  119. inline void log() const {
  120. PLOG_DEBUG << "RTCP report block: "
  121. << "ssrc="
  122. << ntohl(ssrc)
  123. // TODO: Implement these reports
  124. // << ", fractionLost=" << fractionLost
  125. // << ", packetsLost=" << packetsLost
  126. << ", highestSeqNo=" << highestSeqNo() << ", seqNoCycles=" << seqNoCycles()
  127. << ", jitter=" << jitter() << ", lastSR=" << getNTPOfSR()
  128. << ", lastSRDelay=" << getDelaySinceSR();
  129. }
  130. };
  131. struct RTCP_HEADER {
  132. private:
  133. uint8_t _first;
  134. uint8_t _payloadType;
  135. uint16_t _length;
  136. public:
  137. inline uint8_t version() const { return _first >> 6; }
  138. inline bool padding() const { return (_first >> 5) & 0x01; }
  139. inline uint8_t reportCount() const { return _first & 0x0F; }
  140. inline uint8_t payloadType() const { return _payloadType; }
  141. inline uint16_t length() const { return ntohs(_length); }
  142. inline void setPayloadType(uint8_t type) { _payloadType = type; }
  143. inline void setReportCount(uint8_t count) { _first = (_first & 0xF0) | (count & 0x0F); }
  144. inline void setLength(uint16_t length) { _length = htons(length); }
  145. inline void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
  146. _first = 0x02 << 6; // version 2, no padding
  147. setReportCount(reportCount);
  148. setPayloadType(payloadType);
  149. setLength(length);
  150. }
  151. inline void log() const {
  152. PLOG_DEBUG << "RTCP header: "
  153. << "version=" << unsigned(version()) << ", padding=" << padding()
  154. << ", reportCount=" << unsigned(reportCount())
  155. << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
  156. }
  157. };
  158. struct RTCP_FB_HEADER {
  159. RTCP_HEADER header;
  160. SSRC packetSender;
  161. SSRC mediaSource;
  162. [[nodiscard]] SSRC getPacketSenderSSRC() const {
  163. return ntohl(packetSender);
  164. }
  165. [[nodiscard]] SSRC getMediaSourceSSRC() const {
  166. return ntohl(mediaSource);
  167. }
  168. void setPacketSenderSSRC(SSRC ssrc) {
  169. this->packetSender = htonl(ssrc);
  170. }
  171. void setMediaSourceSSRC(SSRC ssrc) {
  172. this->mediaSource = htonl(ssrc);
  173. }
  174. void log() {
  175. header.log();
  176. PLOG_DEBUG << "FB: " << " packet sender: " << getPacketSenderSSRC() << " media source: " << getMediaSourceSSRC();
  177. }
  178. };
  179. struct RTCP_SR {
  180. RTCP_HEADER header;
  181. SSRC senderSSRC;
  182. private:
  183. uint64_t _ntpTimestamp;
  184. uint32_t _rtpTimestamp;
  185. uint32_t _packetCount;
  186. uint32_t _octetCount;
  187. RTCP_ReportBlock _reportBlocks;
  188. public:
  189. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  190. unsigned int length =
  191. ((sizeof(header) + 24 + reportCount * sizeof(RTCP_ReportBlock)) / 4) - 1;
  192. header.prepareHeader(200, reportCount, uint16_t(length));
  193. this->senderSSRC = htonl(senderSSRC);
  194. }
  195. inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  196. inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; }
  197. [[nodiscard]] inline size_t getSize() const {
  198. // "length" in packet is one less than the number of 32 bit words in the packet.
  199. return sizeof(uint32_t) * (1 + size_t(header.length()));
  200. }
  201. inline uint32_t ntpTimestamp() const { return ntohll(_ntpTimestamp); }
  202. inline uint32_t rtpTimestamp() const { return ntohl(_rtpTimestamp); }
  203. inline uint32_t packetCount() const { return ntohl(_packetCount); }
  204. inline uint32_t octetCount() const { return ntohl(_octetCount); }
  205. inline void setNtpTimestamp(uint32_t ts) { _ntpTimestamp = htonll(ts); }
  206. inline void setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
  207. inline void log() const {
  208. header.log();
  209. PLOG_DEBUG << "RTCP SR: "
  210. << " SSRC=" << ntohl(senderSSRC) << ", NTP_TS=" << ntpTimestamp()
  211. << ", RTP_TS=" << rtpTimestamp() << ", packetCount=" << packetCount()
  212. << ", octetCount=" << octetCount();
  213. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  214. getReportBlock(i)->log();
  215. }
  216. }
  217. };
  218. struct RTCP_RR {
  219. RTCP_HEADER header;
  220. SSRC senderSSRC;
  221. private:
  222. RTCP_ReportBlock _reportBlocks;
  223. public:
  224. inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
  225. inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; }
  226. inline SSRC getSenderSSRC() const { return ntohl(senderSSRC); }
  227. inline void setSenderSSRC(SSRC ssrc) { this->senderSSRC = htonl(ssrc); }
  228. [[nodiscard]] inline size_t getSize() const {
  229. // "length" in packet is one less than the number of 32 bit words in the packet.
  230. return sizeof(uint32_t) * (1 + size_t(header.length()));
  231. }
  232. inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
  233. // "length" in packet is one less than the number of 32 bit words in the packet.
  234. size_t length = (sizeWithReportBlocks(reportCount) / 4) - 1;
  235. header.prepareHeader(201, reportCount, uint16_t(length));
  236. this->senderSSRC = htonl(senderSSRC);
  237. }
  238. inline static size_t sizeWithReportBlocks(uint8_t reportCount) {
  239. return sizeof(header) + 4 + size_t(reportCount) * sizeof(RTCP_ReportBlock);
  240. }
  241. inline void log() const {
  242. header.log();
  243. PLOG_DEBUG << "RTCP RR: "
  244. << " SSRC=" << ntohl(senderSSRC);
  245. for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
  246. getReportBlock(i)->log();
  247. }
  248. }
  249. };
  250. struct RTCP_REMB {
  251. RTCP_HEADER header;
  252. SSRC senderSSRC;
  253. SSRC mediaSourceSSRC;
  254. // Unique identifier
  255. const char id[4] = {'R', 'E', 'M', 'B'};
  256. // Num SSRC, Br Exp, Br Mantissa (bit mask)
  257. uint32_t bitrate;
  258. SSRC ssrc[1];
  259. [[nodiscard]] inline size_t getSize() const {
  260. // "length" in packet is one less than the number of 32 bit words in the packet.
  261. return sizeof(uint32_t) * (1 + size_t(header.length()));
  262. }
  263. inline void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int bitrate) {
  264. // Report Count becomes the format here.
  265. header.prepareHeader(206, 15, 0);
  266. // Always zero.
  267. mediaSourceSSRC = 0;
  268. this->senderSSRC = htonl(senderSSRC);
  269. setBitrate(numSSRC, bitrate);
  270. }
  271. inline void setBitrate(unsigned int numSSRC, unsigned int bitrate) {
  272. unsigned int exp = 0;
  273. while (bitrate > pow(2, 18) - 1) {
  274. exp++;
  275. bitrate /= 2;
  276. }
  277. // "length" in packet is one less than the number of 32 bit words in the packet.
  278. header.setLength(uint16_t(((sizeof(header) + 4 * 2 + 4 + 4) / 4) - 1 + numSSRC));
  279. this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | bitrate);
  280. }
  281. // TODO Make this work
  282. // uint64_t getBitrate() const{
  283. // uint32_t ntohed = ntohl(this->bitrate);
  284. // uint64_t bitrate = ntohed & (unsigned int)(pow(2, 18)-1);
  285. // unsigned int exp = ntohed & ((unsigned int)( (pow(2, 6)-1)) << (32u-8u-6u));
  286. // return bitrate * pow(2,exp);
  287. // }
  288. //
  289. // uint8_t getNumSSRCS() const {
  290. // return ntohl(this->bitrate) & (((unsigned int) pow(2,8)-1) << (32u-8u));
  291. // }
  292. inline void setSSRC(uint8_t iterator, SSRC ssrc) { this->ssrc[iterator] = htonl(ssrc); }
  293. inline void log() const {
  294. header.log();
  295. PLOG_DEBUG << "RTCP REMB: "
  296. << " SSRC=" << ntohl(senderSSRC);
  297. }
  298. static unsigned int sizeWithSSRCs(int numSSRC) {
  299. return (sizeof(header) + 4 * 2 + 4 + 4) + sizeof(SSRC) * numSSRC;
  300. }
  301. };
  302. struct RTCP_PLI {
  303. RTCP_FB_HEADER header;
  304. void preparePacket(SSRC messageSSRC) {
  305. header.header.prepareHeader(206, 1, 2);
  306. header.setPacketSenderSSRC(messageSSRC);
  307. header.setMediaSourceSSRC(messageSSRC);
  308. }
  309. void print() {
  310. header.log();
  311. }
  312. [[nodiscard]] static unsigned int size() {
  313. return sizeof(RTCP_FB_HEADER);
  314. }
  315. };
  316. struct RTCP_FIR_PART {
  317. uint32_t ssrc;
  318. #if __BYTE_ORDER == __BIG_ENDIAN
  319. uint32_t seqNo: 8;
  320. uint32_t: 24;
  321. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  322. uint32_t: 24;
  323. uint32_t seqNo: 8;
  324. #endif
  325. };
  326. struct RTCP_FIR {
  327. RTCP_FB_HEADER header;
  328. RTCP_FIR_PART parts[1];
  329. void preparePacket(SSRC messageSSRC, uint8_t seqNo) {
  330. header.header.prepareHeader(206, 4, 2 + 2 * 1);
  331. header.setPacketSenderSSRC(messageSSRC);
  332. header.setMediaSourceSSRC(messageSSRC);
  333. parts[0].ssrc = htonl(messageSSRC);
  334. parts[0].seqNo = seqNo;
  335. }
  336. void print() {
  337. header.log();
  338. }
  339. [[nodiscard]] static unsigned int size() {
  340. return sizeof(RTCP_FB_HEADER) + sizeof(RTCP_FIR_PART);
  341. }
  342. };
  343. struct RTCP_NACK_PART {
  344. uint16_t pid;
  345. uint16_t blp;
  346. };
  347. class RTCP_NACK {
  348. RTCP_FB_HEADER header;
  349. RTCP_NACK_PART parts[1];
  350. public:
  351. void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount) {
  352. header.header.prepareHeader(205, 1, 2 + discreteSeqNoCount);
  353. header.setMediaSourceSSRC(ssrc);
  354. header.setPacketSenderSSRC(ssrc);
  355. }
  356. /**
  357. * Add a packet to the list of missing packets.
  358. * @param fciCount The number of FCI fields that are present in this packet.
  359. * Let the number start at zero and let this function grow the number.
  360. * @param fciPID The seq no of the active FCI. It will be initialized automatically, and will change automatically.
  361. * @param missingPacket The seq no of the missing packet. This will be added to the queue.
  362. * @return true if the packet has grown, false otherwise.
  363. */
  364. bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, const uint16_t &missingPacket) {
  365. if (*fciCount == 0 || missingPacket < *fciPID || missingPacket > (*fciPID + 16)) {
  366. parts[*fciCount].pid = htons(missingPacket);
  367. parts[*fciCount].blp = 0;
  368. *fciPID = missingPacket;
  369. (*fciCount)++;
  370. return true;
  371. } else {
  372. // TODO SPEEED!
  373. parts[(*fciCount) - 1].blp = htons(
  374. ntohs(parts[(*fciCount) - 1].blp) | (1u << (unsigned int) (missingPacket - *fciPID)));
  375. return false;
  376. }
  377. }
  378. [[nodiscard]] static unsigned int getSize(unsigned int discreteSeqNoCount) {
  379. return offsetof(RTCP_NACK, parts) + sizeof(RTCP_NACK_PART) * discreteSeqNoCount;
  380. }
  381. };
  382. class RTP_RTX {
  383. private:
  384. RTP header;
  385. public:
  386. size_t copyTo(RTP *dest, size_t totalSize, uint8_t originalPayloadType) {
  387. memmove((char*)dest, (char*)this, header.getSize());
  388. dest->setSeqNumber(getOriginalSeqNo());
  389. dest->setPayloadType(originalPayloadType);
  390. memmove(dest->getBody(), getBody(), getBodySize(totalSize));
  391. return totalSize;
  392. }
  393. [[nodiscard]] uint16_t getOriginalSeqNo() const {
  394. return ntohs(*(uint16_t *) (header.getBody()));
  395. }
  396. char *getBody() {
  397. return header.getBody() + sizeof(uint16_t);
  398. }
  399. size_t getBodySize(size_t totalSize) {
  400. return totalSize - ((char *) getBody() - (char *) this);
  401. }
  402. RTP &getHeader() {
  403. return header;
  404. }
  405. size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType) {
  406. header.setSeqNumber(getOriginalSeqNo());
  407. header.setSsrc(originalSSRC); // TODO Endianess
  408. header.setPayloadType(originalPayloadType);
  409. // TODO, the -12 is the size of the header (which is variable!)
  410. memmove(header.getBody(), header.getBody() + 2, totalSize - 12 - sizeof(uint16_t));
  411. return totalSize - sizeof(uint16_t);
  412. }
  413. };
  414. #pragma pack(pop)
  415. };
  416. #endif //WEBRTC_SERVER_RTP_HPP