rtp.hpp 10 KB

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