rtp_headers.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2017 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 API_RTP_HEADERS_H_
  11. #define API_RTP_HEADERS_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <string>
  15. #include "absl/types/optional.h"
  16. #include "api/array_view.h"
  17. #include "api/units/timestamp.h"
  18. #include "api/video/color_space.h"
  19. #include "api/video/video_content_type.h"
  20. #include "api/video/video_rotation.h"
  21. #include "api/video/video_timing.h"
  22. #include "common_types.h" // NOLINT (build/include)
  23. namespace webrtc {
  24. struct FeedbackRequest {
  25. // Determines whether the recv delta as specified in
  26. // https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01
  27. // should be included.
  28. bool include_timestamps;
  29. // Include feedback of received packets in the range [sequence_number -
  30. // sequence_count + 1, sequence_number]. That is, no feedback will be sent if
  31. // sequence_count is zero.
  32. int sequence_count;
  33. };
  34. // The Absolute Capture Time extension is used to stamp RTP packets with a NTP
  35. // timestamp showing when the first audio or video frame in a packet was
  36. // originally captured. The intent of this extension is to provide a way to
  37. // accomplish audio-to-video synchronization when RTCP-terminating intermediate
  38. // systems (e.g. mixers) are involved. See:
  39. // http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time
  40. struct AbsoluteCaptureTime {
  41. // Absolute capture timestamp is the NTP timestamp of when the first frame in
  42. // a packet was originally captured. This timestamp MUST be based on the same
  43. // clock as the clock used to generate NTP timestamps for RTCP sender reports
  44. // on the capture system.
  45. //
  46. // It’s not always possible to do an NTP clock readout at the exact moment of
  47. // when a media frame is captured. A capture system MAY postpone the readout
  48. // until a more convenient time. A capture system SHOULD have known delays
  49. // (e.g. from hardware buffers) subtracted from the readout to make the final
  50. // timestamp as close to the actual capture time as possible.
  51. //
  52. // This field is encoded as a 64-bit unsigned fixed-point number with the high
  53. // 32 bits for the timestamp in seconds and low 32 bits for the fractional
  54. // part. This is also known as the UQ32.32 format and is what the RTP
  55. // specification defines as the canonical format to represent NTP timestamps.
  56. uint64_t absolute_capture_timestamp;
  57. // Estimated capture clock offset is the sender’s estimate of the offset
  58. // between its own NTP clock and the capture system’s NTP clock. The sender is
  59. // here defined as the system that owns the NTP clock used to generate the NTP
  60. // timestamps for the RTCP sender reports on this stream. The sender system is
  61. // typically either the capture system or a mixer.
  62. //
  63. // This field is encoded as a 64-bit two’s complement signed fixed-point
  64. // number with the high 32 bits for the seconds and low 32 bits for the
  65. // fractional part. It’s intended to make it easy for a receiver, that knows
  66. // how to estimate the sender system’s NTP clock, to also estimate the capture
  67. // system’s NTP clock:
  68. //
  69. // Capture NTP Clock = Sender NTP Clock + Capture Clock Offset
  70. absl::optional<int64_t> estimated_capture_clock_offset;
  71. };
  72. inline bool operator==(const AbsoluteCaptureTime& lhs,
  73. const AbsoluteCaptureTime& rhs) {
  74. return (lhs.absolute_capture_timestamp == rhs.absolute_capture_timestamp) &&
  75. (lhs.estimated_capture_clock_offset ==
  76. rhs.estimated_capture_clock_offset);
  77. }
  78. inline bool operator!=(const AbsoluteCaptureTime& lhs,
  79. const AbsoluteCaptureTime& rhs) {
  80. return !(lhs == rhs);
  81. }
  82. struct RTPHeaderExtension {
  83. RTPHeaderExtension();
  84. RTPHeaderExtension(const RTPHeaderExtension& other);
  85. RTPHeaderExtension& operator=(const RTPHeaderExtension& other);
  86. static constexpr int kAbsSendTimeFraction = 18;
  87. Timestamp GetAbsoluteSendTimestamp() const {
  88. RTC_DCHECK(hasAbsoluteSendTime);
  89. RTC_DCHECK(absoluteSendTime < (1ul << 24));
  90. return Timestamp::Micros((absoluteSendTime * 1000000ll) /
  91. (1 << kAbsSendTimeFraction));
  92. }
  93. TimeDelta GetAbsoluteSendTimeDelta(uint32_t previous_sendtime) const {
  94. RTC_DCHECK(hasAbsoluteSendTime);
  95. RTC_DCHECK(absoluteSendTime < (1ul << 24));
  96. RTC_DCHECK(previous_sendtime < (1ul << 24));
  97. int32_t delta =
  98. static_cast<int32_t>((absoluteSendTime - previous_sendtime) << 8) >> 8;
  99. return TimeDelta::Micros((delta * 1000000ll) / (1 << kAbsSendTimeFraction));
  100. }
  101. bool hasTransmissionTimeOffset;
  102. int32_t transmissionTimeOffset;
  103. bool hasAbsoluteSendTime;
  104. uint32_t absoluteSendTime;
  105. absl::optional<AbsoluteCaptureTime> absolute_capture_time;
  106. bool hasTransportSequenceNumber;
  107. uint16_t transportSequenceNumber;
  108. absl::optional<FeedbackRequest> feedback_request;
  109. // Audio Level includes both level in dBov and voiced/unvoiced bit. See:
  110. // https://tools.ietf.org/html/rfc6464#section-3
  111. bool hasAudioLevel;
  112. bool voiceActivity;
  113. uint8_t audioLevel;
  114. // For Coordination of Video Orientation. See
  115. // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
  116. // ts_126114v120700p.pdf
  117. bool hasVideoRotation;
  118. VideoRotation videoRotation;
  119. // TODO(ilnik): Refactor this and one above to be absl::optional() and remove
  120. // a corresponding bool flag.
  121. bool hasVideoContentType;
  122. VideoContentType videoContentType;
  123. bool has_video_timing;
  124. VideoSendTiming video_timing;
  125. PlayoutDelay playout_delay = {-1, -1};
  126. // For identification of a stream when ssrc is not signaled. See
  127. // https://tools.ietf.org/html/draft-ietf-avtext-rid-09
  128. // TODO(danilchap): Update url from draft to release version.
  129. std::string stream_id;
  130. std::string repaired_stream_id;
  131. // For identifying the media section used to interpret this RTP packet. See
  132. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-38
  133. std::string mid;
  134. absl::optional<ColorSpace> color_space;
  135. };
  136. enum { kRtpCsrcSize = 15 }; // RFC 3550 page 13
  137. struct RTPHeader {
  138. RTPHeader();
  139. RTPHeader(const RTPHeader& other);
  140. RTPHeader& operator=(const RTPHeader& other);
  141. bool markerBit;
  142. uint8_t payloadType;
  143. uint16_t sequenceNumber;
  144. uint32_t timestamp;
  145. uint32_t ssrc;
  146. uint8_t numCSRCs;
  147. uint32_t arrOfCSRCs[kRtpCsrcSize];
  148. size_t paddingLength;
  149. size_t headerLength;
  150. int payload_type_frequency;
  151. RTPHeaderExtension extension;
  152. };
  153. // RTCP mode to use. Compound mode is described by RFC 4585 and reduced-size
  154. // RTCP mode is described by RFC 5506.
  155. enum class RtcpMode { kOff, kCompound, kReducedSize };
  156. enum NetworkState {
  157. kNetworkUp,
  158. kNetworkDown,
  159. };
  160. } // namespace webrtc
  161. #endif // API_RTP_HEADERS_H_