rtp.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 jitter() const;
  74. [[nodiscard]] uint32_t delaySinceSR() const;
  75. [[nodiscard]] SSRC getSSRC() const;
  76. [[nodiscard]] uint32_t getNTPOfSR() const;
  77. [[nodiscard]] unsigned int getLossPercentage() const;
  78. [[nodiscard]] unsigned int getPacketLostCount() const;
  79. void preparePacket(SSRC in_ssrc, unsigned int packetsLost, unsigned int totalPackets,
  80. uint16_t highestSeqNo, uint16_t seqNoCycles, uint32_t jitter,
  81. uint64_t lastSR_NTP, uint64_t lastSR_DELAY);
  82. void setSSRC(SSRC in_ssrc);
  83. void setPacketsLost(unsigned int packetsLost, unsigned int totalPackets);
  84. void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles);
  85. void setJitter(uint32_t jitter);
  86. void setNTPOfSR(uint64_t ntp);
  87. void setDelaySinceSR(uint32_t sr);
  88. void log() const;
  89. };
  90. struct RTC_CPP_EXPORT RtcpHeader {
  91. uint8_t _first;
  92. uint8_t _payloadType;
  93. uint16_t _length;
  94. [[nodiscard]] uint8_t version() const;
  95. [[nodiscard]] bool padding() const;
  96. [[nodiscard]] uint8_t reportCount() const;
  97. [[nodiscard]] uint8_t payloadType() const;
  98. [[nodiscard]] uint16_t length() const;
  99. [[nodiscard]] size_t lengthInBytes() const;
  100. void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length);
  101. void setPayloadType(uint8_t type);
  102. void setReportCount(uint8_t count);
  103. void setLength(uint16_t length);
  104. void log() const;
  105. };
  106. struct RTC_CPP_EXPORT RtcpFbHeader {
  107. RtcpHeader header;
  108. SSRC _packetSender;
  109. SSRC _mediaSource;
  110. [[nodiscard]] SSRC packetSenderSSRC() const;
  111. [[nodiscard]] SSRC mediaSourceSSRC() const;
  112. void setPacketSenderSSRC(SSRC ssrc);
  113. void setMediaSourceSSRC(SSRC ssrc);
  114. void log() const;
  115. };
  116. struct RTC_CPP_EXPORT RtcpSr {
  117. RtcpHeader header;
  118. SSRC _senderSSRC;
  119. uint64_t _ntpTimestamp;
  120. uint32_t _rtpTimestamp;
  121. uint32_t _packetCount;
  122. uint32_t _octetCount;
  123. RtcpReportBlock _reportBlocks;
  124. [[nodiscard]] static unsigned int Size(unsigned int reportCount);
  125. [[nodiscard]] uint64_t ntpTimestamp() const;
  126. [[nodiscard]] uint32_t rtpTimestamp() const;
  127. [[nodiscard]] uint32_t packetCount() const;
  128. [[nodiscard]] uint32_t octetCount() const;
  129. [[nodiscard]] uint32_t senderSSRC() const;
  130. [[nodiscard]] const RtcpReportBlock *getReportBlock(int num) const;
  131. [[nodiscard]] RtcpReportBlock *getReportBlock(int num);
  132. [[nodiscard]] unsigned int size(unsigned int reportCount);
  133. [[nodiscard]] size_t getSize() const;
  134. void preparePacket(SSRC senderSSRC, uint8_t reportCount);
  135. void setNtpTimestamp(uint64_t ts);
  136. void setRtpTimestamp(uint32_t ts);
  137. void setOctetCount(uint32_t ts);
  138. void setPacketCount(uint32_t ts);
  139. void log() const;
  140. };
  141. struct RTC_CPP_EXPORT RtcpSdesItem {
  142. uint8_t type;
  143. uint8_t _length;
  144. char _text[1];
  145. [[nodiscard]] static unsigned int Size(uint8_t textLength);
  146. [[nodiscard]] string text() const;
  147. [[nodiscard]] uint8_t length() const;
  148. void setText(string text);
  149. };
  150. struct RTC_CPP_EXPORT RtcpSdesChunk {
  151. SSRC _ssrc;
  152. RtcpSdesItem _items;
  153. [[nodiscard]] static unsigned int Size(const std::vector<uint8_t> textLengths);
  154. [[nodiscard]] SSRC ssrc() const;
  155. void setSSRC(SSRC ssrc);
  156. // Get item at given index
  157. // All items with index < num must be valid, otherwise this function has undefined behaviour
  158. // (use safelyCountChunkSize() to check if chunk is valid).
  159. [[nodiscard]] const RtcpSdesItem *getItem(int num) const;
  160. [[nodiscard]] RtcpSdesItem *getItem(int num);
  161. // Get size of chunk
  162. // All items must be valid, otherwise this function has undefined behaviour (use
  163. // safelyCountChunkSize() to check if chunk is valid)
  164. [[nodiscard]] unsigned int getSize() const;
  165. long safelyCountChunkSize(size_t maxChunkSize) const;
  166. };
  167. struct RTC_CPP_EXPORT RtcpSdes {
  168. RtcpHeader header;
  169. RtcpSdesChunk _chunks;
  170. [[nodiscard]] static unsigned int Size(const std::vector<std::vector<uint8_t>> lengths);
  171. bool isValid() const;
  172. // Returns number of chunks in this packet
  173. // Returns 0 if packet is invalid
  174. unsigned int chunksCount() const;
  175. // Get chunk at given index
  176. // All chunks (and their items) with index < `num` must be valid, otherwise this function has
  177. // undefined behaviour (use `isValid` to check if chunk is valid).
  178. const RtcpSdesChunk *getChunk(int num) const;
  179. RtcpSdesChunk *getChunk(int num);
  180. void preparePacket(uint8_t chunkCount);
  181. };
  182. struct RTC_CPP_EXPORT RtcpRr {
  183. RtcpHeader header;
  184. SSRC _senderSSRC;
  185. RtcpReportBlock _reportBlocks;
  186. [[nodiscard]] static size_t SizeWithReportBlocks(uint8_t reportCount);
  187. SSRC senderSSRC() const;
  188. bool isSenderReport();
  189. bool isReceiverReport();
  190. [[nodiscard]] RtcpReportBlock *getReportBlock(int num);
  191. [[nodiscard]] const RtcpReportBlock *getReportBlock(int num) const;
  192. [[nodiscard]] size_t getSize() const;
  193. void preparePacket(SSRC senderSSRC, uint8_t reportCount);
  194. void setSenderSSRC(SSRC ssrc);
  195. void log() const;
  196. };
  197. struct RTC_CPP_EXPORT RtcpRemb {
  198. RtcpFbHeader header;
  199. char _id[4]; // Unique identifier ('R' 'E' 'M' 'B')
  200. uint32_t _bitrate; // Num SSRC, Br Exp, Br Mantissa (bit mask)
  201. SSRC _ssrc[1];
  202. [[nodiscard]] static size_t SizeWithSSRCs(int count);
  203. [[nodiscard]] unsigned int getSize() const;
  204. void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate);
  205. void setBitrate(unsigned int numSSRC, unsigned int in_bitrate);
  206. void setSsrc(int iterator, SSRC newSssrc);
  207. };
  208. struct RTC_CPP_EXPORT RtcpPli {
  209. RtcpFbHeader header;
  210. [[nodiscard]] static unsigned int Size();
  211. void preparePacket(SSRC messageSSRC);
  212. void log() const;
  213. };
  214. struct RTC_CPP_EXPORT RtcpFirPart {
  215. uint32_t ssrc;
  216. uint8_t seqNo;
  217. uint8_t dummy1;
  218. uint16_t dummy2;
  219. };
  220. struct RTC_CPP_EXPORT RtcpFir {
  221. RtcpFbHeader header;
  222. RtcpFirPart parts[1];
  223. static unsigned int Size();
  224. void preparePacket(SSRC messageSSRC, uint8_t seqNo);
  225. void log() const;
  226. };
  227. struct RTC_CPP_EXPORT RtcpNackPart {
  228. uint16_t _pid;
  229. uint16_t _blp;
  230. uint16_t pid();
  231. uint16_t blp();
  232. void setPid(uint16_t pid);
  233. void setBlp(uint16_t blp);
  234. std::vector<uint16_t> getSequenceNumbers();
  235. };
  236. struct RTC_CPP_EXPORT RtcpNack {
  237. RtcpFbHeader header;
  238. RtcpNackPart parts[1];
  239. [[nodiscard]] static unsigned int Size(unsigned int discreteSeqNoCount);
  240. [[nodiscard]] unsigned int getSeqNoCount();
  241. void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount);
  242. /**
  243. * Add a packet to the list of missing packets.
  244. * @param fciCount The number of FCI fields that are present in this packet.
  245. * Let the number start at zero and let this function grow the number.
  246. * @param fciPID The seq no of the active FCI. It will be initialized automatically, and will
  247. * change automatically.
  248. * @param missingPacket The seq no of the missing packet. This will be added to the queue.
  249. * @return true if the packet has grown, false otherwise.
  250. */
  251. bool addMissingPacket(unsigned int *fciCount, uint16_t *fciPID, uint16_t missingPacket);
  252. };
  253. struct RTC_CPP_EXPORT RtpRtx {
  254. RtpHeader header;
  255. [[nodiscard]] const char *getBody() const;
  256. [[nodiscard]] char *getBody();
  257. [[nodiscard]] size_t getBodySize(size_t totalSize) const;
  258. [[nodiscard]] size_t getSize() const;
  259. [[nodiscard]] uint16_t getOriginalSeqNo() const;
  260. // Returns the new size of the packet
  261. size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType);
  262. size_t copyTo(RtpHeader *dest, size_t totalSize, uint8_t originalPayloadType);
  263. };
  264. // For backward compatibility, do not use
  265. using RTP_ExtensionHeader = RtpExtensionHeader;
  266. using RTP = RtpHeader;
  267. using RTCP_ReportBlock = RtcpReportBlock;
  268. using RTCP_HEADER = RtcpHeader;
  269. using RTCP_FB_HEADER = RtcpFbHeader;
  270. using RTCP_SR = RtcpSr;
  271. using RTCP_SDES_ITEM = RtcpSdesItem;
  272. using RTCP_SDES_CHUNK = RtcpSdesChunk;
  273. using RTCP_SDES = RtcpSdes;
  274. using RTCP_RR = RtcpRr;
  275. using RTCP_REMB = RtcpRemb;
  276. using RTCP_PLI = RtcpPli;
  277. using RTCP_FIR_PART = RtcpFirPart;
  278. using RTCP_FIR = RtcpFir;
  279. using RTCP_NACK_PART = RtcpNackPart;
  280. using RTCP_NACK = RtcpNack;
  281. using RTP_RTX = RtpRtx;
  282. #pragma pack(pop)
  283. } // namespace rtc
  284. #endif