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