rtp.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. * Copyright (c) 2020 Staz Modrzynski
  3. * Copyright (c) 2020 Paul-Louis Ageneau
  4. * Copyright (c) 2020 Filip Klembara (in2core)
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  9. */
  10. #ifndef RTC_RTP_HPP
  11. #define RTC_RTP_HPP
  12. #include "common.hpp"
  13. #include <vector>
  14. namespace rtc {
  15. typedef uint32_t SSRC;
  16. RTC_CPP_EXPORT bool IsRtcp(const binary &data);
  17. #pragma pack(push, 1)
  18. struct RTC_CPP_EXPORT RtpExtensionHeader {
  19. uint16_t _profileSpecificId;
  20. uint16_t _headerLength;
  21. [[nodiscard]] uint16_t profileSpecificId() const;
  22. [[nodiscard]] uint16_t headerLength() const;
  23. [[nodiscard]] size_t getSize() const;
  24. [[nodiscard]] const char *getBody() const;
  25. [[nodiscard]] char *getBody();
  26. void setProfileSpecificId(uint16_t profileSpecificId);
  27. void setHeaderLength(uint16_t headerLength);
  28. void clearBody();
  29. void writeCurrentVideoOrientation(size_t offset, uint8_t id, uint8_t value);
  30. void writeOneByteHeader(size_t offset, uint8_t id, const byte *value, size_t size);
  31. };
  32. struct RTC_CPP_EXPORT RtpHeader {
  33. uint8_t _first;
  34. uint8_t _payloadType;
  35. uint16_t _seqNumber;
  36. uint32_t _timestamp;
  37. SSRC _ssrc;
  38. // The following field is SSRC _csrc[]
  39. [[nodiscard]] uint8_t version() const;
  40. [[nodiscard]] bool padding() const;
  41. [[nodiscard]] bool extension() const;
  42. [[nodiscard]] uint8_t csrcCount() const;
  43. [[nodiscard]] uint8_t marker() const;
  44. [[nodiscard]] uint8_t payloadType() const;
  45. [[nodiscard]] uint16_t seqNumber() const;
  46. [[nodiscard]] uint32_t timestamp() const;
  47. [[nodiscard]] uint32_t ssrc() const;
  48. [[nodiscard]] size_t getSize() const;
  49. [[nodiscard]] size_t getExtensionHeaderSize() const;
  50. [[nodiscard]] const RtpExtensionHeader *getExtensionHeader() const;
  51. [[nodiscard]] RtpExtensionHeader *getExtensionHeader();
  52. [[nodiscard]] const char *getBody() const;
  53. [[nodiscard]] char *getBody();
  54. void log() const;
  55. void preparePacket();
  56. void setSeqNumber(uint16_t newSeqNo);
  57. void setPayloadType(uint8_t newPayloadType);
  58. void setSsrc(uint32_t in_ssrc);
  59. void setMarker(bool marker);
  60. void setTimestamp(uint32_t i);
  61. void setExtension(bool extension);
  62. };
  63. struct RTC_CPP_EXPORT RtcpReportBlock {
  64. SSRC _ssrc;
  65. uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
  66. uint16_t _seqNoCycles;
  67. uint16_t _highestSeqNo;
  68. uint32_t _jitter;
  69. uint32_t _lastReport;
  70. uint32_t _delaySinceLastReport;
  71. [[nodiscard]] uint16_t seqNoCycles() const;
  72. [[nodiscard]] uint16_t highestSeqNo() const;
  73. [[nodiscard]] uint32_t extendedHighestSeqNo() const;
  74. [[nodiscard]] uint32_t jitter() const;
  75. [[nodiscard]] uint32_t delaySinceSR() const;
  76. [[nodiscard]] SSRC getSSRC() const;
  77. [[nodiscard]] uint32_t getNTPOfSR() const;
  78. [[nodiscard]] uint8_t getFractionLost() const;
  79. [[nodiscard]] unsigned int getPacketsLostCount() const;
  80. void preparePacket(SSRC in_ssrc, unsigned int packetsLost, unsigned int totalPackets,
  81. uint16_t highestSeqNo, uint16_t seqNoCycles, uint32_t jitter,
  82. uint64_t lastSR_NTP, uint64_t lastSR_DELAY);
  83. void setSSRC(SSRC in_ssrc);
  84. void setPacketsLost(uint8_t fractionLost, unsigned int packetsLostCount);
  85. void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles);
  86. void setJitter(uint32_t jitter);
  87. void setNTPOfSR(uint64_t ntp);
  88. void setDelaySinceSR(uint32_t sr);
  89. void log() const;
  90. };
  91. struct RTC_CPP_EXPORT RtcpHeader {
  92. uint8_t _first;
  93. uint8_t _payloadType;
  94. uint16_t _length;
  95. [[nodiscard]] uint8_t version() const;
  96. [[nodiscard]] bool padding() const;
  97. [[nodiscard]] uint8_t reportCount() const;
  98. [[nodiscard]] uint8_t payloadType() const;
  99. [[nodiscard]] uint16_t length() const;
  100. [[nodiscard]] size_t lengthInBytes() const;
  101. void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length);
  102. void setPayloadType(uint8_t type);
  103. void setReportCount(uint8_t count);
  104. void setLength(uint16_t length);
  105. void log() const;
  106. };
  107. struct RTC_CPP_EXPORT RtcpFbHeader {
  108. RtcpHeader header;
  109. SSRC _packetSender;
  110. SSRC _mediaSource;
  111. [[nodiscard]] SSRC packetSenderSSRC() const;
  112. [[nodiscard]] SSRC mediaSourceSSRC() const;
  113. void setPacketSenderSSRC(SSRC ssrc);
  114. void setMediaSourceSSRC(SSRC ssrc);
  115. void log() const;
  116. };
  117. struct RTC_CPP_EXPORT RtcpSr {
  118. RtcpHeader header;
  119. SSRC _senderSSRC;
  120. uint64_t _ntpTimestamp;
  121. uint32_t _rtpTimestamp;
  122. uint32_t _packetCount;
  123. uint32_t _octetCount;
  124. RtcpReportBlock _reportBlocks;
  125. [[nodiscard]] static unsigned int Size(unsigned int reportCount);
  126. [[nodiscard]] uint64_t ntpTimestamp() const;
  127. [[nodiscard]] uint32_t rtpTimestamp() const;
  128. [[nodiscard]] uint32_t packetCount() const;
  129. [[nodiscard]] uint32_t octetCount() const;
  130. [[nodiscard]] uint32_t senderSSRC() const;
  131. [[nodiscard]] const RtcpReportBlock *getReportBlock(int num) const;
  132. [[nodiscard]] RtcpReportBlock *getReportBlock(int num);
  133. [[nodiscard]] unsigned int size(unsigned int reportCount);
  134. [[nodiscard]] size_t getSize() const;
  135. void preparePacket(SSRC senderSSRC, uint8_t reportCount);
  136. void setNtpTimestamp(uint64_t ts);
  137. void setRtpTimestamp(uint32_t ts);
  138. void setOctetCount(uint32_t ts);
  139. void setPacketCount(uint32_t ts);
  140. void log() const;
  141. };
  142. struct RTC_CPP_EXPORT RtcpSdesItem {
  143. uint8_t type;
  144. uint8_t _length;
  145. char _text[1];
  146. [[nodiscard]] static unsigned int Size(uint8_t textLength);
  147. [[nodiscard]] string text() const;
  148. [[nodiscard]] uint8_t length() const;
  149. void setText(string text);
  150. };
  151. struct RTC_CPP_EXPORT RtcpSdesChunk {
  152. SSRC _ssrc;
  153. RtcpSdesItem _items;
  154. [[nodiscard]] static unsigned int Size(const std::vector<uint8_t> textLengths);
  155. [[nodiscard]] SSRC ssrc() const;
  156. void setSSRC(SSRC ssrc);
  157. // Get item at given index
  158. // All items with index < num must be valid, otherwise this function has undefined behaviour
  159. // (use safelyCountChunkSize() to check if chunk is valid).
  160. [[nodiscard]] const RtcpSdesItem *getItem(int num) const;
  161. [[nodiscard]] RtcpSdesItem *getItem(int num);
  162. // Get size of chunk
  163. // All items must be valid, otherwise this function has undefined behaviour (use
  164. // safelyCountChunkSize() to check if chunk is valid)
  165. [[nodiscard]] unsigned int getSize() const;
  166. long safelyCountChunkSize(size_t maxChunkSize) const;
  167. };
  168. struct RTC_CPP_EXPORT RtcpSdes {
  169. RtcpHeader header;
  170. RtcpSdesChunk _chunks;
  171. [[nodiscard]] static unsigned int Size(const std::vector<std::vector<uint8_t>> lengths);
  172. bool isValid() const;
  173. // Returns number of chunks in this packet
  174. // Returns 0 if packet is invalid
  175. unsigned int chunksCount() const;
  176. // Get chunk at given index
  177. // All chunks (and their items) with index < `num` must be valid, otherwise this function has
  178. // undefined behaviour (use `isValid` to check if chunk is valid).
  179. const RtcpSdesChunk *getChunk(int num) const;
  180. RtcpSdesChunk *getChunk(int num);
  181. void preparePacket(uint8_t chunkCount);
  182. };
  183. struct RTC_CPP_EXPORT RtcpRr {
  184. RtcpHeader header;
  185. SSRC _senderSSRC;
  186. RtcpReportBlock _reportBlocks;
  187. [[nodiscard]] static size_t SizeWithReportBlocks(uint8_t reportCount);
  188. SSRC senderSSRC() const;
  189. bool isSenderReport();
  190. bool isReceiverReport();
  191. [[nodiscard]] RtcpReportBlock *getReportBlock(int num);
  192. [[nodiscard]] const RtcpReportBlock *getReportBlock(int num) const;
  193. [[nodiscard]] size_t getSize() const;
  194. void preparePacket(SSRC senderSSRC, uint8_t reportCount);
  195. void setSenderSSRC(SSRC ssrc);
  196. void log() const;
  197. };
  198. struct RTC_CPP_EXPORT RtcpRemb {
  199. RtcpFbHeader header;
  200. char _id[4]; // Unique identifier ('R' 'E' 'M' 'B')
  201. uint32_t _bitrate; // Num SSRC, Br Exp, Br Mantissa (bit mask)
  202. SSRC _ssrc[1];
  203. [[nodiscard]] static size_t SizeWithSSRCs(int count);
  204. [[nodiscard]] unsigned int getSize() const;
  205. void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate);
  206. void setBitrate(unsigned int numSSRC, unsigned int in_bitrate);
  207. void setSsrc(int iterator, SSRC newSssrc);
  208. unsigned int getNumSSRC();
  209. unsigned int getBitrate();
  210. };
  211. struct RTC_CPP_EXPORT RtcpPli {
  212. RtcpFbHeader header;
  213. [[nodiscard]] static unsigned int Size();
  214. void preparePacket(SSRC messageSSRC);
  215. void log() const;
  216. };
  217. struct RTC_CPP_EXPORT RtcpFirPart {
  218. uint32_t ssrc;
  219. uint8_t seqNo;
  220. uint8_t dummy1;
  221. uint16_t dummy2;
  222. };
  223. struct RTC_CPP_EXPORT RtcpFir {
  224. RtcpFbHeader header;
  225. RtcpFirPart parts[1];
  226. static unsigned int Size();
  227. void preparePacket(SSRC messageSSRC, uint8_t seqNo);
  228. void log() const;
  229. };
  230. struct RTC_CPP_EXPORT RtcpNackPart {
  231. uint16_t _pid;
  232. uint16_t _blp;
  233. uint16_t pid();
  234. uint16_t blp();
  235. void setPid(uint16_t pid);
  236. void setBlp(uint16_t blp);
  237. std::vector<uint16_t> getSequenceNumbers();
  238. };
  239. struct RTC_CPP_EXPORT RtcpNack {
  240. RtcpFbHeader header;
  241. RtcpNackPart parts[1];
  242. [[nodiscard]] static unsigned int Size(unsigned int discreteSeqNoCount);
  243. [[nodiscard]] unsigned int getSeqNoCount();
  244. void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount);
  245. /**
  246. * Add a packet to the list of missing packets.
  247. * @param fciCount The number of FCI fields that are present in this packet.
  248. * Let the number start at zero and let this function grow the number.
  249. * @param fciPID The seq no of the active FCI. It will be initialized automatically, and will
  250. * change automatically.
  251. * @param missingPacket The seq no of the missing packet. This will be added to the queue.
  252. * @return true if the packet has grown, false otherwise.
  253. */
  254. bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket);
  255. };
  256. struct RTC_CPP_EXPORT RtpRtx {
  257. RtpHeader header;
  258. [[nodiscard]] const char *getBody() const;
  259. [[nodiscard]] char *getBody();
  260. [[nodiscard]] size_t getBodySize(size_t totalSize) const;
  261. [[nodiscard]] size_t getSize() const;
  262. [[nodiscard]] uint16_t getOriginalSeqNo() const;
  263. // Returns the new size of the packet
  264. size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType);
  265. size_t copyTo(RtpHeader *dest, size_t totalSize, uint8_t originalPayloadType);
  266. };
  267. #pragma pack(pop)
  268. } // namespace rtc
  269. #endif