rtp.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #ifndef RTC_RTP_HPP
  21. #define RTC_RTP_HPP
  22. #include "common.hpp"
  23. #include <vector>
  24. namespace rtc {
  25. typedef uint32_t SSRC;
  26. #pragma pack(push, 1)
  27. struct RTC_CPP_EXPORT RTP {
  28. uint8_t _first;
  29. uint8_t _payloadType;
  30. uint16_t _seqNumber;
  31. uint32_t _timestamp;
  32. SSRC _ssrc;
  33. SSRC _csrc[16];
  34. [[nodiscard]] uint8_t version() const;
  35. [[nodiscard]] bool padding() const;
  36. [[nodiscard]] bool extension() const;
  37. [[nodiscard]] uint8_t csrcCount() const;
  38. [[nodiscard]] uint8_t marker() const;
  39. [[nodiscard]] uint8_t payloadType() const;
  40. [[nodiscard]] uint16_t seqNumber() const;
  41. [[nodiscard]] uint32_t timestamp() const;
  42. [[nodiscard]] uint32_t ssrc() const;
  43. [[nodiscard]] size_t getSize() const;
  44. [[nodiscard]] const char *getBody() const;
  45. [[nodiscard]] char *getBody();
  46. void log() const;
  47. void preparePacket();
  48. void setSeqNumber(uint16_t newSeqNo);
  49. void setPayloadType(uint8_t newPayloadType);
  50. void setSsrc(uint32_t in_ssrc);
  51. void setMarker(bool marker);
  52. void setTimestamp(uint32_t i);
  53. };
  54. struct RTC_CPP_EXPORT RTCP_ReportBlock {
  55. SSRC _ssrc;
  56. uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
  57. uint16_t _seqNoCycles;
  58. uint16_t _highestSeqNo;
  59. uint32_t _jitter;
  60. uint32_t _lastReport;
  61. uint32_t _delaySinceLastReport;
  62. [[nodiscard]] uint16_t seqNoCycles() const;
  63. [[nodiscard]] uint16_t highestSeqNo() const;
  64. [[nodiscard]] uint32_t jitter() const;
  65. [[nodiscard]] uint32_t delaySinceSR() const;
  66. [[nodiscard]] SSRC getSSRC() const;
  67. [[nodiscard]] uint32_t getNTPOfSR() const;
  68. [[nodiscard]] unsigned int getLossPercentage() const;
  69. [[nodiscard]] unsigned int getPacketLostCount() const;
  70. void preparePacket(SSRC in_ssrc, unsigned int packetsLost, unsigned int totalPackets,
  71. uint16_t highestSeqNo, uint16_t seqNoCycles, uint32_t jitter,
  72. uint64_t lastSR_NTP, uint64_t lastSR_DELAY);
  73. void setSSRC(SSRC in_ssrc);
  74. void setPacketsLost(unsigned int packetsLost, unsigned int totalPackets);
  75. void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles);
  76. void setJitter(uint32_t jitter);
  77. void setNTPOfSR(uint64_t ntp);
  78. void setDelaySinceSR(uint32_t sr);
  79. void log() const;
  80. };
  81. struct RTC_CPP_EXPORT RTCP_HEADER {
  82. uint8_t _first;
  83. uint8_t _payloadType;
  84. uint16_t _length;
  85. [[nodiscard]] uint8_t version() const;
  86. [[nodiscard]] bool padding() const;
  87. [[nodiscard]] uint8_t reportCount() const;
  88. [[nodiscard]] uint8_t payloadType() const;
  89. [[nodiscard]] uint16_t length() const;
  90. [[nodiscard]] size_t lengthInBytes() const;
  91. void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length);
  92. void setPayloadType(uint8_t type);
  93. void setReportCount(uint8_t count);
  94. void setLength(uint16_t length);
  95. void log() const;
  96. };
  97. struct RTC_CPP_EXPORT RTCP_FB_HEADER {
  98. RTCP_HEADER header;
  99. SSRC _packetSender;
  100. SSRC _mediaSource;
  101. [[nodiscard]] SSRC packetSenderSSRC() const;
  102. [[nodiscard]] SSRC mediaSourceSSRC() const;
  103. void setPacketSenderSSRC(SSRC ssrc);
  104. void setMediaSourceSSRC(SSRC ssrc);
  105. void log() const;
  106. };
  107. struct RTC_CPP_EXPORT RTCP_SR {
  108. RTCP_HEADER header;
  109. SSRC _senderSSRC;
  110. uint64_t _ntpTimestamp;
  111. uint32_t _rtpTimestamp;
  112. uint32_t _packetCount;
  113. uint32_t _octetCount;
  114. RTCP_ReportBlock _reportBlocks;
  115. [[nodiscard]] static unsigned int Size(unsigned int reportCount);
  116. [[nodiscard]] uint64_t ntpTimestamp() const;
  117. [[nodiscard]] uint32_t rtpTimestamp() const;
  118. [[nodiscard]] uint32_t packetCount() const;
  119. [[nodiscard]] uint32_t octetCount() const;
  120. [[nodiscard]] uint32_t senderSSRC() const;
  121. [[nodiscard]] const RTCP_ReportBlock *getReportBlock(int num) const;
  122. [[nodiscard]] RTCP_ReportBlock *getReportBlock(int num);
  123. [[nodiscard]] unsigned int size(unsigned int reportCount);
  124. [[nodiscard]] size_t getSize() const;
  125. void preparePacket(SSRC senderSSRC, uint8_t reportCount);
  126. void setNtpTimestamp(uint64_t ts);
  127. void setRtpTimestamp(uint32_t ts);
  128. void setOctetCount(uint32_t ts);
  129. void setPacketCount(uint32_t ts);
  130. void log() const;
  131. };
  132. struct RTC_CPP_EXPORT RTCP_SDES_ITEM {
  133. uint8_t type;
  134. uint8_t _length;
  135. char _text[1];
  136. [[nodiscard]] static unsigned int Size(uint8_t textLength);
  137. [[nodiscard]] string text() const;
  138. [[nodiscard]] uint8_t length() const;
  139. void setText(string text);
  140. };
  141. struct RTCP_SDES_CHUNK {
  142. SSRC _ssrc;
  143. RTCP_SDES_ITEM _items;
  144. [[nodiscard]] static unsigned int Size(const std::vector<uint8_t> textLengths);
  145. [[nodiscard]] SSRC ssrc() const;
  146. void setSSRC(SSRC ssrc);
  147. // Get item at given index
  148. // All items with index < num must be valid, otherwise this function has undefined behaviour
  149. // (use safelyCountChunkSize() to check if chunk is valid).
  150. [[nodiscard]] const RTCP_SDES_ITEM *getItem(int num) const;
  151. [[nodiscard]] RTCP_SDES_ITEM *getItem(int num);
  152. // Get size of chunk
  153. // All items must be valid, otherwise this function has undefined behaviour (use
  154. // safelyCountChunkSize() to check if chunk is valid)
  155. [[nodiscard]] unsigned int getSize() const;
  156. long safelyCountChunkSize(size_t maxChunkSize) const;
  157. };
  158. struct RTC_CPP_EXPORT RTCP_SDES {
  159. RTCP_HEADER header;
  160. RTCP_SDES_CHUNK _chunks;
  161. [[nodiscard]] static unsigned int Size(const std::vector<std::vector<uint8_t>> lengths);
  162. bool isValid() const;
  163. // Returns number of chunks in this packet
  164. // Returns 0 if packet is invalid
  165. unsigned int chunksCount() const;
  166. // Get chunk at given index
  167. // All chunks (and their items) with index < `num` must be valid, otherwise this function has
  168. // undefined behaviour (use `isValid` to check if chunk is valid).
  169. const RTCP_SDES_CHUNK *getChunk(int num) const;
  170. RTCP_SDES_CHUNK *getChunk(int num);
  171. void preparePacket(uint8_t chunkCount);
  172. };
  173. struct RTC_CPP_EXPORT RTCP_RR {
  174. RTCP_HEADER header;
  175. SSRC _senderSSRC;
  176. RTCP_ReportBlock _reportBlocks;
  177. [[nodiscard]] static size_t SizeWithReportBlocks(uint8_t reportCount);
  178. SSRC senderSSRC() const;
  179. bool isSenderReport();
  180. bool isReceiverReport();
  181. [[nodiscard]] RTCP_ReportBlock *getReportBlock(int num);
  182. [[nodiscard]] const RTCP_ReportBlock *getReportBlock(int num) const;
  183. [[nodiscard]] size_t getSize() const;
  184. void preparePacket(SSRC senderSSRC, uint8_t reportCount);
  185. void setSenderSSRC(SSRC ssrc);
  186. void log() const;
  187. };
  188. struct RTC_CPP_EXPORT RTCP_REMB {
  189. RTCP_FB_HEADER header;
  190. char _id[4]; // Unique identifier ('R' 'E' 'M' 'B')
  191. uint32_t _bitrate; // Num SSRC, Br Exp, Br Mantissa (bit mask)
  192. SSRC _ssrc[1];
  193. [[nodiscard]] static size_t SizeWithSSRCs(int count);
  194. [[nodiscard]] unsigned int getSize() const;
  195. void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate);
  196. void setBitrate(unsigned int numSSRC, unsigned int in_bitrate);
  197. void setSsrc(int iterator, SSRC newSssrc);
  198. };
  199. struct RTC_CPP_EXPORT RTCP_PLI {
  200. RTCP_FB_HEADER header;
  201. [[nodiscard]] static unsigned int Size();
  202. void preparePacket(SSRC messageSSRC);
  203. void log() const;
  204. };
  205. struct RTC_CPP_EXPORT RTCP_FIR_PART {
  206. uint32_t ssrc;
  207. uint8_t seqNo;
  208. uint8_t dummy1;
  209. uint16_t dummy2;
  210. };
  211. struct RTC_CPP_EXPORT RTCP_FIR {
  212. RTCP_FB_HEADER header;
  213. RTCP_FIR_PART parts[1];
  214. static unsigned int Size();
  215. void preparePacket(SSRC messageSSRC, uint8_t seqNo);
  216. void log() const;
  217. };
  218. struct RTC_CPP_EXPORT RTCP_NACK_PART {
  219. uint16_t _pid;
  220. uint16_t _blp;
  221. uint16_t pid();
  222. uint16_t blp();
  223. void setPid(uint16_t pid);
  224. void setBlp(uint16_t blp);
  225. std::vector<uint16_t> getSequenceNumbers();
  226. };
  227. struct RTC_CPP_EXPORT RTCP_NACK {
  228. RTCP_FB_HEADER header;
  229. RTCP_NACK_PART parts[1];
  230. [[nodiscard]] static unsigned int Size(unsigned int discreteSeqNoCount);
  231. [[nodiscard]] unsigned int getSeqNoCount();
  232. void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount);
  233. /**
  234. * Add a packet to the list of missing packets.
  235. * @param fciCount The number of FCI fields that are present in this packet.
  236. * Let the number start at zero and let this function grow the number.
  237. * @param fciPID The seq no of the active FCI. It will be initialized automatically, and will
  238. * change automatically.
  239. * @param missingPacket The seq no of the missing packet. This will be added to the queue.
  240. * @return true if the packet has grown, false otherwise.
  241. */
  242. bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket);
  243. };
  244. struct RTC_CPP_EXPORT RTP_RTX {
  245. RTP header;
  246. [[nodiscard]] const char *getBody() const;
  247. [[nodiscard]] char *getBody();
  248. [[nodiscard]] size_t getBodySize(size_t totalSize) const;
  249. [[nodiscard]] size_t getSize() const;
  250. [[nodiscard]] uint16_t getOriginalSeqNo() const;
  251. // Returns the new size of the packet
  252. size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType);
  253. size_t copyTo(RTP *dest, size_t totalSize, uint8_t originalPayloadType);
  254. };
  255. #pragma pack(pop)
  256. }; // namespace rtc
  257. #endif