danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/rtp_header_extensions.h" |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <string.h> |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 14 | #include <cmath> |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 15 | #include <limits> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/rtp_rtcp/include/rtp_cvo.h" |
| 18 | #include "modules/rtp_rtcp/source/byte_io.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | // TODO(bug:9855) Move kNoSpatialIdx from vp9_globals.h to common_constants |
| 20 | #include "modules/video_coding/codecs/interface/common_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | // Absolute send time in RTP streams. |
| 25 | // |
| 26 | // The absolute send time is signaled to the receiver in-band using the |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 27 | // general mechanism for RTP header extensions [RFC8285]. The payload |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 28 | // of this extension (the transmitted value) is a 24-bit unsigned integer |
| 29 | // containing the sender's current time in seconds as a fixed point number |
| 30 | // with 18 bits fractional part. |
| 31 | // |
| 32 | // The form of the absolute send time extension block: |
| 33 | // |
| 34 | // 0 1 2 3 |
| 35 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 36 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | // | ID | len=2 | absolute send time | |
| 38 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
danilchap | e2a0177 | 2016-10-28 07:08:58 -0700 | [diff] [blame] | 39 | constexpr RTPExtensionType AbsoluteSendTime::kId; |
| 40 | constexpr uint8_t AbsoluteSendTime::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 41 | constexpr const char AbsoluteSendTime::kUri[]; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 42 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 43 | bool AbsoluteSendTime::Parse(rtc::ArrayView<const uint8_t> data, |
| 44 | uint32_t* time_24bits) { |
| 45 | if (data.size() != 3) |
| 46 | return false; |
| 47 | *time_24bits = ByteReader<uint32_t, 3>::ReadBigEndian(data.data()); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 51 | bool AbsoluteSendTime::Write(rtc::ArrayView<uint8_t> data, |
| 52 | uint32_t time_24bits) { |
| 53 | RTC_DCHECK_EQ(data.size(), 3); |
Danil Chapovalov | f3ba648 | 2017-06-12 15:43:55 +0200 | [diff] [blame] | 54 | RTC_DCHECK_LE(time_24bits, 0x00FFFFFF); |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 55 | ByteWriter<uint32_t, 3>::WriteBigEndian(data.data(), time_24bits); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
| 59 | // An RTP Header Extension for Client-to-Mixer Audio Level Indication |
| 60 | // |
| 61 | // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/ |
| 62 | // |
| 63 | // The form of the audio level extension block: |
| 64 | // |
| 65 | // 0 1 |
| 66 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 67 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | // | ID | len=0 |V| level | |
| 69 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | // |
danilchap | e2a0177 | 2016-10-28 07:08:58 -0700 | [diff] [blame] | 71 | constexpr RTPExtensionType AudioLevel::kId; |
| 72 | constexpr uint8_t AudioLevel::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 73 | constexpr const char AudioLevel::kUri[]; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 74 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 75 | bool AudioLevel::Parse(rtc::ArrayView<const uint8_t> data, |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 76 | bool* voice_activity, |
| 77 | uint8_t* audio_level) { |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 78 | if (data.size() != 1) |
| 79 | return false; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 80 | *voice_activity = (data[0] & 0x80) != 0; |
| 81 | *audio_level = data[0] & 0x7F; |
| 82 | return true; |
| 83 | } |
| 84 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 85 | bool AudioLevel::Write(rtc::ArrayView<uint8_t> data, |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 86 | bool voice_activity, |
| 87 | uint8_t audio_level) { |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 88 | RTC_DCHECK_EQ(data.size(), 1); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 89 | RTC_CHECK_LE(audio_level, 0x7f); |
| 90 | data[0] = (voice_activity ? 0x80 : 0x00) | audio_level; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | // From RFC 5450: Transmission Time Offsets in RTP Streams. |
| 95 | // |
| 96 | // The transmission time is signaled to the receiver in-band using the |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 97 | // general mechanism for RTP header extensions [RFC8285]. The payload |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 98 | // of this extension (the transmitted value) is a 24-bit signed integer. |
| 99 | // When added to the RTP timestamp of the packet, it represents the |
| 100 | // "effective" RTP transmission time of the packet, on the RTP |
| 101 | // timescale. |
| 102 | // |
| 103 | // The form of the transmission offset extension block: |
| 104 | // |
| 105 | // 0 1 2 3 |
| 106 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 107 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 108 | // | ID | len=2 | transmission offset | |
| 109 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
danilchap | e2a0177 | 2016-10-28 07:08:58 -0700 | [diff] [blame] | 110 | constexpr RTPExtensionType TransmissionOffset::kId; |
| 111 | constexpr uint8_t TransmissionOffset::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 112 | constexpr const char TransmissionOffset::kUri[]; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 113 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 114 | bool TransmissionOffset::Parse(rtc::ArrayView<const uint8_t> data, |
| 115 | int32_t* rtp_time) { |
| 116 | if (data.size() != 3) |
| 117 | return false; |
| 118 | *rtp_time = ByteReader<int32_t, 3>::ReadBigEndian(data.data()); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 122 | bool TransmissionOffset::Write(rtc::ArrayView<uint8_t> data, int32_t rtp_time) { |
| 123 | RTC_DCHECK_EQ(data.size(), 3); |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 124 | RTC_DCHECK_LE(rtp_time, 0x00ffffff); |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 125 | ByteWriter<int32_t, 3>::WriteBigEndian(data.data(), rtp_time); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 129 | // TransportSequenceNumber |
| 130 | // |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 131 | // 0 1 2 |
| 132 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 |
| 133 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 134 | // | ID | L=1 |transport-wide sequence number | |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 135 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
danilchap | e2a0177 | 2016-10-28 07:08:58 -0700 | [diff] [blame] | 136 | constexpr RTPExtensionType TransportSequenceNumber::kId; |
| 137 | constexpr uint8_t TransportSequenceNumber::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 138 | constexpr const char TransportSequenceNumber::kUri[]; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 139 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 140 | bool TransportSequenceNumber::Parse(rtc::ArrayView<const uint8_t> data, |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 141 | uint16_t* transport_sequence_number) { |
| 142 | if (data.size() != kValueSizeBytes) |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 143 | return false; |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 144 | *transport_sequence_number = ByteReader<uint16_t>::ReadBigEndian(data.data()); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 148 | bool TransportSequenceNumber::Write(rtc::ArrayView<uint8_t> data, |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 149 | uint16_t transport_sequence_number) { |
| 150 | RTC_DCHECK_EQ(data.size(), ValueSize(transport_sequence_number)); |
| 151 | ByteWriter<uint16_t>::WriteBigEndian(data.data(), transport_sequence_number); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | // TransportSequenceNumberV2 |
| 156 | // |
| 157 | // In addition to the format used for TransportSequencNumber, V2 also supports |
| 158 | // the following packet format where two extra bytes are used to specify that |
| 159 | // the sender requests immediate feedback. |
| 160 | // 0 1 2 3 |
| 161 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 162 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 163 | // | ID | L=3 |transport-wide sequence number |T| seq count | |
| 164 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 165 | // |seq count cont.| |
| 166 | // +-+-+-+-+-+-+-+-+ |
| 167 | // |
| 168 | // The bit |T| determines whether the feedback should include timing information |
Johannes Kron | 0da25a1 | 2019-03-06 09:34:13 +0100 | [diff] [blame] | 169 | // or not and |seq_count| determines how many packets the feedback packet should |
| 170 | // cover including the current packet. If |seq_count| is zero no feedback is |
| 171 | // requested. |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 172 | constexpr RTPExtensionType TransportSequenceNumberV2::kId; |
Johannes Kron | 0da25a1 | 2019-03-06 09:34:13 +0100 | [diff] [blame] | 173 | constexpr uint8_t TransportSequenceNumberV2::kValueSizeBytes; |
| 174 | constexpr uint8_t |
| 175 | TransportSequenceNumberV2::kValueSizeBytesWithoutFeedbackRequest; |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 176 | constexpr const char TransportSequenceNumberV2::kUri[]; |
| 177 | constexpr uint16_t TransportSequenceNumberV2::kIncludeTimestampsBit; |
| 178 | |
| 179 | bool TransportSequenceNumberV2::Parse( |
| 180 | rtc::ArrayView<const uint8_t> data, |
| 181 | uint16_t* transport_sequence_number, |
| 182 | absl::optional<FeedbackRequest>* feedback_request) { |
Johannes Kron | 0da25a1 | 2019-03-06 09:34:13 +0100 | [diff] [blame] | 183 | if (data.size() != kValueSizeBytes && |
| 184 | data.size() != kValueSizeBytesWithoutFeedbackRequest) |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 185 | return false; |
| 186 | |
| 187 | *transport_sequence_number = ByteReader<uint16_t>::ReadBigEndian(data.data()); |
| 188 | |
Johannes Kron | 0da25a1 | 2019-03-06 09:34:13 +0100 | [diff] [blame] | 189 | *feedback_request = absl::nullopt; |
| 190 | if (data.size() == kValueSizeBytes) { |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 191 | uint16_t feedback_request_raw = |
| 192 | ByteReader<uint16_t>::ReadBigEndian(data.data() + 2); |
| 193 | bool include_timestamps = |
| 194 | (feedback_request_raw & kIncludeTimestampsBit) != 0; |
| 195 | uint16_t sequence_count = feedback_request_raw & ~kIncludeTimestampsBit; |
Johannes Kron | 0da25a1 | 2019-03-06 09:34:13 +0100 | [diff] [blame] | 196 | |
| 197 | // If |sequence_count| is zero no feedback is requested. |
| 198 | if (sequence_count != 0) { |
| 199 | *feedback_request = {include_timestamps, sequence_count}; |
| 200 | } |
Johannes Kron | 54047be | 2019-02-21 14:09:20 +0000 | [diff] [blame] | 201 | } |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool TransportSequenceNumberV2::Write( |
| 206 | rtc::ArrayView<uint8_t> data, |
| 207 | uint16_t transport_sequence_number, |
| 208 | const absl::optional<FeedbackRequest>& feedback_request) { |
| 209 | RTC_DCHECK_EQ(data.size(), |
| 210 | ValueSize(transport_sequence_number, feedback_request)); |
| 211 | |
| 212 | ByteWriter<uint16_t>::WriteBigEndian(data.data(), transport_sequence_number); |
| 213 | |
| 214 | if (feedback_request) { |
| 215 | RTC_DCHECK_GE(feedback_request->sequence_count, 0); |
| 216 | RTC_DCHECK_LT(feedback_request->sequence_count, kIncludeTimestampsBit); |
| 217 | uint16_t feedback_request_raw = |
| 218 | feedback_request->sequence_count | |
| 219 | (feedback_request->include_timestamps ? kIncludeTimestampsBit : 0); |
| 220 | ByteWriter<uint16_t>::WriteBigEndian(data.data() + 2, feedback_request_raw); |
| 221 | } |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
| 225 | // Coordination of Video Orientation in RTP streams. |
| 226 | // |
| 227 | // Coordination of Video Orientation consists in signaling of the current |
| 228 | // orientation of the image captured on the sender side to the receiver for |
| 229 | // appropriate rendering and displaying. |
| 230 | // |
| 231 | // 0 1 |
| 232 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 233 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 234 | // | ID | len=0 |0 0 0 0 C F R R| |
| 235 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
danilchap | e2a0177 | 2016-10-28 07:08:58 -0700 | [diff] [blame] | 236 | constexpr RTPExtensionType VideoOrientation::kId; |
| 237 | constexpr uint8_t VideoOrientation::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 238 | constexpr const char VideoOrientation::kUri[]; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 239 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 240 | bool VideoOrientation::Parse(rtc::ArrayView<const uint8_t> data, |
| 241 | VideoRotation* rotation) { |
| 242 | if (data.size() != 1) |
| 243 | return false; |
magjed | 71eb61c | 2016-09-08 03:24:58 -0700 | [diff] [blame] | 244 | *rotation = ConvertCVOByteToVideoRotation(data[0]); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 248 | bool VideoOrientation::Write(rtc::ArrayView<uint8_t> data, |
| 249 | VideoRotation rotation) { |
| 250 | RTC_DCHECK_EQ(data.size(), 1); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 251 | data[0] = ConvertVideoRotationToCVOByte(rotation); |
| 252 | return true; |
| 253 | } |
| 254 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 255 | bool VideoOrientation::Parse(rtc::ArrayView<const uint8_t> data, |
| 256 | uint8_t* value) { |
| 257 | if (data.size() != 1) |
| 258 | return false; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 259 | *value = data[0]; |
| 260 | return true; |
| 261 | } |
| 262 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 263 | bool VideoOrientation::Write(rtc::ArrayView<uint8_t> data, uint8_t value) { |
| 264 | RTC_DCHECK_EQ(data.size(), 1); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 265 | data[0] = value; |
| 266 | return true; |
| 267 | } |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 268 | |
| 269 | // 0 1 2 3 |
| 270 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 271 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 272 | // | ID | len=2 | MIN delay | MAX delay | |
| 273 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 274 | constexpr RTPExtensionType PlayoutDelayLimits::kId; |
| 275 | constexpr uint8_t PlayoutDelayLimits::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 276 | constexpr const char PlayoutDelayLimits::kUri[]; |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 277 | |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 278 | bool PlayoutDelayLimits::Parse(rtc::ArrayView<const uint8_t> data, |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 279 | PlayoutDelay* playout_delay) { |
| 280 | RTC_DCHECK(playout_delay); |
danilchap | 978504e | 2017-04-06 01:03:53 -0700 | [diff] [blame] | 281 | if (data.size() != 3) |
| 282 | return false; |
| 283 | uint32_t raw = ByteReader<uint32_t, 3>::ReadBigEndian(data.data()); |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 284 | uint16_t min_raw = (raw >> 12); |
| 285 | uint16_t max_raw = (raw & 0xfff); |
| 286 | if (min_raw > max_raw) |
| 287 | return false; |
| 288 | playout_delay->min_ms = min_raw * kGranularityMs; |
| 289 | playout_delay->max_ms = max_raw * kGranularityMs; |
| 290 | return true; |
| 291 | } |
| 292 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 293 | bool PlayoutDelayLimits::Write(rtc::ArrayView<uint8_t> data, |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 294 | const PlayoutDelay& playout_delay) { |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 295 | RTC_DCHECK_EQ(data.size(), 3); |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 296 | RTC_DCHECK_LE(0, playout_delay.min_ms); |
| 297 | RTC_DCHECK_LE(playout_delay.min_ms, playout_delay.max_ms); |
| 298 | RTC_DCHECK_LE(playout_delay.max_ms, kMaxMs); |
| 299 | // Convert MS to value to be sent on extension header. |
| 300 | uint32_t min_delay = playout_delay.min_ms / kGranularityMs; |
| 301 | uint32_t max_delay = playout_delay.max_ms / kGranularityMs; |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 302 | ByteWriter<uint32_t, 3>::WriteBigEndian(data.data(), |
| 303 | (min_delay << 12) | max_delay); |
Danil Chapovalov | 08b0351 | 2016-09-07 15:08:13 +0200 | [diff] [blame] | 304 | return true; |
| 305 | } |
| 306 | |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 307 | // Video Content Type. |
| 308 | // |
| 309 | // E.g. default video or screenshare. |
| 310 | // |
| 311 | // 0 1 |
| 312 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 313 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 314 | // | ID | len=0 | Content type | |
| 315 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 316 | constexpr RTPExtensionType VideoContentTypeExtension::kId; |
| 317 | constexpr uint8_t VideoContentTypeExtension::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 318 | constexpr const char VideoContentTypeExtension::kUri[]; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 319 | |
| 320 | bool VideoContentTypeExtension::Parse(rtc::ArrayView<const uint8_t> data, |
| 321 | VideoContentType* content_type) { |
| 322 | if (data.size() == 1 && |
ilnik | 6d5b4d6 | 2017-08-30 03:32:14 -0700 | [diff] [blame] | 323 | videocontenttypehelpers::IsValidContentType(data[0])) { |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 324 | *content_type = static_cast<VideoContentType>(data[0]); |
| 325 | return true; |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 330 | bool VideoContentTypeExtension::Write(rtc::ArrayView<uint8_t> data, |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 331 | VideoContentType content_type) { |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 332 | RTC_DCHECK_EQ(data.size(), 1); |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 333 | data[0] = static_cast<uint8_t>(content_type); |
| 334 | return true; |
| 335 | } |
| 336 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 337 | // Video Timing. |
| 338 | // 6 timestamps in milliseconds counted from capture time stored in rtp header: |
| 339 | // encode start/finish, packetization complete, pacer exit and reserved for |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 340 | // modification by the network modification. |flags| is a bitmask and has the |
| 341 | // following allowed values: |
| 342 | // 0 = Valid data, but no flags available (backwards compatibility) |
| 343 | // 1 = Frame marked as timing frame due to cyclic timer. |
| 344 | // 2 = Frame marked as timing frame due to size being outside limit. |
| 345 | // 255 = Invalid. The whole timing frame extension should be ignored. |
| 346 | // |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 347 | // 0 1 2 3 |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 348 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 349 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 350 | // | ID | len=12| flags | encode start ms delta | |
| 351 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 352 | // | encode finish ms delta | packetizer finish ms delta | |
| 353 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 354 | // | pacer exit ms delta | network timestamp ms delta | |
| 355 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 356 | // | network2 timestamp ms delta | |
| 357 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 358 | |
| 359 | constexpr RTPExtensionType VideoTimingExtension::kId; |
| 360 | constexpr uint8_t VideoTimingExtension::kValueSizeBytes; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 361 | constexpr const char VideoTimingExtension::kUri[]; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 362 | |
| 363 | bool VideoTimingExtension::Parse(rtc::ArrayView<const uint8_t> data, |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 364 | VideoSendTiming* timing) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 365 | RTC_DCHECK(timing); |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 366 | // TODO(sprang): Deprecate support for old wire format. |
| 367 | ptrdiff_t off = 0; |
| 368 | switch (data.size()) { |
| 369 | case kValueSizeBytes - 1: |
| 370 | timing->flags = 0; |
| 371 | off = 1; // Old wire format without the flags field. |
| 372 | break; |
| 373 | case kValueSizeBytes: |
| 374 | timing->flags = ByteReader<uint8_t>::ReadBigEndian(data.data()); |
| 375 | break; |
| 376 | default: |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | timing->encode_start_delta_ms = ByteReader<uint16_t>::ReadBigEndian( |
| 381 | data.data() + VideoSendTiming::kEncodeStartDeltaOffset - off); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 382 | timing->encode_finish_delta_ms = ByteReader<uint16_t>::ReadBigEndian( |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 383 | data.data() + VideoSendTiming::kEncodeFinishDeltaOffset - off); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 384 | timing->packetization_finish_delta_ms = ByteReader<uint16_t>::ReadBigEndian( |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 385 | data.data() + VideoSendTiming::kPacketizationFinishDeltaOffset - off); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 386 | timing->pacer_exit_delta_ms = ByteReader<uint16_t>::ReadBigEndian( |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 387 | data.data() + VideoSendTiming::kPacerExitDeltaOffset - off); |
Danil Chapovalov | 996eb9e | 2017-10-30 17:14:41 +0100 | [diff] [blame] | 388 | timing->network_timestamp_delta_ms = ByteReader<uint16_t>::ReadBigEndian( |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 389 | data.data() + VideoSendTiming::kNetworkTimestampDeltaOffset - off); |
Danil Chapovalov | 996eb9e | 2017-10-30 17:14:41 +0100 | [diff] [blame] | 390 | timing->network2_timestamp_delta_ms = ByteReader<uint16_t>::ReadBigEndian( |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 391 | data.data() + VideoSendTiming::kNetwork2TimestampDeltaOffset - off); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 395 | bool VideoTimingExtension::Write(rtc::ArrayView<uint8_t> data, |
| 396 | const VideoSendTiming& timing) { |
| 397 | RTC_DCHECK_EQ(data.size(), 1 + 2 * 6); |
| 398 | ByteWriter<uint8_t>::WriteBigEndian( |
| 399 | data.data() + VideoSendTiming::kFlagsOffset, timing.flags); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 400 | ByteWriter<uint16_t>::WriteBigEndian( |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 401 | data.data() + VideoSendTiming::kEncodeStartDeltaOffset, |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 402 | timing.encode_start_delta_ms); |
| 403 | ByteWriter<uint16_t>::WriteBigEndian( |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 404 | data.data() + VideoSendTiming::kEncodeFinishDeltaOffset, |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 405 | timing.encode_finish_delta_ms); |
| 406 | ByteWriter<uint16_t>::WriteBigEndian( |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 407 | data.data() + VideoSendTiming::kPacketizationFinishDeltaOffset, |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 408 | timing.packetization_finish_delta_ms); |
| 409 | ByteWriter<uint16_t>::WriteBigEndian( |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 410 | data.data() + VideoSendTiming::kPacerExitDeltaOffset, |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 411 | timing.pacer_exit_delta_ms); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 412 | ByteWriter<uint16_t>::WriteBigEndian( |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 413 | data.data() + VideoSendTiming::kNetworkTimestampDeltaOffset, |
Danil Chapovalov | f0cc814 | 2017-10-31 17:59:39 +0100 | [diff] [blame] | 414 | timing.network_timestamp_delta_ms); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 415 | ByteWriter<uint16_t>::WriteBigEndian( |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 416 | data.data() + VideoSendTiming::kNetwork2TimestampDeltaOffset, |
Danil Chapovalov | f0cc814 | 2017-10-31 17:59:39 +0100 | [diff] [blame] | 417 | timing.network2_timestamp_delta_ms); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 418 | return true; |
| 419 | } |
| 420 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 421 | bool VideoTimingExtension::Write(rtc::ArrayView<uint8_t> data, |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 422 | uint16_t time_delta_ms, |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 423 | uint8_t offset) { |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 424 | RTC_DCHECK_GE(data.size(), offset + 2); |
Danil Chapovalov | f0cc814 | 2017-10-31 17:59:39 +0100 | [diff] [blame] | 425 | RTC_DCHECK_LE(offset, kValueSizeBytes - sizeof(uint16_t)); |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 426 | ByteWriter<uint16_t>::WriteBigEndian(data.data() + offset, time_delta_ms); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 427 | return true; |
| 428 | } |
| 429 | |
Johnny Lee | e0c8b23 | 2018-09-11 16:50:49 -0400 | [diff] [blame] | 430 | // Frame Marking. |
| 431 | // |
| 432 | // Meta-information about an RTP stream outside the encrypted media payload, |
| 433 | // useful for an RTP switch to do codec-agnostic selective forwarding |
| 434 | // without decrypting the payload. |
| 435 | // |
| 436 | // For non-scalable streams: |
| 437 | // 0 1 |
| 438 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 439 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 440 | // | ID | L = 0 |S|E|I|D|0 0 0 0| |
| 441 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 442 | // |
| 443 | // For scalable streams: |
| 444 | // 0 1 2 3 |
| 445 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 446 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 447 | // | ID | L = 2 |S|E|I|D|B| TID | LID | TL0PICIDX | |
| 448 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 449 | |
| 450 | constexpr RTPExtensionType FrameMarkingExtension::kId; |
| 451 | constexpr const char FrameMarkingExtension::kUri[]; |
| 452 | |
| 453 | bool FrameMarkingExtension::IsScalable(uint8_t temporal_id, uint8_t layer_id) { |
| 454 | return temporal_id != kNoTemporalIdx || layer_id != kNoSpatialIdx; |
| 455 | } |
| 456 | |
| 457 | bool FrameMarkingExtension::Parse(rtc::ArrayView<const uint8_t> data, |
| 458 | FrameMarking* frame_marking) { |
| 459 | RTC_DCHECK(frame_marking); |
| 460 | |
| 461 | if (data.size() != 1 && data.size() != 3) |
| 462 | return false; |
| 463 | |
| 464 | frame_marking->start_of_frame = (data[0] & 0x80) != 0; |
| 465 | frame_marking->end_of_frame = (data[0] & 0x40) != 0; |
| 466 | frame_marking->independent_frame = (data[0] & 0x20) != 0; |
| 467 | frame_marking->discardable_frame = (data[0] & 0x10) != 0; |
| 468 | |
| 469 | if (data.size() == 3) { |
| 470 | frame_marking->base_layer_sync = (data[0] & 0x08) != 0; |
| 471 | frame_marking->temporal_id = data[0] & 0x7; |
| 472 | frame_marking->layer_id = data[1]; |
| 473 | frame_marking->tl0_pic_idx = data[2]; |
| 474 | } else { |
| 475 | // non-scalable |
| 476 | frame_marking->base_layer_sync = false; |
| 477 | frame_marking->temporal_id = kNoTemporalIdx; |
| 478 | frame_marking->layer_id = kNoSpatialIdx; |
| 479 | frame_marking->tl0_pic_idx = 0; |
| 480 | } |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | size_t FrameMarkingExtension::ValueSize(const FrameMarking& frame_marking) { |
| 485 | if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id)) |
| 486 | return 3; |
| 487 | else |
| 488 | return 1; |
| 489 | } |
| 490 | |
| 491 | bool FrameMarkingExtension::Write(rtc::ArrayView<uint8_t> data, |
| 492 | const FrameMarking& frame_marking) { |
| 493 | RTC_DCHECK_GE(data.size(), 1); |
| 494 | RTC_CHECK_LE(frame_marking.temporal_id, 0x07); |
| 495 | data[0] = frame_marking.start_of_frame ? 0x80 : 0x00; |
| 496 | data[0] |= frame_marking.end_of_frame ? 0x40 : 0x00; |
| 497 | data[0] |= frame_marking.independent_frame ? 0x20 : 0x00; |
| 498 | data[0] |= frame_marking.discardable_frame ? 0x10 : 0x00; |
| 499 | |
| 500 | if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id)) { |
| 501 | RTC_DCHECK_EQ(data.size(), 3); |
| 502 | data[0] |= frame_marking.base_layer_sync ? 0x08 : 0x00; |
| 503 | data[0] |= frame_marking.temporal_id & 0x07; |
| 504 | data[1] = frame_marking.layer_id; |
| 505 | data[2] = frame_marking.tl0_pic_idx; |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 510 | // Color space including HDR metadata as an optional field. |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 511 | // |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 512 | // RTP header extension to carry color space information and optionally HDR |
| 513 | // metadata. The float values in the HDR metadata struct are upscaled by a |
| 514 | // static factor and transmitted as unsigned integers. |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 515 | // |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 516 | // Data layout of color space with HDR metadata (two-byte RTP header extension) |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 517 | // 0 1 2 3 |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 518 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 519 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 520 | // | ID | length=28 | primaries | transfer | |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 521 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 522 | // | matrix |range+chr.sit. | luminance_max | |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 523 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 524 | // | luminance_min | mastering_metadata.| |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 525 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 526 | // |primary_r.x and .y | mastering_metadata.| |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 527 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 528 | // |primary_g.x and .y | mastering_metadata.| |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 529 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 530 | // |primary_b.x and .y | mastering_metadata.| |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 531 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 532 | // |white.x and .y | max_content_light_level | |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 533 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 534 | // | max_frame_average_light_level | |
| 535 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 536 | // |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 537 | // Data layout of color space w/o HDR metadata (one-byte RTP header extension) |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 538 | // 0 1 2 3 |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 539 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 540 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 541 | // | ID | L = 3 | primaries | transfer | matrix | |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 542 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 543 | // |range+chr.sit. | |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 544 | // +-+-+-+-+-+-+-+-+ |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 545 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 546 | constexpr RTPExtensionType ColorSpaceExtension::kId; |
| 547 | constexpr uint8_t ColorSpaceExtension::kValueSizeBytes; |
| 548 | constexpr const char ColorSpaceExtension::kUri[]; |
| 549 | |
| 550 | bool ColorSpaceExtension::Parse(rtc::ArrayView<const uint8_t> data, |
| 551 | ColorSpace* color_space) { |
| 552 | RTC_DCHECK(color_space); |
| 553 | if (data.size() != kValueSizeBytes && |
| 554 | data.size() != kValueSizeBytesWithoutHdrMetadata) |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 555 | return false; |
| 556 | |
| 557 | size_t offset = 0; |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 558 | // Read color space information. |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 559 | if (!color_space->set_primaries_from_uint8(data[offset++])) |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 560 | return false; |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 561 | if (!color_space->set_transfer_from_uint8(data[offset++])) |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 562 | return false; |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 563 | if (!color_space->set_matrix_from_uint8(data[offset++])) |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 564 | return false; |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 565 | |
| 566 | uint8_t range_and_chroma_siting = data[offset++]; |
| 567 | if (!color_space->set_range_from_uint8((range_and_chroma_siting >> 4) & 0x03)) |
| 568 | return false; |
| 569 | if (!color_space->set_chroma_siting_horizontal_from_uint8( |
| 570 | (range_and_chroma_siting >> 2) & 0x03)) |
| 571 | return false; |
| 572 | if (!color_space->set_chroma_siting_vertical_from_uint8( |
| 573 | range_and_chroma_siting & 0x03)) |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 574 | return false; |
| 575 | |
| 576 | // Read HDR metadata if it exists, otherwise clear it. |
| 577 | if (data.size() == kValueSizeBytesWithoutHdrMetadata) { |
| 578 | color_space->set_hdr_metadata(nullptr); |
| 579 | } else { |
| 580 | HdrMetadata hdr_metadata; |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 581 | offset += ParseHdrMetadata(data.subview(offset), &hdr_metadata); |
| 582 | if (!hdr_metadata.Validate()) |
| 583 | return false; |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 584 | color_space->set_hdr_metadata(&hdr_metadata); |
| 585 | } |
| 586 | RTC_DCHECK_EQ(ValueSize(*color_space), offset); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 590 | bool ColorSpaceExtension::Write(rtc::ArrayView<uint8_t> data, |
| 591 | const ColorSpace& color_space) { |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 592 | RTC_DCHECK_EQ(data.size(), ValueSize(color_space)); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 593 | size_t offset = 0; |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 594 | // Write color space information. |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 595 | data[offset++] = static_cast<uint8_t>(color_space.primaries()); |
| 596 | data[offset++] = static_cast<uint8_t>(color_space.transfer()); |
| 597 | data[offset++] = static_cast<uint8_t>(color_space.matrix()); |
| 598 | data[offset++] = CombineRangeAndChromaSiting( |
| 599 | color_space.range(), color_space.chroma_siting_horizontal(), |
| 600 | color_space.chroma_siting_vertical()); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 601 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 602 | // Write HDR metadata if it exists. |
| 603 | if (color_space.hdr_metadata()) { |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 604 | offset += |
| 605 | WriteHdrMetadata(data.subview(offset), *color_space.hdr_metadata()); |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 606 | } |
| 607 | RTC_DCHECK_EQ(ValueSize(color_space), offset); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 608 | return true; |
| 609 | } |
| 610 | |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 611 | // Combines range and chroma siting into one byte with the following bit layout: |
| 612 | // bits 0-1 Chroma siting vertical. |
| 613 | // 2-3 Chroma siting horizontal. |
| 614 | // 4-5 Range. |
| 615 | // 6-7 Unused. |
| 616 | uint8_t ColorSpaceExtension::CombineRangeAndChromaSiting( |
| 617 | ColorSpace::RangeID range, |
| 618 | ColorSpace::ChromaSiting chroma_siting_horizontal, |
| 619 | ColorSpace::ChromaSiting chroma_siting_vertical) { |
| 620 | RTC_DCHECK_LE(static_cast<uint8_t>(range), 3); |
| 621 | RTC_DCHECK_LE(static_cast<uint8_t>(chroma_siting_horizontal), 3); |
| 622 | RTC_DCHECK_LE(static_cast<uint8_t>(chroma_siting_vertical), 3); |
| 623 | return (static_cast<uint8_t>(range) << 4) | |
| 624 | (static_cast<uint8_t>(chroma_siting_horizontal) << 2) | |
| 625 | static_cast<uint8_t>(chroma_siting_vertical); |
| 626 | } |
| 627 | |
| 628 | size_t ColorSpaceExtension::ParseHdrMetadata(rtc::ArrayView<const uint8_t> data, |
| 629 | HdrMetadata* hdr_metadata) { |
| 630 | RTC_DCHECK_EQ(data.size(), |
| 631 | kValueSizeBytes - kValueSizeBytesWithoutHdrMetadata); |
| 632 | size_t offset = 0; |
| 633 | offset += ParseLuminance(data.data() + offset, |
| 634 | &hdr_metadata->mastering_metadata.luminance_max, |
| 635 | kLuminanceMaxDenominator); |
| 636 | offset += ParseLuminance(data.data() + offset, |
| 637 | &hdr_metadata->mastering_metadata.luminance_min, |
| 638 | kLuminanceMinDenominator); |
| 639 | offset += ParseChromaticity(data.data() + offset, |
| 640 | &hdr_metadata->mastering_metadata.primary_r); |
| 641 | offset += ParseChromaticity(data.data() + offset, |
| 642 | &hdr_metadata->mastering_metadata.primary_g); |
| 643 | offset += ParseChromaticity(data.data() + offset, |
| 644 | &hdr_metadata->mastering_metadata.primary_b); |
| 645 | offset += ParseChromaticity(data.data() + offset, |
| 646 | &hdr_metadata->mastering_metadata.white_point); |
| 647 | hdr_metadata->max_content_light_level = |
| 648 | ByteReader<uint16_t>::ReadBigEndian(data.data() + offset); |
| 649 | offset += 2; |
| 650 | hdr_metadata->max_frame_average_light_level = |
| 651 | ByteReader<uint16_t>::ReadBigEndian(data.data() + offset); |
| 652 | offset += 2; |
| 653 | return offset; |
| 654 | } |
| 655 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 656 | size_t ColorSpaceExtension::ParseChromaticity( |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 657 | const uint8_t* data, |
| 658 | HdrMasteringMetadata::Chromaticity* p) { |
| 659 | uint16_t chromaticity_x_scaled = ByteReader<uint16_t>::ReadBigEndian(data); |
| 660 | uint16_t chromaticity_y_scaled = |
| 661 | ByteReader<uint16_t>::ReadBigEndian(data + 2); |
| 662 | p->x = static_cast<float>(chromaticity_x_scaled) / kChromaticityDenominator; |
| 663 | p->y = static_cast<float>(chromaticity_y_scaled) / kChromaticityDenominator; |
| 664 | return 4; // Return number of bytes read. |
| 665 | } |
| 666 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 667 | size_t ColorSpaceExtension::ParseLuminance(const uint8_t* data, |
| 668 | float* f, |
| 669 | int denominator) { |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 670 | uint16_t luminance_scaled = ByteReader<uint16_t>::ReadBigEndian(data); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 671 | *f = static_cast<float>(luminance_scaled) / denominator; |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 672 | return 2; // Return number of bytes read. |
| 673 | } |
| 674 | |
| 675 | size_t ColorSpaceExtension::WriteHdrMetadata(rtc::ArrayView<uint8_t> data, |
| 676 | const HdrMetadata& hdr_metadata) { |
| 677 | RTC_DCHECK_EQ(data.size(), |
| 678 | kValueSizeBytes - kValueSizeBytesWithoutHdrMetadata); |
| 679 | RTC_DCHECK(hdr_metadata.Validate()); |
| 680 | size_t offset = 0; |
| 681 | offset += WriteLuminance(data.data() + offset, |
| 682 | hdr_metadata.mastering_metadata.luminance_max, |
| 683 | kLuminanceMaxDenominator); |
| 684 | offset += WriteLuminance(data.data() + offset, |
| 685 | hdr_metadata.mastering_metadata.luminance_min, |
| 686 | kLuminanceMinDenominator); |
| 687 | offset += WriteChromaticity(data.data() + offset, |
| 688 | hdr_metadata.mastering_metadata.primary_r); |
| 689 | offset += WriteChromaticity(data.data() + offset, |
| 690 | hdr_metadata.mastering_metadata.primary_g); |
| 691 | offset += WriteChromaticity(data.data() + offset, |
| 692 | hdr_metadata.mastering_metadata.primary_b); |
| 693 | offset += WriteChromaticity(data.data() + offset, |
| 694 | hdr_metadata.mastering_metadata.white_point); |
| 695 | |
| 696 | ByteWriter<uint16_t>::WriteBigEndian(data.data() + offset, |
| 697 | hdr_metadata.max_content_light_level); |
| 698 | offset += 2; |
| 699 | ByteWriter<uint16_t>::WriteBigEndian( |
| 700 | data.data() + offset, hdr_metadata.max_frame_average_light_level); |
| 701 | offset += 2; |
| 702 | return offset; |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 705 | size_t ColorSpaceExtension::WriteChromaticity( |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 706 | uint8_t* data, |
| 707 | const HdrMasteringMetadata::Chromaticity& p) { |
| 708 | RTC_DCHECK_GE(p.x, 0.0f); |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 709 | RTC_DCHECK_LE(p.x, 1.0f); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 710 | RTC_DCHECK_GE(p.y, 0.0f); |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 711 | RTC_DCHECK_LE(p.y, 1.0f); |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 712 | ByteWriter<uint16_t>::WriteBigEndian( |
| 713 | data, std::round(p.x * kChromaticityDenominator)); |
| 714 | ByteWriter<uint16_t>::WriteBigEndian( |
| 715 | data + 2, std::round(p.y * kChromaticityDenominator)); |
| 716 | return 4; // Return number of bytes written. |
| 717 | } |
| 718 | |
Johannes Kron | 09d6588 | 2018-11-27 14:36:41 +0100 | [diff] [blame] | 719 | size_t ColorSpaceExtension::WriteLuminance(uint8_t* data, |
| 720 | float f, |
| 721 | int denominator) { |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 722 | RTC_DCHECK_GE(f, 0.0f); |
Johannes Kron | c13f4be | 2018-12-12 09:52:53 +0100 | [diff] [blame] | 723 | float upscaled_value = f * denominator; |
| 724 | RTC_DCHECK_LE(upscaled_value, std::numeric_limits<uint16_t>::max()); |
| 725 | ByteWriter<uint16_t>::WriteBigEndian(data, std::round(upscaled_value)); |
| 726 | return 2; // Return number of bytes written. |
Johannes Kron | ad1d9f0 | 2018-11-09 11:12:36 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Steve Anton | a3251dd | 2017-07-21 09:58:31 -0700 | [diff] [blame] | 729 | bool BaseRtpStringExtension::Parse(rtc::ArrayView<const uint8_t> data, |
Steve Anton | a3251dd | 2017-07-21 09:58:31 -0700 | [diff] [blame] | 730 | std::string* str) { |
| 731 | if (data.empty() || data[0] == 0) // Valid string extension can't be empty. |
| 732 | return false; |
| 733 | const char* cstr = reinterpret_cast<const char*>(data.data()); |
| 734 | // If there is a \0 character in the middle of the |data|, treat it as end |
| 735 | // of the string. Well-formed string extensions shouldn't contain it. |
| 736 | str->assign(cstr, strnlen(cstr, data.size())); |
| 737 | RTC_DCHECK(!str->empty()); |
| 738 | return true; |
| 739 | } |
| 740 | |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 741 | bool BaseRtpStringExtension::Write(rtc::ArrayView<uint8_t> data, |
| 742 | const std::string& str) { |
Niels Möller | d57efc1 | 2019-03-22 14:02:11 +0100 | [diff] [blame^] | 743 | if (str.size() > kMaxValueSizeBytes) { |
Johannes Kron | 78cdde3 | 2018-10-05 10:00:46 +0200 | [diff] [blame] | 744 | return false; |
| 745 | } |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 746 | RTC_DCHECK_EQ(data.size(), str.size()); |
Steve Anton | a3251dd | 2017-07-21 09:58:31 -0700 | [diff] [blame] | 747 | RTC_DCHECK_GE(str.size(), 1); |
Danil Chapovalov | 9bf3158 | 2018-06-18 13:48:20 +0200 | [diff] [blame] | 748 | memcpy(data.data(), str.data(), str.size()); |
Steve Anton | a3251dd | 2017-07-21 09:58:31 -0700 | [diff] [blame] | 749 | return true; |
| 750 | } |
| 751 | |
| 752 | // Constant declarations for string RTP header extension types. |
| 753 | |
danilchap | ef8d773 | 2017-04-19 02:59:48 -0700 | [diff] [blame] | 754 | constexpr RTPExtensionType RtpStreamId::kId; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 755 | constexpr const char RtpStreamId::kUri[]; |
danilchap | ef8d773 | 2017-04-19 02:59:48 -0700 | [diff] [blame] | 756 | |
danilchap | ef8d773 | 2017-04-19 02:59:48 -0700 | [diff] [blame] | 757 | constexpr RTPExtensionType RepairedRtpStreamId::kId; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 758 | constexpr const char RepairedRtpStreamId::kUri[]; |
danilchap | ef8d773 | 2017-04-19 02:59:48 -0700 | [diff] [blame] | 759 | |
Steve Anton | a3251dd | 2017-07-21 09:58:31 -0700 | [diff] [blame] | 760 | constexpr RTPExtensionType RtpMid::kId; |
Steve Anton | d14d9f7 | 2017-07-21 10:59:39 -0700 | [diff] [blame] | 761 | constexpr const char RtpMid::kUri[]; |
erikvarga | e6b1619 | 2017-05-11 02:36:32 -0700 | [diff] [blame] | 762 | |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 763 | } // namespace webrtc |