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