rtp_headers.h 6.9 KB

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