| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 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> |
| Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 15 | #include <stdint.h> |
| Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 17 | #include <optional> |
| Niels Möller | d57efc1 | 2019-03-22 13:02:11 | [diff] [blame] | 18 | #include <string> |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 19 | |
| Sebastian Jansson | 3d61ab1 | 2019-06-14 11:35:51 | [diff] [blame] | 20 | #include "api/units/timestamp.h" |
| Johannes Kron | 09d6588 | 2018-11-27 13:36:41 | [diff] [blame] | 21 | #include "api/video/color_space.h" |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 22 | #include "api/video/video_content_type.h" |
| 23 | #include "api/video/video_rotation.h" |
| 24 | #include "api/video/video_timing.h" |
| Dor Hen | aefed55 | 2024-06-18 13:20:35 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/system/rtc_export.h" |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| Johannes Kron | 075f687 | 2019-02-14 13:41:05 | [diff] [blame] | 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 - |
| Johannes Kron | 0da25a1 | 2019-03-06 08:34:13 | [diff] [blame] | 36 | // sequence_count + 1, sequence_number]. That is, no feedback will be sent if |
| 37 | // sequence_count is zero. |
| Johannes Kron | 075f687 | 2019-02-14 13:41:05 | [diff] [blame] | 38 | int sequence_count; |
| 39 | }; |
| 40 | |
| Chen Xing | cd8a6e2 | 2019-07-01 08:56:51 | [diff] [blame] | 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 |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 78 | std::optional<int64_t> estimated_capture_clock_offset; |
| Chen Xing | cd8a6e2 | 2019-07-01 08:56:51 | [diff] [blame] | 79 | }; |
| 80 | |
| Joachim Reiersen | 5075cb4 | 2024-03-22 01:08:54 | [diff] [blame] | 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 | |
| Chen Xing | e08648d | 2019-08-05 14:29:13 | [diff] [blame] | 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 | |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 116 | struct RTPHeaderExtension { |
| 117 | RTPHeaderExtension(); |
| 118 | RTPHeaderExtension(const RTPHeaderExtension& other); |
| 119 | RTPHeaderExtension& operator=(const RTPHeaderExtension& other); |
| 120 | |
| Sebastian Jansson | 3d61ab1 | 2019-06-14 11:35:51 | [diff] [blame] | 121 | static constexpr int kAbsSendTimeFraction = 18; |
| 122 | |
| 123 | Timestamp GetAbsoluteSendTimestamp() const { |
| 124 | RTC_DCHECK(hasAbsoluteSendTime); |
| 125 | RTC_DCHECK(absoluteSendTime < (1ul << 24)); |
| Danil Chapovalov | 0c626af | 2020-02-10 10:16:00 | [diff] [blame] | 126 | return Timestamp::Micros((absoluteSendTime * 1000000ll) / |
| 127 | (1 << kAbsSendTimeFraction)); |
| Sebastian Jansson | 3d61ab1 | 2019-06-14 11:35:51 | [diff] [blame] | 128 | } |
| 129 | |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 130 | bool hasTransmissionTimeOffset; |
| 131 | int32_t transmissionTimeOffset; |
| 132 | bool hasAbsoluteSendTime; |
| 133 | uint32_t absoluteSendTime; |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 134 | std::optional<AbsoluteCaptureTime> absolute_capture_time; |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 135 | bool hasTransportSequenceNumber; |
| 136 | uint16_t transportSequenceNumber; |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 137 | std::optional<FeedbackRequest> feedback_request; |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 138 | |
| 139 | // Audio Level includes both level in dBov and voiced/unvoiced bit. See: |
| Chen Xing | d2a6686 | 2019-06-03 12:53:42 | [diff] [blame] | 140 | // https://tools.ietf.org/html/rfc6464#section-3 |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 141 | std::optional<AudioLevel> audio_level() const { return audio_level_; } |
| Joachim Reiersen | 5075cb4 | 2024-03-22 01:08:54 | [diff] [blame] | 142 | |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 143 | void set_audio_level(std::optional<AudioLevel> audio_level) { |
| Joachim Reiersen | a341fe3 | 2024-04-16 21:33:29 | [diff] [blame] | 144 | audio_level_ = audio_level; |
| 145 | } |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 146 | |
| 147 | // For Coordination of Video Orientation. See |
| 148 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ |
| 149 | // ts_126114v120700p.pdf |
| 150 | bool hasVideoRotation; |
| 151 | VideoRotation videoRotation; |
| 152 | |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 153 | // TODO(ilnik): Refactor this and one above to be std::optional() and remove |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 154 | // a corresponding bool flag. |
| 155 | bool hasVideoContentType; |
| 156 | VideoContentType videoContentType; |
| 157 | |
| 158 | bool has_video_timing; |
| 159 | VideoSendTiming video_timing; |
| 160 | |
| Niels Möller | d381eed | 2020-09-02 13:34:40 | [diff] [blame] | 161 | VideoPlayoutDelay playout_delay; |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 162 | |
| 163 | // For identification of a stream when ssrc is not signaled. See |
| Danil Chapovalov | eb28298 | 2021-03-20 18:43:11 | [diff] [blame] | 164 | // https://tools.ietf.org/html/rfc8852 |
| Niels Möller | d57efc1 | 2019-03-22 13:02:11 | [diff] [blame] | 165 | std::string stream_id; |
| 166 | std::string repaired_stream_id; |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 167 | |
| 168 | // For identifying the media section used to interpret this RTP packet. See |
| Danil Chapovalov | eb28298 | 2021-03-20 18:43:11 | [diff] [blame] | 169 | // https://tools.ietf.org/html/rfc8843 |
| Niels Möller | d57efc1 | 2019-03-22 13:02:11 | [diff] [blame] | 170 | std::string mid; |
| Johannes Kron | ad1d9f0 | 2018-11-09 10:12:36 | [diff] [blame] | 171 | |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 172 | std::optional<ColorSpace> color_space; |
| Joachim Reiersen | a341fe3 | 2024-04-16 21:33:29 | [diff] [blame] | 173 | |
| 174 | private: |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 175 | std::optional<AudioLevel> audio_level_; |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 176 | }; |
| 177 | |
| Niels Möller | 418f580 | 2019-05-08 12:24:15 | [diff] [blame] | 178 | enum { kRtpCsrcSize = 15 }; // RFC 3550 page 13 |
| 179 | |
| Philipp Hancke | 4893638 | 2023-01-20 14:23:50 | [diff] [blame] | 180 | struct RTC_EXPORT RTPHeader { |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 181 | RTPHeader(); |
| 182 | RTPHeader(const RTPHeader& other); |
| 183 | RTPHeader& operator=(const RTPHeader& other); |
| 184 | |
| 185 | bool markerBit; |
| 186 | uint8_t payloadType; |
| 187 | uint16_t sequenceNumber; |
| 188 | uint32_t timestamp; |
| 189 | uint32_t ssrc; |
| 190 | uint8_t numCSRCs; |
| 191 | uint32_t arrOfCSRCs[kRtpCsrcSize]; |
| 192 | size_t paddingLength; |
| 193 | size_t headerLength; |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 194 | RTPHeaderExtension extension; |
| 195 | }; |
| 196 | |
| 197 | // RTCP mode to use. Compound mode is described by RFC 4585 and reduced-size |
| 198 | // RTCP mode is described by RFC 5506. |
| 199 | enum class RtcpMode { kOff, kCompound, kReducedSize }; |
| 200 | |
| 201 | enum NetworkState { |
| 202 | kNetworkUp, |
| 203 | kNetworkDown, |
| 204 | }; |
| 205 | |
| Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 206 | } // namespace webrtc |
| 207 | |
| 208 | #endif // API_RTP_HEADERS_H_ |