rtp.hpp 11 KB

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