blob: fefe6c618f6dcfe6575d3d862efcd55733653e85 [file] [log] [blame]
danilchap1edb7ab2016-04-20 05:25:10 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
danilchap1edb7ab2016-04-20 05:25:10 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Johannes Kronad1d9f02018-11-09 11:12:36 +010015#include <cmath>
Johannes Kronc13f4be2018-12-12 09:52:53 +010016#include <limits>
Yves Gerey988cc082018-10-23 12:03:01 +020017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/rtp_rtcp/include/rtp_cvo.h"
19#include "modules/rtp_rtcp/source/byte_io.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020// TODO(bug:9855) Move kNoSpatialIdx from vp9_globals.h to common_constants
21#include "modules/video_coding/codecs/interface/common_constants.h"
Niels Möller834a5542019-09-23 10:31:16 +020022#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
danilchap1edb7ab2016-04-20 05:25:10 -070024
25namespace webrtc {
26// Absolute send time in RTP streams.
27//
28// The absolute send time is signaled to the receiver in-band using the
Johannes Kron07ba2b92018-09-26 13:33:35 +020029// general mechanism for RTP header extensions [RFC8285]. The payload
danilchap1edb7ab2016-04-20 05:25:10 -070030// of this extension (the transmitted value) is a 24-bit unsigned integer
31// containing the sender's current time in seconds as a fixed point number
32// with 18 bits fractional part.
33//
34// The form of the absolute send time extension block:
35//
36// 0 1 2 3
37// 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
38// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39// | ID | len=2 | absolute send time |
40// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchape2a01772016-10-28 07:08:58 -070041constexpr RTPExtensionType AbsoluteSendTime::kId;
42constexpr uint8_t AbsoluteSendTime::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -070043constexpr const char AbsoluteSendTime::kUri[];
danilchap1edb7ab2016-04-20 05:25:10 -070044
danilchap978504e2017-04-06 01:03:53 -070045bool AbsoluteSendTime::Parse(rtc::ArrayView<const uint8_t> data,
46 uint32_t* time_24bits) {
47 if (data.size() != 3)
48 return false;
49 *time_24bits = ByteReader<uint32_t, 3>::ReadBigEndian(data.data());
danilchap1edb7ab2016-04-20 05:25:10 -070050 return true;
51}
52
Danil Chapovalov9bf31582018-06-18 13:48:20 +020053bool AbsoluteSendTime::Write(rtc::ArrayView<uint8_t> data,
54 uint32_t time_24bits) {
55 RTC_DCHECK_EQ(data.size(), 3);
Danil Chapovalovf3ba6482017-06-12 15:43:55 +020056 RTC_DCHECK_LE(time_24bits, 0x00FFFFFF);
Danil Chapovalov9bf31582018-06-18 13:48:20 +020057 ByteWriter<uint32_t, 3>::WriteBigEndian(data.data(), time_24bits);
danilchap1edb7ab2016-04-20 05:25:10 -070058 return true;
59}
60
Chen Xingcd8a6e22019-07-01 10:56:51 +020061// Absolute Capture Time
62//
63// The Absolute Capture Time extension is used to stamp RTP packets with a NTP
64// timestamp showing when the first audio or video frame in a packet was
65// originally captured. The intent of this extension is to provide a way to
66// accomplish audio-to-video synchronization when RTCP-terminating intermediate
67// systems (e.g. mixers) are involved.
68//
69// Data layout of the shortened version of abs-capture-time:
70//
71// 0 1 2 3
72// 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
73// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74// | ID | len=7 | absolute capture timestamp (bit 0-23) |
75// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76// | absolute capture timestamp (bit 24-55) |
77// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78// | ... (56-63) |
79// +-+-+-+-+-+-+-+-+
80//
81// Data layout of the extended version of abs-capture-time:
82//
83// 0 1 2 3
84// 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
85// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86// | ID | len=15| absolute capture timestamp (bit 0-23) |
87// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88// | absolute capture timestamp (bit 24-55) |
89// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90// | ... (56-63) | estimated capture clock offset (bit 0-23) |
91// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92// | estimated capture clock offset (bit 24-55) |
93// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94// | ... (56-63) |
95// +-+-+-+-+-+-+-+-+
96constexpr RTPExtensionType AbsoluteCaptureTimeExtension::kId;
97constexpr uint8_t AbsoluteCaptureTimeExtension::kValueSizeBytes;
98constexpr uint8_t AbsoluteCaptureTimeExtension::
99 kValueSizeBytesWithoutEstimatedCaptureClockOffset;
100constexpr const char AbsoluteCaptureTimeExtension::kUri[];
101
102bool AbsoluteCaptureTimeExtension::Parse(rtc::ArrayView<const uint8_t> data,
103 AbsoluteCaptureTime* extension) {
104 if (data.size() != kValueSizeBytes &&
105 data.size() != kValueSizeBytesWithoutEstimatedCaptureClockOffset) {
106 return false;
107 }
108
109 extension->absolute_capture_timestamp =
110 ByteReader<uint64_t>::ReadBigEndian(data.data());
111
112 if (data.size() != kValueSizeBytesWithoutEstimatedCaptureClockOffset) {
113 extension->estimated_capture_clock_offset =
114 ByteReader<int64_t>::ReadBigEndian(data.data() + 8);
115 }
116
117 return true;
118}
119
120size_t AbsoluteCaptureTimeExtension::ValueSize(
121 const AbsoluteCaptureTime& extension) {
122 if (extension.estimated_capture_clock_offset != absl::nullopt) {
123 return kValueSizeBytes;
124 } else {
125 return kValueSizeBytesWithoutEstimatedCaptureClockOffset;
126 }
127}
128
129bool AbsoluteCaptureTimeExtension::Write(rtc::ArrayView<uint8_t> data,
130 const AbsoluteCaptureTime& extension) {
131 RTC_DCHECK_EQ(data.size(), ValueSize(extension));
132
133 ByteWriter<uint64_t>::WriteBigEndian(data.data(),
134 extension.absolute_capture_timestamp);
135
136 if (data.size() != kValueSizeBytesWithoutEstimatedCaptureClockOffset) {
137 ByteWriter<int64_t>::WriteBigEndian(
138 data.data() + 8, extension.estimated_capture_clock_offset.value());
139 }
140
141 return true;
142}
143
danilchap1edb7ab2016-04-20 05:25:10 -0700144// An RTP Header Extension for Client-to-Mixer Audio Level Indication
145//
Minyue Lid1ea4c92019-10-31 10:59:18 +0100146// https://tools.ietf.org/html/rfc6464
danilchap1edb7ab2016-04-20 05:25:10 -0700147//
148// The form of the audio level extension block:
149//
Minyue Lid1ea4c92019-10-31 10:59:18 +0100150// 0 1
151// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
152// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153// | ID | len=0 |V| level |
154// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155// Sample Audio Level Encoding Using the One-Byte Header Format
danilchap1edb7ab2016-04-20 05:25:10 -0700156//
Minyue Lid1ea4c92019-10-31 10:59:18 +0100157// 0 1 2
158// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
159// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160// | ID | len=1 |V| level |
161// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162// Sample Audio Level Encoding Using the Two-Byte Header Format
163
danilchape2a01772016-10-28 07:08:58 -0700164constexpr RTPExtensionType AudioLevel::kId;
165constexpr uint8_t AudioLevel::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700166constexpr const char AudioLevel::kUri[];
danilchap1edb7ab2016-04-20 05:25:10 -0700167
danilchap978504e2017-04-06 01:03:53 -0700168bool AudioLevel::Parse(rtc::ArrayView<const uint8_t> data,
danilchap1edb7ab2016-04-20 05:25:10 -0700169 bool* voice_activity,
170 uint8_t* audio_level) {
Minyue Lid1ea4c92019-10-31 10:59:18 +0100171 // One-byte and two-byte format share the same data definition.
danilchap978504e2017-04-06 01:03:53 -0700172 if (data.size() != 1)
173 return false;
danilchap1edb7ab2016-04-20 05:25:10 -0700174 *voice_activity = (data[0] & 0x80) != 0;
175 *audio_level = data[0] & 0x7F;
176 return true;
177}
178
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200179bool AudioLevel::Write(rtc::ArrayView<uint8_t> data,
danilchap1edb7ab2016-04-20 05:25:10 -0700180 bool voice_activity,
181 uint8_t audio_level) {
Minyue Lid1ea4c92019-10-31 10:59:18 +0100182 // One-byte and two-byte format share the same data definition.
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200183 RTC_DCHECK_EQ(data.size(), 1);
danilchap1edb7ab2016-04-20 05:25:10 -0700184 RTC_CHECK_LE(audio_level, 0x7f);
185 data[0] = (voice_activity ? 0x80 : 0x00) | audio_level;
186 return true;
187}
188
189// From RFC 5450: Transmission Time Offsets in RTP Streams.
190//
191// The transmission time is signaled to the receiver in-band using the
Johannes Kron07ba2b92018-09-26 13:33:35 +0200192// general mechanism for RTP header extensions [RFC8285]. The payload
danilchap1edb7ab2016-04-20 05:25:10 -0700193// of this extension (the transmitted value) is a 24-bit signed integer.
194// When added to the RTP timestamp of the packet, it represents the
195// "effective" RTP transmission time of the packet, on the RTP
196// timescale.
197//
198// The form of the transmission offset extension block:
199//
200// 0 1 2 3
201// 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
202// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203// | ID | len=2 | transmission offset |
204// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchape2a01772016-10-28 07:08:58 -0700205constexpr RTPExtensionType TransmissionOffset::kId;
206constexpr uint8_t TransmissionOffset::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700207constexpr const char TransmissionOffset::kUri[];
danilchap1edb7ab2016-04-20 05:25:10 -0700208
danilchap978504e2017-04-06 01:03:53 -0700209bool TransmissionOffset::Parse(rtc::ArrayView<const uint8_t> data,
210 int32_t* rtp_time) {
211 if (data.size() != 3)
212 return false;
213 *rtp_time = ByteReader<int32_t, 3>::ReadBigEndian(data.data());
danilchap1edb7ab2016-04-20 05:25:10 -0700214 return true;
215}
216
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200217bool TransmissionOffset::Write(rtc::ArrayView<uint8_t> data, int32_t rtp_time) {
218 RTC_DCHECK_EQ(data.size(), 3);
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200219 RTC_DCHECK_LE(rtp_time, 0x00ffffff);
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200220 ByteWriter<int32_t, 3>::WriteBigEndian(data.data(), rtp_time);
danilchap1edb7ab2016-04-20 05:25:10 -0700221 return true;
222}
223
Johannes Kron54047be2019-02-21 14:09:20 +0000224// TransportSequenceNumber
225//
danilchap1edb7ab2016-04-20 05:25:10 -0700226// 0 1 2
227// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
228// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kron54047be2019-02-21 14:09:20 +0000229// | ID | L=1 |transport-wide sequence number |
danilchap1edb7ab2016-04-20 05:25:10 -0700230// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchape2a01772016-10-28 07:08:58 -0700231constexpr RTPExtensionType TransportSequenceNumber::kId;
232constexpr uint8_t TransportSequenceNumber::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700233constexpr const char TransportSequenceNumber::kUri[];
danilchap1edb7ab2016-04-20 05:25:10 -0700234
danilchap978504e2017-04-06 01:03:53 -0700235bool TransportSequenceNumber::Parse(rtc::ArrayView<const uint8_t> data,
Johannes Kron54047be2019-02-21 14:09:20 +0000236 uint16_t* transport_sequence_number) {
237 if (data.size() != kValueSizeBytes)
danilchap978504e2017-04-06 01:03:53 -0700238 return false;
Johannes Kron54047be2019-02-21 14:09:20 +0000239 *transport_sequence_number = ByteReader<uint16_t>::ReadBigEndian(data.data());
danilchap1edb7ab2016-04-20 05:25:10 -0700240 return true;
241}
242
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200243bool TransportSequenceNumber::Write(rtc::ArrayView<uint8_t> data,
Johannes Kron54047be2019-02-21 14:09:20 +0000244 uint16_t transport_sequence_number) {
245 RTC_DCHECK_EQ(data.size(), ValueSize(transport_sequence_number));
246 ByteWriter<uint16_t>::WriteBigEndian(data.data(), transport_sequence_number);
247 return true;
248}
249
250// TransportSequenceNumberV2
251//
252// In addition to the format used for TransportSequencNumber, V2 also supports
253// the following packet format where two extra bytes are used to specify that
254// the sender requests immediate feedback.
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 | L=3 |transport-wide sequence number |T| seq count |
259// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
260// |seq count cont.|
261// +-+-+-+-+-+-+-+-+
262//
263// The bit |T| determines whether the feedback should include timing information
Johannes Kron0da25a12019-03-06 09:34:13 +0100264// or not and |seq_count| determines how many packets the feedback packet should
265// cover including the current packet. If |seq_count| is zero no feedback is
266// requested.
Johannes Kron54047be2019-02-21 14:09:20 +0000267constexpr RTPExtensionType TransportSequenceNumberV2::kId;
Johannes Kron0da25a12019-03-06 09:34:13 +0100268constexpr uint8_t TransportSequenceNumberV2::kValueSizeBytes;
269constexpr uint8_t
270 TransportSequenceNumberV2::kValueSizeBytesWithoutFeedbackRequest;
Johannes Kron54047be2019-02-21 14:09:20 +0000271constexpr const char TransportSequenceNumberV2::kUri[];
272constexpr uint16_t TransportSequenceNumberV2::kIncludeTimestampsBit;
273
274bool TransportSequenceNumberV2::Parse(
275 rtc::ArrayView<const uint8_t> data,
276 uint16_t* transport_sequence_number,
277 absl::optional<FeedbackRequest>* feedback_request) {
Johannes Kron0da25a12019-03-06 09:34:13 +0100278 if (data.size() != kValueSizeBytes &&
279 data.size() != kValueSizeBytesWithoutFeedbackRequest)
Johannes Kron54047be2019-02-21 14:09:20 +0000280 return false;
281
282 *transport_sequence_number = ByteReader<uint16_t>::ReadBigEndian(data.data());
283
Johannes Kron0da25a12019-03-06 09:34:13 +0100284 *feedback_request = absl::nullopt;
285 if (data.size() == kValueSizeBytes) {
Johannes Kron54047be2019-02-21 14:09:20 +0000286 uint16_t feedback_request_raw =
287 ByteReader<uint16_t>::ReadBigEndian(data.data() + 2);
288 bool include_timestamps =
289 (feedback_request_raw & kIncludeTimestampsBit) != 0;
290 uint16_t sequence_count = feedback_request_raw & ~kIncludeTimestampsBit;
Johannes Kron0da25a12019-03-06 09:34:13 +0100291
292 // If |sequence_count| is zero no feedback is requested.
293 if (sequence_count != 0) {
294 *feedback_request = {include_timestamps, sequence_count};
295 }
Johannes Kron54047be2019-02-21 14:09:20 +0000296 }
297 return true;
298}
299
300bool TransportSequenceNumberV2::Write(
301 rtc::ArrayView<uint8_t> data,
302 uint16_t transport_sequence_number,
303 const absl::optional<FeedbackRequest>& feedback_request) {
304 RTC_DCHECK_EQ(data.size(),
305 ValueSize(transport_sequence_number, feedback_request));
306
307 ByteWriter<uint16_t>::WriteBigEndian(data.data(), transport_sequence_number);
308
309 if (feedback_request) {
310 RTC_DCHECK_GE(feedback_request->sequence_count, 0);
311 RTC_DCHECK_LT(feedback_request->sequence_count, kIncludeTimestampsBit);
312 uint16_t feedback_request_raw =
313 feedback_request->sequence_count |
314 (feedback_request->include_timestamps ? kIncludeTimestampsBit : 0);
315 ByteWriter<uint16_t>::WriteBigEndian(data.data() + 2, feedback_request_raw);
316 }
danilchap1edb7ab2016-04-20 05:25:10 -0700317 return true;
318}
319
320// Coordination of Video Orientation in RTP streams.
321//
322// Coordination of Video Orientation consists in signaling of the current
323// orientation of the image captured on the sender side to the receiver for
324// appropriate rendering and displaying.
325//
326// 0 1
327// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
328// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
329// | ID | len=0 |0 0 0 0 C F R R|
330// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
danilchape2a01772016-10-28 07:08:58 -0700331constexpr RTPExtensionType VideoOrientation::kId;
332constexpr uint8_t VideoOrientation::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700333constexpr const char VideoOrientation::kUri[];
danilchap1edb7ab2016-04-20 05:25:10 -0700334
danilchap978504e2017-04-06 01:03:53 -0700335bool VideoOrientation::Parse(rtc::ArrayView<const uint8_t> data,
336 VideoRotation* rotation) {
337 if (data.size() != 1)
338 return false;
magjed71eb61c2016-09-08 03:24:58 -0700339 *rotation = ConvertCVOByteToVideoRotation(data[0]);
danilchap1edb7ab2016-04-20 05:25:10 -0700340 return true;
341}
342
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200343bool VideoOrientation::Write(rtc::ArrayView<uint8_t> data,
344 VideoRotation rotation) {
345 RTC_DCHECK_EQ(data.size(), 1);
danilchap1edb7ab2016-04-20 05:25:10 -0700346 data[0] = ConvertVideoRotationToCVOByte(rotation);
347 return true;
348}
349
danilchap978504e2017-04-06 01:03:53 -0700350bool VideoOrientation::Parse(rtc::ArrayView<const uint8_t> data,
351 uint8_t* value) {
352 if (data.size() != 1)
353 return false;
danilchap1edb7ab2016-04-20 05:25:10 -0700354 *value = data[0];
355 return true;
356}
357
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200358bool VideoOrientation::Write(rtc::ArrayView<uint8_t> data, uint8_t value) {
359 RTC_DCHECK_EQ(data.size(), 1);
danilchap1edb7ab2016-04-20 05:25:10 -0700360 data[0] = value;
361 return true;
362}
Danil Chapovalov08b03512016-09-07 15:08:13 +0200363
364// 0 1 2 3
365// 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
366// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367// | ID | len=2 | MIN delay | MAX delay |
368// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
369constexpr RTPExtensionType PlayoutDelayLimits::kId;
370constexpr uint8_t PlayoutDelayLimits::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700371constexpr const char PlayoutDelayLimits::kUri[];
Danil Chapovalov08b03512016-09-07 15:08:13 +0200372
danilchap978504e2017-04-06 01:03:53 -0700373bool PlayoutDelayLimits::Parse(rtc::ArrayView<const uint8_t> data,
Danil Chapovalov08b03512016-09-07 15:08:13 +0200374 PlayoutDelay* playout_delay) {
375 RTC_DCHECK(playout_delay);
danilchap978504e2017-04-06 01:03:53 -0700376 if (data.size() != 3)
377 return false;
378 uint32_t raw = ByteReader<uint32_t, 3>::ReadBigEndian(data.data());
Danil Chapovalov08b03512016-09-07 15:08:13 +0200379 uint16_t min_raw = (raw >> 12);
380 uint16_t max_raw = (raw & 0xfff);
381 if (min_raw > max_raw)
382 return false;
383 playout_delay->min_ms = min_raw * kGranularityMs;
384 playout_delay->max_ms = max_raw * kGranularityMs;
385 return true;
386}
387
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200388bool PlayoutDelayLimits::Write(rtc::ArrayView<uint8_t> data,
Danil Chapovalov08b03512016-09-07 15:08:13 +0200389 const PlayoutDelay& playout_delay) {
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200390 RTC_DCHECK_EQ(data.size(), 3);
Danil Chapovalov08b03512016-09-07 15:08:13 +0200391 RTC_DCHECK_LE(0, playout_delay.min_ms);
392 RTC_DCHECK_LE(playout_delay.min_ms, playout_delay.max_ms);
393 RTC_DCHECK_LE(playout_delay.max_ms, kMaxMs);
394 // Convert MS to value to be sent on extension header.
395 uint32_t min_delay = playout_delay.min_ms / kGranularityMs;
396 uint32_t max_delay = playout_delay.max_ms / kGranularityMs;
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200397 ByteWriter<uint32_t, 3>::WriteBigEndian(data.data(),
398 (min_delay << 12) | max_delay);
Danil Chapovalov08b03512016-09-07 15:08:13 +0200399 return true;
400}
401
ilnik00d802b2017-04-11 10:34:31 -0700402// Video Content Type.
403//
404// E.g. default video or screenshare.
405//
406// 0 1
407// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
408// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
409// | ID | len=0 | Content type |
410// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
411constexpr RTPExtensionType VideoContentTypeExtension::kId;
412constexpr uint8_t VideoContentTypeExtension::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700413constexpr const char VideoContentTypeExtension::kUri[];
ilnik00d802b2017-04-11 10:34:31 -0700414
415bool VideoContentTypeExtension::Parse(rtc::ArrayView<const uint8_t> data,
416 VideoContentType* content_type) {
417 if (data.size() == 1 &&
ilnik6d5b4d62017-08-30 03:32:14 -0700418 videocontenttypehelpers::IsValidContentType(data[0])) {
ilnik00d802b2017-04-11 10:34:31 -0700419 *content_type = static_cast<VideoContentType>(data[0]);
420 return true;
421 }
422 return false;
423}
424
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200425bool VideoContentTypeExtension::Write(rtc::ArrayView<uint8_t> data,
ilnik00d802b2017-04-11 10:34:31 -0700426 VideoContentType content_type) {
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200427 RTC_DCHECK_EQ(data.size(), 1);
ilnik00d802b2017-04-11 10:34:31 -0700428 data[0] = static_cast<uint8_t>(content_type);
429 return true;
430}
431
ilnik04f4d122017-06-19 07:18:55 -0700432// Video Timing.
433// 6 timestamps in milliseconds counted from capture time stored in rtp header:
434// encode start/finish, packetization complete, pacer exit and reserved for
sprangba050a62017-08-18 02:51:12 -0700435// modification by the network modification. |flags| is a bitmask and has the
436// following allowed values:
437// 0 = Valid data, but no flags available (backwards compatibility)
438// 1 = Frame marked as timing frame due to cyclic timer.
439// 2 = Frame marked as timing frame due to size being outside limit.
440// 255 = Invalid. The whole timing frame extension should be ignored.
441//
ilnik04f4d122017-06-19 07:18:55 -0700442// 0 1 2 3
Johannes Krond0b69a82018-12-03 14:18:53 +0100443// 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
444// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
445// | ID | len=12| flags | encode start ms delta |
446// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
447// | encode finish ms delta | packetizer finish ms delta |
448// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
449// | pacer exit ms delta | network timestamp ms delta |
450// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
sprangba050a62017-08-18 02:51:12 -0700451// | network2 timestamp ms delta |
452// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
ilnik04f4d122017-06-19 07:18:55 -0700453
454constexpr RTPExtensionType VideoTimingExtension::kId;
455constexpr uint8_t VideoTimingExtension::kValueSizeBytes;
Steve Antond14d9f72017-07-21 10:59:39 -0700456constexpr const char VideoTimingExtension::kUri[];
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100457constexpr uint8_t VideoTimingExtension::kFlagsOffset;
458constexpr uint8_t VideoTimingExtension::kEncodeStartDeltaOffset;
459constexpr uint8_t VideoTimingExtension::kEncodeFinishDeltaOffset;
460constexpr uint8_t VideoTimingExtension::kPacketizationFinishDeltaOffset;
461constexpr uint8_t VideoTimingExtension::kPacerExitDeltaOffset;
462constexpr uint8_t VideoTimingExtension::kNetworkTimestampDeltaOffset;
463constexpr uint8_t VideoTimingExtension::kNetwork2TimestampDeltaOffset;
ilnik04f4d122017-06-19 07:18:55 -0700464
465bool VideoTimingExtension::Parse(rtc::ArrayView<const uint8_t> data,
ilnik2edc6842017-07-06 03:06:50 -0700466 VideoSendTiming* timing) {
ilnik04f4d122017-06-19 07:18:55 -0700467 RTC_DCHECK(timing);
sprangba050a62017-08-18 02:51:12 -0700468 // TODO(sprang): Deprecate support for old wire format.
469 ptrdiff_t off = 0;
470 switch (data.size()) {
471 case kValueSizeBytes - 1:
472 timing->flags = 0;
473 off = 1; // Old wire format without the flags field.
474 break;
475 case kValueSizeBytes:
476 timing->flags = ByteReader<uint8_t>::ReadBigEndian(data.data());
477 break;
478 default:
479 return false;
480 }
481
482 timing->encode_start_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100483 data.data() + kEncodeStartDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700484 timing->encode_finish_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100485 data.data() + kEncodeFinishDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700486 timing->packetization_finish_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100487 data.data() + kPacketizationFinishDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700488 timing->pacer_exit_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100489 data.data() + kPacerExitDeltaOffset - off);
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100490 timing->network_timestamp_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100491 data.data() + kNetworkTimestampDeltaOffset - off);
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100492 timing->network2_timestamp_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100493 data.data() + kNetwork2TimestampDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700494 return true;
495}
496
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200497bool VideoTimingExtension::Write(rtc::ArrayView<uint8_t> data,
498 const VideoSendTiming& timing) {
499 RTC_DCHECK_EQ(data.size(), 1 + 2 * 6);
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100500 ByteWriter<uint8_t>::WriteBigEndian(data.data() + kFlagsOffset, timing.flags);
501 ByteWriter<uint16_t>::WriteBigEndian(data.data() + kEncodeStartDeltaOffset,
502 timing.encode_start_delta_ms);
503 ByteWriter<uint16_t>::WriteBigEndian(data.data() + kEncodeFinishDeltaOffset,
504 timing.encode_finish_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700505 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100506 data.data() + kPacketizationFinishDeltaOffset,
ilnik04f4d122017-06-19 07:18:55 -0700507 timing.packetization_finish_delta_ms);
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100508 ByteWriter<uint16_t>::WriteBigEndian(data.data() + kPacerExitDeltaOffset,
509 timing.pacer_exit_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700510 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100511 data.data() + kNetworkTimestampDeltaOffset,
Danil Chapovalovf0cc8142017-10-31 17:59:39 +0100512 timing.network_timestamp_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700513 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalovdf2c6012020-01-17 15:37:31 +0100514 data.data() + kNetwork2TimestampDeltaOffset,
Danil Chapovalovf0cc8142017-10-31 17:59:39 +0100515 timing.network2_timestamp_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700516 return true;
517}
518
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200519bool VideoTimingExtension::Write(rtc::ArrayView<uint8_t> data,
ilnik04f4d122017-06-19 07:18:55 -0700520 uint16_t time_delta_ms,
sprangba050a62017-08-18 02:51:12 -0700521 uint8_t offset) {
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200522 RTC_DCHECK_GE(data.size(), offset + 2);
Danil Chapovalovf0cc8142017-10-31 17:59:39 +0100523 RTC_DCHECK_LE(offset, kValueSizeBytes - sizeof(uint16_t));
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200524 ByteWriter<uint16_t>::WriteBigEndian(data.data() + offset, time_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700525 return true;
526}
527
Johnny Leee0c8b232018-09-11 16:50:49 -0400528// Frame Marking.
529//
530// Meta-information about an RTP stream outside the encrypted media payload,
531// useful for an RTP switch to do codec-agnostic selective forwarding
532// without decrypting the payload.
533//
534// For non-scalable streams:
535// 0 1
536// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
537// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538// | ID | L = 0 |S|E|I|D|0 0 0 0|
539// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540//
541// For scalable streams:
542// 0 1 2 3
543// 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
544// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
545// | ID | L = 2 |S|E|I|D|B| TID | LID | TL0PICIDX |
546// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
547
548constexpr RTPExtensionType FrameMarkingExtension::kId;
549constexpr const char FrameMarkingExtension::kUri[];
550
551bool FrameMarkingExtension::IsScalable(uint8_t temporal_id, uint8_t layer_id) {
552 return temporal_id != kNoTemporalIdx || layer_id != kNoSpatialIdx;
553}
554
555bool FrameMarkingExtension::Parse(rtc::ArrayView<const uint8_t> data,
556 FrameMarking* frame_marking) {
557 RTC_DCHECK(frame_marking);
558
559 if (data.size() != 1 && data.size() != 3)
560 return false;
561
562 frame_marking->start_of_frame = (data[0] & 0x80) != 0;
563 frame_marking->end_of_frame = (data[0] & 0x40) != 0;
564 frame_marking->independent_frame = (data[0] & 0x20) != 0;
565 frame_marking->discardable_frame = (data[0] & 0x10) != 0;
566
567 if (data.size() == 3) {
568 frame_marking->base_layer_sync = (data[0] & 0x08) != 0;
569 frame_marking->temporal_id = data[0] & 0x7;
570 frame_marking->layer_id = data[1];
571 frame_marking->tl0_pic_idx = data[2];
572 } else {
573 // non-scalable
574 frame_marking->base_layer_sync = false;
575 frame_marking->temporal_id = kNoTemporalIdx;
576 frame_marking->layer_id = kNoSpatialIdx;
577 frame_marking->tl0_pic_idx = 0;
578 }
579 return true;
580}
581
582size_t FrameMarkingExtension::ValueSize(const FrameMarking& frame_marking) {
583 if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id))
584 return 3;
585 else
586 return 1;
587}
588
589bool FrameMarkingExtension::Write(rtc::ArrayView<uint8_t> data,
590 const FrameMarking& frame_marking) {
591 RTC_DCHECK_GE(data.size(), 1);
592 RTC_CHECK_LE(frame_marking.temporal_id, 0x07);
593 data[0] = frame_marking.start_of_frame ? 0x80 : 0x00;
594 data[0] |= frame_marking.end_of_frame ? 0x40 : 0x00;
595 data[0] |= frame_marking.independent_frame ? 0x20 : 0x00;
596 data[0] |= frame_marking.discardable_frame ? 0x10 : 0x00;
597
598 if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id)) {
599 RTC_DCHECK_EQ(data.size(), 3);
600 data[0] |= frame_marking.base_layer_sync ? 0x08 : 0x00;
601 data[0] |= frame_marking.temporal_id & 0x07;
602 data[1] = frame_marking.layer_id;
603 data[2] = frame_marking.tl0_pic_idx;
604 }
605 return true;
606}
607
Johannes Kron09d65882018-11-27 14:36:41 +0100608// Color space including HDR metadata as an optional field.
Johannes Kronad1d9f02018-11-09 11:12:36 +0100609//
Johannes Krond0b69a82018-12-03 14:18:53 +0100610// RTP header extension to carry color space information and optionally HDR
611// metadata. The float values in the HDR metadata struct are upscaled by a
612// static factor and transmitted as unsigned integers.
Johannes Kronad1d9f02018-11-09 11:12:36 +0100613//
Johannes Krond0b69a82018-12-03 14:18:53 +0100614// Data layout of color space with HDR metadata (two-byte RTP header extension)
Johannes Kronad1d9f02018-11-09 11:12:36 +0100615// 0 1 2 3
Johannes Krond0b69a82018-12-03 14:18:53 +0100616// 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
617// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100618// | ID | length=28 | primaries | transfer |
Johannes Krond0b69a82018-12-03 14:18:53 +0100619// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100620// | matrix |range+chr.sit. | luminance_max |
Johannes Krond0b69a82018-12-03 14:18:53 +0100621// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100622// | luminance_min | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100623// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100624// |primary_r.x and .y | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100625// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100626// |primary_g.x and .y | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100627// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100628// |primary_b.x and .y | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100629// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100630// |white.x and .y | max_content_light_level |
Johannes Krond0b69a82018-12-03 14:18:53 +0100631// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100632// | max_frame_average_light_level |
633// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kron09d65882018-11-27 14:36:41 +0100634//
Johannes Krond0b69a82018-12-03 14:18:53 +0100635// Data layout of color space w/o HDR metadata (one-byte RTP header extension)
Johannes Kron09d65882018-11-27 14:36:41 +0100636// 0 1 2 3
Johannes Krond0b69a82018-12-03 14:18:53 +0100637// 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
638// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100639// | ID | L = 3 | primaries | transfer | matrix |
Johannes Krond0b69a82018-12-03 14:18:53 +0100640// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100641// |range+chr.sit. |
Johannes Krond0b69a82018-12-03 14:18:53 +0100642// +-+-+-+-+-+-+-+-+
Johannes Kronad1d9f02018-11-09 11:12:36 +0100643
Johannes Kron09d65882018-11-27 14:36:41 +0100644constexpr RTPExtensionType ColorSpaceExtension::kId;
645constexpr uint8_t ColorSpaceExtension::kValueSizeBytes;
646constexpr const char ColorSpaceExtension::kUri[];
647
648bool ColorSpaceExtension::Parse(rtc::ArrayView<const uint8_t> data,
649 ColorSpace* color_space) {
650 RTC_DCHECK(color_space);
651 if (data.size() != kValueSizeBytes &&
652 data.size() != kValueSizeBytesWithoutHdrMetadata)
Johannes Kronad1d9f02018-11-09 11:12:36 +0100653 return false;
654
655 size_t offset = 0;
Johannes Kron09d65882018-11-27 14:36:41 +0100656 // Read color space information.
Johannes Kronc13f4be2018-12-12 09:52:53 +0100657 if (!color_space->set_primaries_from_uint8(data[offset++]))
Johannes Kron09d65882018-11-27 14:36:41 +0100658 return false;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100659 if (!color_space->set_transfer_from_uint8(data[offset++]))
Johannes Kron09d65882018-11-27 14:36:41 +0100660 return false;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100661 if (!color_space->set_matrix_from_uint8(data[offset++]))
Johannes Kron09d65882018-11-27 14:36:41 +0100662 return false;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100663
664 uint8_t range_and_chroma_siting = data[offset++];
665 if (!color_space->set_range_from_uint8((range_and_chroma_siting >> 4) & 0x03))
666 return false;
667 if (!color_space->set_chroma_siting_horizontal_from_uint8(
668 (range_and_chroma_siting >> 2) & 0x03))
669 return false;
670 if (!color_space->set_chroma_siting_vertical_from_uint8(
671 range_and_chroma_siting & 0x03))
Johannes Kron09d65882018-11-27 14:36:41 +0100672 return false;
673
674 // Read HDR metadata if it exists, otherwise clear it.
675 if (data.size() == kValueSizeBytesWithoutHdrMetadata) {
676 color_space->set_hdr_metadata(nullptr);
677 } else {
678 HdrMetadata hdr_metadata;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100679 offset += ParseHdrMetadata(data.subview(offset), &hdr_metadata);
680 if (!hdr_metadata.Validate())
681 return false;
Johannes Kron09d65882018-11-27 14:36:41 +0100682 color_space->set_hdr_metadata(&hdr_metadata);
683 }
684 RTC_DCHECK_EQ(ValueSize(*color_space), offset);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100685 return true;
686}
687
Johannes Kron09d65882018-11-27 14:36:41 +0100688bool ColorSpaceExtension::Write(rtc::ArrayView<uint8_t> data,
689 const ColorSpace& color_space) {
Johannes Krond0b69a82018-12-03 14:18:53 +0100690 RTC_DCHECK_EQ(data.size(), ValueSize(color_space));
Johannes Kronad1d9f02018-11-09 11:12:36 +0100691 size_t offset = 0;
Johannes Kron09d65882018-11-27 14:36:41 +0100692 // Write color space information.
Johannes Kronc13f4be2018-12-12 09:52:53 +0100693 data[offset++] = static_cast<uint8_t>(color_space.primaries());
694 data[offset++] = static_cast<uint8_t>(color_space.transfer());
695 data[offset++] = static_cast<uint8_t>(color_space.matrix());
696 data[offset++] = CombineRangeAndChromaSiting(
697 color_space.range(), color_space.chroma_siting_horizontal(),
698 color_space.chroma_siting_vertical());
Johannes Kronad1d9f02018-11-09 11:12:36 +0100699
Johannes Kron09d65882018-11-27 14:36:41 +0100700 // Write HDR metadata if it exists.
701 if (color_space.hdr_metadata()) {
Johannes Kronc13f4be2018-12-12 09:52:53 +0100702 offset +=
703 WriteHdrMetadata(data.subview(offset), *color_space.hdr_metadata());
Johannes Kron09d65882018-11-27 14:36:41 +0100704 }
705 RTC_DCHECK_EQ(ValueSize(color_space), offset);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100706 return true;
707}
708
Johannes Kronc13f4be2018-12-12 09:52:53 +0100709// Combines range and chroma siting into one byte with the following bit layout:
710// bits 0-1 Chroma siting vertical.
711// 2-3 Chroma siting horizontal.
712// 4-5 Range.
713// 6-7 Unused.
714uint8_t ColorSpaceExtension::CombineRangeAndChromaSiting(
715 ColorSpace::RangeID range,
716 ColorSpace::ChromaSiting chroma_siting_horizontal,
717 ColorSpace::ChromaSiting chroma_siting_vertical) {
718 RTC_DCHECK_LE(static_cast<uint8_t>(range), 3);
719 RTC_DCHECK_LE(static_cast<uint8_t>(chroma_siting_horizontal), 3);
720 RTC_DCHECK_LE(static_cast<uint8_t>(chroma_siting_vertical), 3);
721 return (static_cast<uint8_t>(range) << 4) |
722 (static_cast<uint8_t>(chroma_siting_horizontal) << 2) |
723 static_cast<uint8_t>(chroma_siting_vertical);
724}
725
726size_t ColorSpaceExtension::ParseHdrMetadata(rtc::ArrayView<const uint8_t> data,
727 HdrMetadata* hdr_metadata) {
728 RTC_DCHECK_EQ(data.size(),
729 kValueSizeBytes - kValueSizeBytesWithoutHdrMetadata);
730 size_t offset = 0;
731 offset += ParseLuminance(data.data() + offset,
732 &hdr_metadata->mastering_metadata.luminance_max,
733 kLuminanceMaxDenominator);
734 offset += ParseLuminance(data.data() + offset,
735 &hdr_metadata->mastering_metadata.luminance_min,
736 kLuminanceMinDenominator);
737 offset += ParseChromaticity(data.data() + offset,
738 &hdr_metadata->mastering_metadata.primary_r);
739 offset += ParseChromaticity(data.data() + offset,
740 &hdr_metadata->mastering_metadata.primary_g);
741 offset += ParseChromaticity(data.data() + offset,
742 &hdr_metadata->mastering_metadata.primary_b);
743 offset += ParseChromaticity(data.data() + offset,
744 &hdr_metadata->mastering_metadata.white_point);
745 hdr_metadata->max_content_light_level =
746 ByteReader<uint16_t>::ReadBigEndian(data.data() + offset);
747 offset += 2;
748 hdr_metadata->max_frame_average_light_level =
749 ByteReader<uint16_t>::ReadBigEndian(data.data() + offset);
750 offset += 2;
751 return offset;
752}
753
Johannes Kron09d65882018-11-27 14:36:41 +0100754size_t ColorSpaceExtension::ParseChromaticity(
Johannes Kronad1d9f02018-11-09 11:12:36 +0100755 const uint8_t* data,
756 HdrMasteringMetadata::Chromaticity* p) {
757 uint16_t chromaticity_x_scaled = ByteReader<uint16_t>::ReadBigEndian(data);
758 uint16_t chromaticity_y_scaled =
759 ByteReader<uint16_t>::ReadBigEndian(data + 2);
760 p->x = static_cast<float>(chromaticity_x_scaled) / kChromaticityDenominator;
761 p->y = static_cast<float>(chromaticity_y_scaled) / kChromaticityDenominator;
762 return 4; // Return number of bytes read.
763}
764
Johannes Kron09d65882018-11-27 14:36:41 +0100765size_t ColorSpaceExtension::ParseLuminance(const uint8_t* data,
766 float* f,
767 int denominator) {
Johannes Kronc13f4be2018-12-12 09:52:53 +0100768 uint16_t luminance_scaled = ByteReader<uint16_t>::ReadBigEndian(data);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100769 *f = static_cast<float>(luminance_scaled) / denominator;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100770 return 2; // Return number of bytes read.
771}
772
773size_t ColorSpaceExtension::WriteHdrMetadata(rtc::ArrayView<uint8_t> data,
774 const HdrMetadata& hdr_metadata) {
775 RTC_DCHECK_EQ(data.size(),
776 kValueSizeBytes - kValueSizeBytesWithoutHdrMetadata);
777 RTC_DCHECK(hdr_metadata.Validate());
778 size_t offset = 0;
779 offset += WriteLuminance(data.data() + offset,
780 hdr_metadata.mastering_metadata.luminance_max,
781 kLuminanceMaxDenominator);
782 offset += WriteLuminance(data.data() + offset,
783 hdr_metadata.mastering_metadata.luminance_min,
784 kLuminanceMinDenominator);
785 offset += WriteChromaticity(data.data() + offset,
786 hdr_metadata.mastering_metadata.primary_r);
787 offset += WriteChromaticity(data.data() + offset,
788 hdr_metadata.mastering_metadata.primary_g);
789 offset += WriteChromaticity(data.data() + offset,
790 hdr_metadata.mastering_metadata.primary_b);
791 offset += WriteChromaticity(data.data() + offset,
792 hdr_metadata.mastering_metadata.white_point);
793
794 ByteWriter<uint16_t>::WriteBigEndian(data.data() + offset,
795 hdr_metadata.max_content_light_level);
796 offset += 2;
797 ByteWriter<uint16_t>::WriteBigEndian(
798 data.data() + offset, hdr_metadata.max_frame_average_light_level);
799 offset += 2;
800 return offset;
Johannes Kronad1d9f02018-11-09 11:12:36 +0100801}
802
Johannes Kron09d65882018-11-27 14:36:41 +0100803size_t ColorSpaceExtension::WriteChromaticity(
Johannes Kronad1d9f02018-11-09 11:12:36 +0100804 uint8_t* data,
805 const HdrMasteringMetadata::Chromaticity& p) {
806 RTC_DCHECK_GE(p.x, 0.0f);
Johannes Kronc13f4be2018-12-12 09:52:53 +0100807 RTC_DCHECK_LE(p.x, 1.0f);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100808 RTC_DCHECK_GE(p.y, 0.0f);
Johannes Kronc13f4be2018-12-12 09:52:53 +0100809 RTC_DCHECK_LE(p.y, 1.0f);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100810 ByteWriter<uint16_t>::WriteBigEndian(
811 data, std::round(p.x * kChromaticityDenominator));
812 ByteWriter<uint16_t>::WriteBigEndian(
813 data + 2, std::round(p.y * kChromaticityDenominator));
814 return 4; // Return number of bytes written.
815}
816
Johannes Kron09d65882018-11-27 14:36:41 +0100817size_t ColorSpaceExtension::WriteLuminance(uint8_t* data,
818 float f,
819 int denominator) {
Johannes Kronad1d9f02018-11-09 11:12:36 +0100820 RTC_DCHECK_GE(f, 0.0f);
Johannes Kronc13f4be2018-12-12 09:52:53 +0100821 float upscaled_value = f * denominator;
822 RTC_DCHECK_LE(upscaled_value, std::numeric_limits<uint16_t>::max());
823 ByteWriter<uint16_t>::WriteBigEndian(data, std::round(upscaled_value));
824 return 2; // Return number of bytes written.
Johannes Kronad1d9f02018-11-09 11:12:36 +0100825}
826
Steve Antona3251dd2017-07-21 09:58:31 -0700827bool BaseRtpStringExtension::Parse(rtc::ArrayView<const uint8_t> data,
Steve Antona3251dd2017-07-21 09:58:31 -0700828 std::string* str) {
829 if (data.empty() || data[0] == 0) // Valid string extension can't be empty.
830 return false;
831 const char* cstr = reinterpret_cast<const char*>(data.data());
832 // If there is a \0 character in the middle of the |data|, treat it as end
833 // of the string. Well-formed string extensions shouldn't contain it.
834 str->assign(cstr, strnlen(cstr, data.size()));
835 RTC_DCHECK(!str->empty());
836 return true;
837}
838
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200839bool BaseRtpStringExtension::Write(rtc::ArrayView<uint8_t> data,
840 const std::string& str) {
Niels Möllerd57efc12019-03-22 14:02:11 +0100841 if (str.size() > kMaxValueSizeBytes) {
Johannes Kron78cdde32018-10-05 10:00:46 +0200842 return false;
843 }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200844 RTC_DCHECK_EQ(data.size(), str.size());
Steve Antona3251dd2017-07-21 09:58:31 -0700845 RTC_DCHECK_GE(str.size(), 1);
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200846 memcpy(data.data(), str.data(), str.size());
Steve Antona3251dd2017-07-21 09:58:31 -0700847 return true;
848}
849
850// Constant declarations for string RTP header extension types.
851
danilchapef8d7732017-04-19 02:59:48 -0700852constexpr RTPExtensionType RtpStreamId::kId;
Steve Antond14d9f72017-07-21 10:59:39 -0700853constexpr const char RtpStreamId::kUri[];
danilchapef8d7732017-04-19 02:59:48 -0700854
danilchapef8d7732017-04-19 02:59:48 -0700855constexpr RTPExtensionType RepairedRtpStreamId::kId;
Steve Antond14d9f72017-07-21 10:59:39 -0700856constexpr const char RepairedRtpStreamId::kUri[];
danilchapef8d7732017-04-19 02:59:48 -0700857
Steve Antona3251dd2017-07-21 09:58:31 -0700858constexpr RTPExtensionType RtpMid::kId;
Steve Antond14d9f72017-07-21 10:59:39 -0700859constexpr const char RtpMid::kUri[];
erikvargae6b16192017-05-11 02:36:32 -0700860
Minyue Licae27792019-11-29 16:18:59 +0100861// An RTP Header Extension for Inband Comfort Noise
862//
863// The form of the audio level extension block:
864//
865// 0 1
866// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
867// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
868// | ID | len=0 |N| level |
869// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
870// Sample Audio Level Encoding Using the One-Byte Header Format
871//
872// 0 1 2
873// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
874// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
875// | ID | len=1 |N| level |
876// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
877// Sample Audio Level Encoding Using the Two-Byte Header Format
878
879constexpr RTPExtensionType InbandComfortNoiseExtension::kId;
880constexpr uint8_t InbandComfortNoiseExtension::kValueSizeBytes;
881constexpr const char InbandComfortNoiseExtension::kUri[];
882
883bool InbandComfortNoiseExtension::Parse(rtc::ArrayView<const uint8_t> data,
884 absl::optional<uint8_t>* level) {
885 if (data.size() != kValueSizeBytes)
886 return false;
887 *level = (data[0] & 0b1000'0000) != 0
888 ? absl::nullopt
889 : absl::make_optional(data[0] & 0b0111'1111);
890 return true;
891}
892
893bool InbandComfortNoiseExtension::Write(rtc::ArrayView<uint8_t> data,
894 absl::optional<uint8_t> level) {
895 RTC_DCHECK_EQ(data.size(), kValueSizeBytes);
896 data[0] = 0b0000'0000;
897 if (level) {
898 if (*level > 127) {
899 return false;
900 }
901 data[0] = 0b1000'0000 | *level;
902 }
903 return true;
904}
905
danilchap1edb7ab2016-04-20 05:25:10 -0700906} // namespace webrtc