rtp_packet.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_
  12. #include <string>
  13. #include <vector>
  14. #include "absl/types/optional.h"
  15. #include "api/array_view.h"
  16. #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
  17. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  18. #include "rtc_base/copy_on_write_buffer.h"
  19. namespace webrtc {
  20. class RtpPacket {
  21. public:
  22. using ExtensionType = RTPExtensionType;
  23. using ExtensionManager = RtpHeaderExtensionMap;
  24. // |extensions| required for SetExtension/ReserveExtension functions during
  25. // packet creating and used if available in Parse function.
  26. // Adding and getting extensions will fail until |extensions| is
  27. // provided via constructor or IdentifyExtensions function.
  28. RtpPacket();
  29. explicit RtpPacket(const ExtensionManager* extensions);
  30. RtpPacket(const RtpPacket&);
  31. RtpPacket(const ExtensionManager* extensions, size_t capacity);
  32. ~RtpPacket();
  33. RtpPacket& operator=(const RtpPacket&) = default;
  34. // Parse and copy given buffer into Packet.
  35. // Does not require extension map to be registered (map is only required to
  36. // read or allocate extensions in methods GetExtension, AllocateExtension,
  37. // etc.)
  38. bool Parse(const uint8_t* buffer, size_t size);
  39. bool Parse(rtc::ArrayView<const uint8_t> packet);
  40. // Parse and move given buffer into Packet.
  41. bool Parse(rtc::CopyOnWriteBuffer packet);
  42. // Maps extensions id to their types.
  43. void IdentifyExtensions(const ExtensionManager& extensions);
  44. // Header.
  45. bool Marker() const { return marker_; }
  46. uint8_t PayloadType() const { return payload_type_; }
  47. uint16_t SequenceNumber() const { return sequence_number_; }
  48. uint32_t Timestamp() const { return timestamp_; }
  49. uint32_t Ssrc() const { return ssrc_; }
  50. std::vector<uint32_t> Csrcs() const;
  51. size_t headers_size() const { return payload_offset_; }
  52. // Payload.
  53. size_t payload_size() const { return payload_size_; }
  54. size_t padding_size() const { return padding_size_; }
  55. rtc::ArrayView<const uint8_t> payload() const {
  56. return rtc::MakeArrayView(data() + payload_offset_, payload_size_);
  57. }
  58. rtc::CopyOnWriteBuffer PayloadBuffer() const {
  59. return buffer_.Slice(payload_offset_, payload_size_);
  60. }
  61. // Buffer.
  62. rtc::CopyOnWriteBuffer Buffer() const { return buffer_; }
  63. size_t capacity() const { return buffer_.capacity(); }
  64. size_t size() const {
  65. return payload_offset_ + payload_size_ + padding_size_;
  66. }
  67. const uint8_t* data() const { return buffer_.cdata(); }
  68. size_t FreeCapacity() const { return capacity() - size(); }
  69. size_t MaxPayloadSize() const { return capacity() - headers_size(); }
  70. // Reset fields and buffer.
  71. void Clear();
  72. // Header setters.
  73. void CopyHeaderFrom(const RtpPacket& packet);
  74. void SetMarker(bool marker_bit);
  75. void SetPayloadType(uint8_t payload_type);
  76. void SetSequenceNumber(uint16_t seq_no);
  77. void SetTimestamp(uint32_t timestamp);
  78. void SetSsrc(uint32_t ssrc);
  79. // Fills with zeroes mutable extensions,
  80. // which are modified after FEC protection is generated.
  81. void ZeroMutableExtensions();
  82. // Removes extension of given |type|, returns false is extension was not
  83. // registered in packet's extension map or not present in the packet. Only
  84. // extension that should be removed must be registered, other extensions may
  85. // not be registered and will be preserved as is.
  86. bool RemoveExtension(ExtensionType type);
  87. // Writes csrc list. Assumes:
  88. // a) There is enough room left in buffer.
  89. // b) Extension headers, payload or padding data has not already been added.
  90. void SetCsrcs(rtc::ArrayView<const uint32_t> csrcs);
  91. // Header extensions.
  92. template <typename Extension>
  93. bool HasExtension() const;
  94. bool HasExtension(ExtensionType type) const;
  95. template <typename Extension, typename FirstValue, typename... Values>
  96. bool GetExtension(FirstValue, Values...) const;
  97. template <typename Extension>
  98. absl::optional<typename Extension::value_type> GetExtension() const;
  99. // Returns view of the raw extension or empty view on failure.
  100. template <typename Extension>
  101. rtc::ArrayView<const uint8_t> GetRawExtension() const;
  102. template <typename Extension, typename... Values>
  103. bool SetExtension(const Values&...);
  104. template <typename Extension>
  105. bool ReserveExtension();
  106. // Find or allocate an extension |type|. Returns view of size |length|
  107. // to write raw extension to or an empty view on failure.
  108. rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
  109. // Find an extension |type|.
  110. // Returns view of the raw extension or empty view on failure.
  111. rtc::ArrayView<const uint8_t> FindExtension(ExtensionType type) const;
  112. // Reserve size_bytes for payload. Returns nullptr on failure.
  113. uint8_t* SetPayloadSize(size_t size_bytes);
  114. // Same as SetPayloadSize but doesn't guarantee to keep current payload.
  115. uint8_t* AllocatePayload(size_t size_bytes);
  116. bool SetPadding(size_t padding_size);
  117. // Returns debug string of RTP packet (without detailed extension info).
  118. std::string ToString() const;
  119. private:
  120. struct ExtensionInfo {
  121. explicit ExtensionInfo(uint8_t id) : ExtensionInfo(id, 0, 0) {}
  122. ExtensionInfo(uint8_t id, uint8_t length, uint16_t offset)
  123. : id(id), length(length), offset(offset) {}
  124. uint8_t id;
  125. uint8_t length;
  126. uint16_t offset;
  127. };
  128. // Helper function for Parse. Fill header fields using data in given buffer,
  129. // but does not touch packet own buffer, leaving packet in invalid state.
  130. bool ParseBuffer(const uint8_t* buffer, size_t size);
  131. // Returns pointer to extension info for a given id. Returns nullptr if not
  132. // found.
  133. const ExtensionInfo* FindExtensionInfo(int id) const;
  134. // Returns reference to extension info for a given id. Creates a new entry
  135. // with the specified id if not found.
  136. ExtensionInfo& FindOrCreateExtensionInfo(int id);
  137. // Allocates and returns place to store rtp header extension.
  138. // Returns empty arrayview on failure.
  139. rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
  140. // Promotes existing one-byte header extensions to two-byte header extensions
  141. // by rewriting the data and updates the corresponding extension offsets.
  142. void PromoteToTwoByteHeaderExtension();
  143. uint16_t SetExtensionLengthMaybeAddZeroPadding(size_t extensions_offset);
  144. uint8_t* WriteAt(size_t offset) { return buffer_.data() + offset; }
  145. void WriteAt(size_t offset, uint8_t byte) { buffer_.data()[offset] = byte; }
  146. const uint8_t* ReadAt(size_t offset) const { return buffer_.data() + offset; }
  147. // Header.
  148. bool marker_;
  149. uint8_t payload_type_;
  150. uint8_t padding_size_;
  151. uint16_t sequence_number_;
  152. uint32_t timestamp_;
  153. uint32_t ssrc_;
  154. size_t payload_offset_; // Match header size with csrcs and extensions.
  155. size_t payload_size_;
  156. ExtensionManager extensions_;
  157. std::vector<ExtensionInfo> extension_entries_;
  158. size_t extensions_size_ = 0; // Unaligned.
  159. rtc::CopyOnWriteBuffer buffer_;
  160. };
  161. template <typename Extension>
  162. bool RtpPacket::HasExtension() const {
  163. return HasExtension(Extension::kId);
  164. }
  165. template <typename Extension, typename FirstValue, typename... Values>
  166. bool RtpPacket::GetExtension(FirstValue first, Values... values) const {
  167. auto raw = FindExtension(Extension::kId);
  168. if (raw.empty())
  169. return false;
  170. return Extension::Parse(raw, first, values...);
  171. }
  172. template <typename Extension>
  173. absl::optional<typename Extension::value_type> RtpPacket::GetExtension() const {
  174. absl::optional<typename Extension::value_type> result;
  175. auto raw = FindExtension(Extension::kId);
  176. if (raw.empty() || !Extension::Parse(raw, &result.emplace()))
  177. result = absl::nullopt;
  178. return result;
  179. }
  180. template <typename Extension>
  181. rtc::ArrayView<const uint8_t> RtpPacket::GetRawExtension() const {
  182. return FindExtension(Extension::kId);
  183. }
  184. template <typename Extension, typename... Values>
  185. bool RtpPacket::SetExtension(const Values&... values) {
  186. const size_t value_size = Extension::ValueSize(values...);
  187. auto buffer = AllocateExtension(Extension::kId, value_size);
  188. if (buffer.empty())
  189. return false;
  190. return Extension::Write(buffer, values...);
  191. }
  192. template <typename Extension>
  193. bool RtpPacket::ReserveExtension() {
  194. auto buffer = AllocateExtension(Extension::kId, Extension::kValueSizeBytes);
  195. if (buffer.empty())
  196. return false;
  197. memset(buffer.data(), 0, Extension::kValueSizeBytes);
  198. return true;
  199. }
  200. } // namespace webrtc
  201. #endif // MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_