rtp_headers.h (7888B)
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 11 #ifndef API_RTP_HEADERS_H_ 12 #define API_RTP_HEADERS_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <optional> 18 #include <string> 19 20 #include "api/units/timestamp.h" 21 #include "api/video/color_space.h" 22 #include "api/video/video_content_type.h" 23 #include "api/video/video_rotation.h" 24 #include "api/video/video_timing.h" 25 #include "rtc_base/checks.h" 26 #include "rtc_base/system/rtc_export.h" 27 28 namespace webrtc { 29 30 struct FeedbackRequest { 31 // Determines whether the recv delta as specified in 32 // https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01 33 // should be included. 34 bool include_timestamps; 35 // Include feedback of received packets in the range [sequence_number - 36 // sequence_count + 1, sequence_number]. That is, no feedback will be sent if 37 // sequence_count is zero. 38 int sequence_count; 39 }; 40 41 // The Absolute Capture Time extension is used to stamp RTP packets with a NTP 42 // timestamp showing when the first audio or video frame in a packet was 43 // originally captured. The intent of this extension is to provide a way to 44 // accomplish audio-to-video synchronization when RTCP-terminating intermediate 45 // systems (e.g. mixers) are involved. See: 46 // http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time 47 struct AbsoluteCaptureTime { 48 // Absolute capture timestamp is the NTP timestamp of when the first frame in 49 // a packet was originally captured. This timestamp MUST be based on the same 50 // clock as the clock used to generate NTP timestamps for RTCP sender reports 51 // on the capture system. 52 // 53 // It’s not always possible to do an NTP clock readout at the exact moment of 54 // when a media frame is captured. A capture system MAY postpone the readout 55 // until a more convenient time. A capture system SHOULD have known delays 56 // (e.g. from hardware buffers) subtracted from the readout to make the final 57 // timestamp as close to the actual capture time as possible. 58 // 59 // This field is encoded as a 64-bit unsigned fixed-point number with the high 60 // 32 bits for the timestamp in seconds and low 32 bits for the fractional 61 // part. This is also known as the UQ32.32 format and is what the RTP 62 // specification defines as the canonical format to represent NTP timestamps. 63 uint64_t absolute_capture_timestamp; 64 65 // Estimated capture clock offset is the sender’s estimate of the offset 66 // between its own NTP clock and the capture system’s NTP clock. The sender is 67 // here defined as the system that owns the NTP clock used to generate the NTP 68 // timestamps for the RTCP sender reports on this stream. The sender system is 69 // typically either the capture system or a mixer. 70 // 71 // This field is encoded as a 64-bit two’s complement signed fixed-point 72 // number with the high 32 bits for the seconds and low 32 bits for the 73 // fractional part. It’s intended to make it easy for a receiver, that knows 74 // how to estimate the sender system’s NTP clock, to also estimate the capture 75 // system’s NTP clock: 76 // 77 // Capture NTP Clock = Sender NTP Clock + Capture Clock Offset 78 std::optional<int64_t> estimated_capture_clock_offset; 79 }; 80 81 // The audio level extension is used to indicate the voice activity and the 82 // audio level of the payload in the RTP stream. See: 83 // https://tools.ietf.org/html/rfc6464#section-3. 84 class AudioLevel { 85 public: 86 AudioLevel(); 87 AudioLevel(bool voice_activity, int audio_level); 88 AudioLevel(const AudioLevel& other) = default; 89 AudioLevel& operator=(const AudioLevel& other) = default; 90 91 // Flag indicating whether the encoder believes the audio packet contains 92 // voice activity. 93 bool voice_activity() const { return voice_activity_; } 94 95 // Audio level in -dBov. Values range from 0 to 127, representing 0 to -127 96 // dBov. 127 represents digital silence. 97 int level() const { return audio_level_; } 98 99 private: 100 bool voice_activity_; 101 int audio_level_; 102 }; 103 104 inline bool operator==(const AbsoluteCaptureTime& lhs, 105 const AbsoluteCaptureTime& rhs) { 106 return (lhs.absolute_capture_timestamp == rhs.absolute_capture_timestamp) && 107 (lhs.estimated_capture_clock_offset == 108 rhs.estimated_capture_clock_offset); 109 } 110 111 inline bool operator!=(const AbsoluteCaptureTime& lhs, 112 const AbsoluteCaptureTime& rhs) { 113 return !(lhs == rhs); 114 } 115 116 enum { kRtpCsrcSize = 15 }; // RFC 3550 page 13 117 118 // Audio level of CSRCs See: 119 // https://tools.ietf.org/html/rfc6465 120 struct CsrcAudioLevelList { 121 CsrcAudioLevelList() : numAudioLevels(0) { } 122 CsrcAudioLevelList(const CsrcAudioLevelList&) = default; 123 CsrcAudioLevelList& operator=(const CsrcAudioLevelList&) = default; 124 uint8_t numAudioLevels; 125 // arrOfAudioLevels has the same ordering as RTPHeader.arrOfCSRCs 126 uint8_t arrOfAudioLevels[kRtpCsrcSize]; 127 }; 128 129 struct RTPHeaderExtension { 130 RTPHeaderExtension(); 131 RTPHeaderExtension(const RTPHeaderExtension& other); 132 RTPHeaderExtension& operator=(const RTPHeaderExtension& other); 133 134 static constexpr int kAbsSendTimeFraction = 18; 135 136 Timestamp GetAbsoluteSendTimestamp() const { 137 RTC_DCHECK(hasAbsoluteSendTime); 138 RTC_DCHECK(absoluteSendTime < (1ul << 24)); 139 return Timestamp::Micros((absoluteSendTime * 1000000ll) / 140 (1 << kAbsSendTimeFraction)); 141 } 142 143 bool hasTransmissionTimeOffset; 144 int32_t transmissionTimeOffset; 145 bool hasAbsoluteSendTime; 146 uint32_t absoluteSendTime; 147 std::optional<AbsoluteCaptureTime> absolute_capture_time; 148 bool hasTransportSequenceNumber; 149 uint16_t transportSequenceNumber; 150 std::optional<FeedbackRequest> feedback_request; 151 152 // Audio Level includes both level in dBov and voiced/unvoiced bit. See: 153 // https://tools.ietf.org/html/rfc6464#section-3 154 std::optional<AudioLevel> audio_level() const { return audio_level_; } 155 156 void set_audio_level(std::optional<AudioLevel> audio_level) { 157 audio_level_ = audio_level; 158 } 159 160 // For Coordination of Video Orientation. See 161 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ 162 // ts_126114v120700p.pdf 163 bool hasVideoRotation; 164 VideoRotation videoRotation; 165 166 // TODO(ilnik): Refactor this and one above to be std::optional() and remove 167 // a corresponding bool flag. 168 bool hasVideoContentType; 169 VideoContentType videoContentType; 170 171 bool has_video_timing; 172 VideoSendTiming video_timing; 173 174 VideoPlayoutDelay playout_delay; 175 176 // For identification of a stream when ssrc is not signaled. See 177 // https://tools.ietf.org/html/rfc8852 178 std::string stream_id; 179 std::string repaired_stream_id; 180 181 // For identifying the media section used to interpret this RTP packet. See 182 // https://tools.ietf.org/html/rfc8843 183 std::string mid; 184 185 std::optional<ColorSpace> color_space; 186 187 CsrcAudioLevelList csrcAudioLevels; 188 189 private: 190 std::optional<AudioLevel> audio_level_; 191 }; 192 193 struct RTC_EXPORT RTPHeader { 194 RTPHeader(); 195 RTPHeader(const RTPHeader& other); 196 RTPHeader& operator=(const RTPHeader& other); 197 198 bool markerBit; 199 uint8_t payloadType; 200 uint16_t sequenceNumber; 201 uint32_t timestamp; 202 uint32_t ssrc; 203 uint8_t numCSRCs; 204 uint32_t arrOfCSRCs[kRtpCsrcSize]; 205 size_t paddingLength; 206 size_t headerLength; 207 RTPHeaderExtension extension; 208 }; 209 210 // RTCP mode to use. Compound mode is described by RFC 4585 and reduced-size 211 // RTCP mode is described by RFC 5506. 212 enum class RtcpMode { kOff, kCompound, kReducedSize }; 213 214 enum NetworkState { 215 kNetworkUp, 216 kNetworkDown, 217 }; 218 219 } // namespace webrtc 220 221 #endif // API_RTP_HEADERS_H_