blob: 800226504a37f5a4b6116f78cf0c562f978a4484 [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[];
ilnik04f4d122017-06-19 07:18:55 -0700457
458bool VideoTimingExtension::Parse(rtc::ArrayView<const uint8_t> data,
ilnik2edc6842017-07-06 03:06:50 -0700459 VideoSendTiming* timing) {
ilnik04f4d122017-06-19 07:18:55 -0700460 RTC_DCHECK(timing);
sprangba050a62017-08-18 02:51:12 -0700461 // TODO(sprang): Deprecate support for old wire format.
462 ptrdiff_t off = 0;
463 switch (data.size()) {
464 case kValueSizeBytes - 1:
465 timing->flags = 0;
466 off = 1; // Old wire format without the flags field.
467 break;
468 case kValueSizeBytes:
469 timing->flags = ByteReader<uint8_t>::ReadBigEndian(data.data());
470 break;
471 default:
472 return false;
473 }
474
475 timing->encode_start_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
476 data.data() + VideoSendTiming::kEncodeStartDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700477 timing->encode_finish_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
sprangba050a62017-08-18 02:51:12 -0700478 data.data() + VideoSendTiming::kEncodeFinishDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700479 timing->packetization_finish_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
sprangba050a62017-08-18 02:51:12 -0700480 data.data() + VideoSendTiming::kPacketizationFinishDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700481 timing->pacer_exit_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
sprangba050a62017-08-18 02:51:12 -0700482 data.data() + VideoSendTiming::kPacerExitDeltaOffset - off);
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100483 timing->network_timestamp_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
sprangba050a62017-08-18 02:51:12 -0700484 data.data() + VideoSendTiming::kNetworkTimestampDeltaOffset - off);
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100485 timing->network2_timestamp_delta_ms = ByteReader<uint16_t>::ReadBigEndian(
sprangba050a62017-08-18 02:51:12 -0700486 data.data() + VideoSendTiming::kNetwork2TimestampDeltaOffset - off);
ilnik04f4d122017-06-19 07:18:55 -0700487 return true;
488}
489
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200490bool VideoTimingExtension::Write(rtc::ArrayView<uint8_t> data,
491 const VideoSendTiming& timing) {
492 RTC_DCHECK_EQ(data.size(), 1 + 2 * 6);
493 ByteWriter<uint8_t>::WriteBigEndian(
494 data.data() + VideoSendTiming::kFlagsOffset, timing.flags);
ilnik04f4d122017-06-19 07:18:55 -0700495 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200496 data.data() + VideoSendTiming::kEncodeStartDeltaOffset,
sprangba050a62017-08-18 02:51:12 -0700497 timing.encode_start_delta_ms);
498 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200499 data.data() + VideoSendTiming::kEncodeFinishDeltaOffset,
ilnik04f4d122017-06-19 07:18:55 -0700500 timing.encode_finish_delta_ms);
501 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200502 data.data() + VideoSendTiming::kPacketizationFinishDeltaOffset,
ilnik04f4d122017-06-19 07:18:55 -0700503 timing.packetization_finish_delta_ms);
504 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200505 data.data() + VideoSendTiming::kPacerExitDeltaOffset,
ilnik2edc6842017-07-06 03:06:50 -0700506 timing.pacer_exit_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700507 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200508 data.data() + VideoSendTiming::kNetworkTimestampDeltaOffset,
Danil Chapovalovf0cc8142017-10-31 17:59:39 +0100509 timing.network_timestamp_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700510 ByteWriter<uint16_t>::WriteBigEndian(
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200511 data.data() + VideoSendTiming::kNetwork2TimestampDeltaOffset,
Danil Chapovalovf0cc8142017-10-31 17:59:39 +0100512 timing.network2_timestamp_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700513 return true;
514}
515
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200516bool VideoTimingExtension::Write(rtc::ArrayView<uint8_t> data,
ilnik04f4d122017-06-19 07:18:55 -0700517 uint16_t time_delta_ms,
sprangba050a62017-08-18 02:51:12 -0700518 uint8_t offset) {
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200519 RTC_DCHECK_GE(data.size(), offset + 2);
Danil Chapovalovf0cc8142017-10-31 17:59:39 +0100520 RTC_DCHECK_LE(offset, kValueSizeBytes - sizeof(uint16_t));
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200521 ByteWriter<uint16_t>::WriteBigEndian(data.data() + offset, time_delta_ms);
ilnik04f4d122017-06-19 07:18:55 -0700522 return true;
523}
524
Johnny Leee0c8b232018-09-11 16:50:49 -0400525// Frame Marking.
526//
527// Meta-information about an RTP stream outside the encrypted media payload,
528// useful for an RTP switch to do codec-agnostic selective forwarding
529// without decrypting the payload.
530//
531// For non-scalable streams:
532// 0 1
533// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
534// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
535// | ID | L = 0 |S|E|I|D|0 0 0 0|
536// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537//
538// For scalable streams:
539// 0 1 2 3
540// 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
541// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542// | ID | L = 2 |S|E|I|D|B| TID | LID | TL0PICIDX |
543// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544
545constexpr RTPExtensionType FrameMarkingExtension::kId;
546constexpr const char FrameMarkingExtension::kUri[];
547
548bool FrameMarkingExtension::IsScalable(uint8_t temporal_id, uint8_t layer_id) {
549 return temporal_id != kNoTemporalIdx || layer_id != kNoSpatialIdx;
550}
551
552bool FrameMarkingExtension::Parse(rtc::ArrayView<const uint8_t> data,
553 FrameMarking* frame_marking) {
554 RTC_DCHECK(frame_marking);
555
556 if (data.size() != 1 && data.size() != 3)
557 return false;
558
559 frame_marking->start_of_frame = (data[0] & 0x80) != 0;
560 frame_marking->end_of_frame = (data[0] & 0x40) != 0;
561 frame_marking->independent_frame = (data[0] & 0x20) != 0;
562 frame_marking->discardable_frame = (data[0] & 0x10) != 0;
563
564 if (data.size() == 3) {
565 frame_marking->base_layer_sync = (data[0] & 0x08) != 0;
566 frame_marking->temporal_id = data[0] & 0x7;
567 frame_marking->layer_id = data[1];
568 frame_marking->tl0_pic_idx = data[2];
569 } else {
570 // non-scalable
571 frame_marking->base_layer_sync = false;
572 frame_marking->temporal_id = kNoTemporalIdx;
573 frame_marking->layer_id = kNoSpatialIdx;
574 frame_marking->tl0_pic_idx = 0;
575 }
576 return true;
577}
578
579size_t FrameMarkingExtension::ValueSize(const FrameMarking& frame_marking) {
580 if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id))
581 return 3;
582 else
583 return 1;
584}
585
586bool FrameMarkingExtension::Write(rtc::ArrayView<uint8_t> data,
587 const FrameMarking& frame_marking) {
588 RTC_DCHECK_GE(data.size(), 1);
589 RTC_CHECK_LE(frame_marking.temporal_id, 0x07);
590 data[0] = frame_marking.start_of_frame ? 0x80 : 0x00;
591 data[0] |= frame_marking.end_of_frame ? 0x40 : 0x00;
592 data[0] |= frame_marking.independent_frame ? 0x20 : 0x00;
593 data[0] |= frame_marking.discardable_frame ? 0x10 : 0x00;
594
595 if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id)) {
596 RTC_DCHECK_EQ(data.size(), 3);
597 data[0] |= frame_marking.base_layer_sync ? 0x08 : 0x00;
598 data[0] |= frame_marking.temporal_id & 0x07;
599 data[1] = frame_marking.layer_id;
600 data[2] = frame_marking.tl0_pic_idx;
601 }
602 return true;
603}
604
Johannes Kron09d65882018-11-27 14:36:41 +0100605// Color space including HDR metadata as an optional field.
Johannes Kronad1d9f02018-11-09 11:12:36 +0100606//
Johannes Krond0b69a82018-12-03 14:18:53 +0100607// RTP header extension to carry color space information and optionally HDR
608// metadata. The float values in the HDR metadata struct are upscaled by a
609// static factor and transmitted as unsigned integers.
Johannes Kronad1d9f02018-11-09 11:12:36 +0100610//
Johannes Krond0b69a82018-12-03 14:18:53 +0100611// Data layout of color space with HDR metadata (two-byte RTP header extension)
Johannes Kronad1d9f02018-11-09 11:12:36 +0100612// 0 1 2 3
Johannes Krond0b69a82018-12-03 14:18:53 +0100613// 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
614// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100615// | ID | length=28 | primaries | transfer |
Johannes Krond0b69a82018-12-03 14:18:53 +0100616// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100617// | matrix |range+chr.sit. | luminance_max |
Johannes Krond0b69a82018-12-03 14:18:53 +0100618// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100619// | luminance_min | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100620// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100621// |primary_r.x and .y | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100622// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100623// |primary_g.x and .y | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100624// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100625// |primary_b.x and .y | mastering_metadata.|
Johannes Krond0b69a82018-12-03 14:18:53 +0100626// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100627// |white.x and .y | max_content_light_level |
Johannes Krond0b69a82018-12-03 14:18:53 +0100628// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100629// | max_frame_average_light_level |
630// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kron09d65882018-11-27 14:36:41 +0100631//
Johannes Krond0b69a82018-12-03 14:18:53 +0100632// Data layout of color space w/o HDR metadata (one-byte RTP header extension)
Johannes Kron09d65882018-11-27 14:36:41 +0100633// 0 1 2 3
Johannes Krond0b69a82018-12-03 14:18:53 +0100634// 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
635// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100636// | ID | L = 3 | primaries | transfer | matrix |
Johannes Krond0b69a82018-12-03 14:18:53 +0100637// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Johannes Kronc13f4be2018-12-12 09:52:53 +0100638// |range+chr.sit. |
Johannes Krond0b69a82018-12-03 14:18:53 +0100639// +-+-+-+-+-+-+-+-+
Johannes Kronad1d9f02018-11-09 11:12:36 +0100640
Johannes Kron09d65882018-11-27 14:36:41 +0100641constexpr RTPExtensionType ColorSpaceExtension::kId;
642constexpr uint8_t ColorSpaceExtension::kValueSizeBytes;
643constexpr const char ColorSpaceExtension::kUri[];
644
645bool ColorSpaceExtension::Parse(rtc::ArrayView<const uint8_t> data,
646 ColorSpace* color_space) {
647 RTC_DCHECK(color_space);
648 if (data.size() != kValueSizeBytes &&
649 data.size() != kValueSizeBytesWithoutHdrMetadata)
Johannes Kronad1d9f02018-11-09 11:12:36 +0100650 return false;
651
652 size_t offset = 0;
Johannes Kron09d65882018-11-27 14:36:41 +0100653 // Read color space information.
Johannes Kronc13f4be2018-12-12 09:52:53 +0100654 if (!color_space->set_primaries_from_uint8(data[offset++]))
Johannes Kron09d65882018-11-27 14:36:41 +0100655 return false;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100656 if (!color_space->set_transfer_from_uint8(data[offset++]))
Johannes Kron09d65882018-11-27 14:36:41 +0100657 return false;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100658 if (!color_space->set_matrix_from_uint8(data[offset++]))
Johannes Kron09d65882018-11-27 14:36:41 +0100659 return false;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100660
661 uint8_t range_and_chroma_siting = data[offset++];
662 if (!color_space->set_range_from_uint8((range_and_chroma_siting >> 4) & 0x03))
663 return false;
664 if (!color_space->set_chroma_siting_horizontal_from_uint8(
665 (range_and_chroma_siting >> 2) & 0x03))
666 return false;
667 if (!color_space->set_chroma_siting_vertical_from_uint8(
668 range_and_chroma_siting & 0x03))
Johannes Kron09d65882018-11-27 14:36:41 +0100669 return false;
670
671 // Read HDR metadata if it exists, otherwise clear it.
672 if (data.size() == kValueSizeBytesWithoutHdrMetadata) {
673 color_space->set_hdr_metadata(nullptr);
674 } else {
675 HdrMetadata hdr_metadata;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100676 offset += ParseHdrMetadata(data.subview(offset), &hdr_metadata);
677 if (!hdr_metadata.Validate())
678 return false;
Johannes Kron09d65882018-11-27 14:36:41 +0100679 color_space->set_hdr_metadata(&hdr_metadata);
680 }
681 RTC_DCHECK_EQ(ValueSize(*color_space), offset);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100682 return true;
683}
684
Johannes Kron09d65882018-11-27 14:36:41 +0100685bool ColorSpaceExtension::Write(rtc::ArrayView<uint8_t> data,
686 const ColorSpace& color_space) {
Johannes Krond0b69a82018-12-03 14:18:53 +0100687 RTC_DCHECK_EQ(data.size(), ValueSize(color_space));
Johannes Kronad1d9f02018-11-09 11:12:36 +0100688 size_t offset = 0;
Johannes Kron09d65882018-11-27 14:36:41 +0100689 // Write color space information.
Johannes Kronc13f4be2018-12-12 09:52:53 +0100690 data[offset++] = static_cast<uint8_t>(color_space.primaries());
691 data[offset++] = static_cast<uint8_t>(color_space.transfer());
692 data[offset++] = static_cast<uint8_t>(color_space.matrix());
693 data[offset++] = CombineRangeAndChromaSiting(
694 color_space.range(), color_space.chroma_siting_horizontal(),
695 color_space.chroma_siting_vertical());
Johannes Kronad1d9f02018-11-09 11:12:36 +0100696
Johannes Kron09d65882018-11-27 14:36:41 +0100697 // Write HDR metadata if it exists.
698 if (color_space.hdr_metadata()) {
Johannes Kronc13f4be2018-12-12 09:52:53 +0100699 offset +=
700 WriteHdrMetadata(data.subview(offset), *color_space.hdr_metadata());
Johannes Kron09d65882018-11-27 14:36:41 +0100701 }
702 RTC_DCHECK_EQ(ValueSize(color_space), offset);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100703 return true;
704}
705
Johannes Kronc13f4be2018-12-12 09:52:53 +0100706// Combines range and chroma siting into one byte with the following bit layout:
707// bits 0-1 Chroma siting vertical.
708// 2-3 Chroma siting horizontal.
709// 4-5 Range.
710// 6-7 Unused.
711uint8_t ColorSpaceExtension::CombineRangeAndChromaSiting(
712 ColorSpace::RangeID range,
713 ColorSpace::ChromaSiting chroma_siting_horizontal,
714 ColorSpace::ChromaSiting chroma_siting_vertical) {
715 RTC_DCHECK_LE(static_cast<uint8_t>(range), 3);
716 RTC_DCHECK_LE(static_cast<uint8_t>(chroma_siting_horizontal), 3);
717 RTC_DCHECK_LE(static_cast<uint8_t>(chroma_siting_vertical), 3);
718 return (static_cast<uint8_t>(range) << 4) |
719 (static_cast<uint8_t>(chroma_siting_horizontal) << 2) |
720 static_cast<uint8_t>(chroma_siting_vertical);
721}
722
723size_t ColorSpaceExtension::ParseHdrMetadata(rtc::ArrayView<const uint8_t> data,
724 HdrMetadata* hdr_metadata) {
725 RTC_DCHECK_EQ(data.size(),
726 kValueSizeBytes - kValueSizeBytesWithoutHdrMetadata);
727 size_t offset = 0;
728 offset += ParseLuminance(data.data() + offset,
729 &hdr_metadata->mastering_metadata.luminance_max,
730 kLuminanceMaxDenominator);
731 offset += ParseLuminance(data.data() + offset,
732 &hdr_metadata->mastering_metadata.luminance_min,
733 kLuminanceMinDenominator);
734 offset += ParseChromaticity(data.data() + offset,
735 &hdr_metadata->mastering_metadata.primary_r);
736 offset += ParseChromaticity(data.data() + offset,
737 &hdr_metadata->mastering_metadata.primary_g);
738 offset += ParseChromaticity(data.data() + offset,
739 &hdr_metadata->mastering_metadata.primary_b);
740 offset += ParseChromaticity(data.data() + offset,
741 &hdr_metadata->mastering_metadata.white_point);
742 hdr_metadata->max_content_light_level =
743 ByteReader<uint16_t>::ReadBigEndian(data.data() + offset);
744 offset += 2;
745 hdr_metadata->max_frame_average_light_level =
746 ByteReader<uint16_t>::ReadBigEndian(data.data() + offset);
747 offset += 2;
748 return offset;
749}
750
Johannes Kron09d65882018-11-27 14:36:41 +0100751size_t ColorSpaceExtension::ParseChromaticity(
Johannes Kronad1d9f02018-11-09 11:12:36 +0100752 const uint8_t* data,
753 HdrMasteringMetadata::Chromaticity* p) {
754 uint16_t chromaticity_x_scaled = ByteReader<uint16_t>::ReadBigEndian(data);
755 uint16_t chromaticity_y_scaled =
756 ByteReader<uint16_t>::ReadBigEndian(data + 2);
757 p->x = static_cast<float>(chromaticity_x_scaled) / kChromaticityDenominator;
758 p->y = static_cast<float>(chromaticity_y_scaled) / kChromaticityDenominator;
759 return 4; // Return number of bytes read.
760}
761
Johannes Kron09d65882018-11-27 14:36:41 +0100762size_t ColorSpaceExtension::ParseLuminance(const uint8_t* data,
763 float* f,
764 int denominator) {
Johannes Kronc13f4be2018-12-12 09:52:53 +0100765 uint16_t luminance_scaled = ByteReader<uint16_t>::ReadBigEndian(data);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100766 *f = static_cast<float>(luminance_scaled) / denominator;
Johannes Kronc13f4be2018-12-12 09:52:53 +0100767 return 2; // Return number of bytes read.
768}
769
770size_t ColorSpaceExtension::WriteHdrMetadata(rtc::ArrayView<uint8_t> data,
771 const HdrMetadata& hdr_metadata) {
772 RTC_DCHECK_EQ(data.size(),
773 kValueSizeBytes - kValueSizeBytesWithoutHdrMetadata);
774 RTC_DCHECK(hdr_metadata.Validate());
775 size_t offset = 0;
776 offset += WriteLuminance(data.data() + offset,
777 hdr_metadata.mastering_metadata.luminance_max,
778 kLuminanceMaxDenominator);
779 offset += WriteLuminance(data.data() + offset,
780 hdr_metadata.mastering_metadata.luminance_min,
781 kLuminanceMinDenominator);
782 offset += WriteChromaticity(data.data() + offset,
783 hdr_metadata.mastering_metadata.primary_r);
784 offset += WriteChromaticity(data.data() + offset,
785 hdr_metadata.mastering_metadata.primary_g);
786 offset += WriteChromaticity(data.data() + offset,
787 hdr_metadata.mastering_metadata.primary_b);
788 offset += WriteChromaticity(data.data() + offset,
789 hdr_metadata.mastering_metadata.white_point);
790
791 ByteWriter<uint16_t>::WriteBigEndian(data.data() + offset,
792 hdr_metadata.max_content_light_level);
793 offset += 2;
794 ByteWriter<uint16_t>::WriteBigEndian(
795 data.data() + offset, hdr_metadata.max_frame_average_light_level);
796 offset += 2;
797 return offset;
Johannes Kronad1d9f02018-11-09 11:12:36 +0100798}
799
Johannes Kron09d65882018-11-27 14:36:41 +0100800size_t ColorSpaceExtension::WriteChromaticity(
Johannes Kronad1d9f02018-11-09 11:12:36 +0100801 uint8_t* data,
802 const HdrMasteringMetadata::Chromaticity& p) {
803 RTC_DCHECK_GE(p.x, 0.0f);
Johannes Kronc13f4be2018-12-12 09:52:53 +0100804 RTC_DCHECK_LE(p.x, 1.0f);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100805 RTC_DCHECK_GE(p.y, 0.0f);
Johannes Kronc13f4be2018-12-12 09:52:53 +0100806 RTC_DCHECK_LE(p.y, 1.0f);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100807 ByteWriter<uint16_t>::WriteBigEndian(
808 data, std::round(p.x * kChromaticityDenominator));
809 ByteWriter<uint16_t>::WriteBigEndian(
810 data + 2, std::round(p.y * kChromaticityDenominator));
811 return 4; // Return number of bytes written.
812}
813
Johannes Kron09d65882018-11-27 14:36:41 +0100814size_t ColorSpaceExtension::WriteLuminance(uint8_t* data,
815 float f,
816 int denominator) {
Johannes Kronad1d9f02018-11-09 11:12:36 +0100817 RTC_DCHECK_GE(f, 0.0f);
Johannes Kronc13f4be2018-12-12 09:52:53 +0100818 float upscaled_value = f * denominator;
819 RTC_DCHECK_LE(upscaled_value, std::numeric_limits<uint16_t>::max());
820 ByteWriter<uint16_t>::WriteBigEndian(data, std::round(upscaled_value));
821 return 2; // Return number of bytes written.
Johannes Kronad1d9f02018-11-09 11:12:36 +0100822}
823
Steve Antona3251dd2017-07-21 09:58:31 -0700824bool BaseRtpStringExtension::Parse(rtc::ArrayView<const uint8_t> data,
Steve Antona3251dd2017-07-21 09:58:31 -0700825 std::string* str) {
826 if (data.empty() || data[0] == 0) // Valid string extension can't be empty.
827 return false;
828 const char* cstr = reinterpret_cast<const char*>(data.data());
829 // If there is a \0 character in the middle of the |data|, treat it as end
830 // of the string. Well-formed string extensions shouldn't contain it.
831 str->assign(cstr, strnlen(cstr, data.size()));
832 RTC_DCHECK(!str->empty());
833 return true;
834}
835
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200836bool BaseRtpStringExtension::Write(rtc::ArrayView<uint8_t> data,
837 const std::string& str) {
Niels Möllerd57efc12019-03-22 14:02:11 +0100838 if (str.size() > kMaxValueSizeBytes) {
Johannes Kron78cdde32018-10-05 10:00:46 +0200839 return false;
840 }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200841 RTC_DCHECK_EQ(data.size(), str.size());
Steve Antona3251dd2017-07-21 09:58:31 -0700842 RTC_DCHECK_GE(str.size(), 1);
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200843 memcpy(data.data(), str.data(), str.size());
Steve Antona3251dd2017-07-21 09:58:31 -0700844 return true;
845}
846
847// Constant declarations for string RTP header extension types.
848
danilchapef8d7732017-04-19 02:59:48 -0700849constexpr RTPExtensionType RtpStreamId::kId;
Steve Antond14d9f72017-07-21 10:59:39 -0700850constexpr const char RtpStreamId::kUri[];
danilchapef8d7732017-04-19 02:59:48 -0700851
danilchapef8d7732017-04-19 02:59:48 -0700852constexpr RTPExtensionType RepairedRtpStreamId::kId;
Steve Antond14d9f72017-07-21 10:59:39 -0700853constexpr const char RepairedRtpStreamId::kUri[];
danilchapef8d7732017-04-19 02:59:48 -0700854
Steve Antona3251dd2017-07-21 09:58:31 -0700855constexpr RTPExtensionType RtpMid::kId;
Steve Antond14d9f72017-07-21 10:59:39 -0700856constexpr const char RtpMid::kUri[];
erikvargae6b16192017-05-11 02:36:32 -0700857
Minyue Licae27792019-11-29 16:18:59 +0100858// An RTP Header Extension for Inband Comfort Noise
859//
860// The form of the audio level extension block:
861//
862// 0 1
863// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
864// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
865// | ID | len=0 |N| level |
866// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
867// Sample Audio Level Encoding Using the One-Byte Header Format
868//
869// 0 1 2
870// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
871// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
872// | ID | len=1 |N| level |
873// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
874// Sample Audio Level Encoding Using the Two-Byte Header Format
875
876constexpr RTPExtensionType InbandComfortNoiseExtension::kId;
877constexpr uint8_t InbandComfortNoiseExtension::kValueSizeBytes;
878constexpr const char InbandComfortNoiseExtension::kUri[];
879
880bool InbandComfortNoiseExtension::Parse(rtc::ArrayView<const uint8_t> data,
881 absl::optional<uint8_t>* level) {
882 if (data.size() != kValueSizeBytes)
883 return false;
884 *level = (data[0] & 0b1000'0000) != 0
885 ? absl::nullopt
886 : absl::make_optional(data[0] & 0b0111'1111);
887 return true;
888}
889
890bool InbandComfortNoiseExtension::Write(rtc::ArrayView<uint8_t> data,
891 absl::optional<uint8_t> level) {
892 RTC_DCHECK_EQ(data.size(), kValueSizeBytes);
893 data[0] = 0b0000'0000;
894 if (level) {
895 if (*level > 127) {
896 return false;
897 }
898 data[0] = 0b1000'0000 | *level;
899 }
900 return true;
901}
902
danilchap1edb7ab2016-04-20 05:25:10 -0700903} // namespace webrtc