rtp.hpp 11 KB

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