blob: c1eaf8c92e9d719b43c2a048335acf7bf72553f5 [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef MODULES_RTP_RTCP_SOURCE_RTP_HEADER_EXTENSIONS_H_
11#define MODULES_RTP_RTCP_SOURCE_RTP_HEADER_EXTENSIONS_H_
danilchap1edb7ab2016-04-20 05:25:10 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
pbosc7c26a02017-01-02 08:42:32 -080014#include <stdint.h>
danilchapef8d7732017-04-19 02:59:48 -070015#include <string>
pbosc7c26a02017-01-02 08:42:32 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/array_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "api/rtp_headers.h"
Johannes Kron09d65882018-11-27 14:36:41 +010019#include "api/video/color_space.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/video_content_type.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "api/video/video_frame_marking.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/video_rotation.h"
23#include "api/video/video_timing.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
danilchap1edb7ab2016-04-20 05:25:10 -070026
27namespace webrtc {
28
29class AbsoluteSendTime {
30 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +010031 using value_type = uint32_t;
danilchap1edb7ab2016-04-20 05:25:10 -070032 static constexpr RTPExtensionType kId = kRtpExtensionAbsoluteSendTime;
33 static constexpr uint8_t kValueSizeBytes = 3;
Steve Antond14d9f72017-07-21 10:59:39 -070034 static constexpr const char kUri[] =
danilchap6ba58d62016-11-09 05:46:39 -080035 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
danilchape2a01772016-10-28 07:08:58 -070036
danilchap978504e2017-04-06 01:03:53 -070037 static bool Parse(rtc::ArrayView<const uint8_t> data, uint32_t* time_24bits);
Danil Chapovalovf3ba6482017-06-12 15:43:55 +020038 static size_t ValueSize(uint32_t time_24bits) { return kValueSizeBytes; }
Danil Chapovalov9bf31582018-06-18 13:48:20 +020039 static bool Write(rtc::ArrayView<uint8_t> data, uint32_t time_24bits);
danilchapa897f262016-08-26 05:42:41 -070040
41 static constexpr uint32_t MsTo24Bits(int64_t time_ms) {
42 return static_cast<uint32_t>(((time_ms << 18) + 500) / 1000) & 0x00FFFFFF;
43 }
danilchap1edb7ab2016-04-20 05:25:10 -070044};
45
46class AudioLevel {
47 public:
48 static constexpr RTPExtensionType kId = kRtpExtensionAudioLevel;
49 static constexpr uint8_t kValueSizeBytes = 1;
Steve Antond14d9f72017-07-21 10:59:39 -070050 static constexpr const char kUri[] =
danilchap6ba58d62016-11-09 05:46:39 -080051 "urn:ietf:params:rtp-hdrext:ssrc-audio-level";
danilchape2a01772016-10-28 07:08:58 -070052
danilchap978504e2017-04-06 01:03:53 -070053 static bool Parse(rtc::ArrayView<const uint8_t> data,
danilchap1edb7ab2016-04-20 05:25:10 -070054 bool* voice_activity,
55 uint8_t* audio_level);
erikvargae6b16192017-05-11 02:36:32 -070056 static size_t ValueSize(bool voice_activity, uint8_t audio_level) {
57 return kValueSizeBytes;
58 }
Danil Chapovalov9bf31582018-06-18 13:48:20 +020059 static bool Write(rtc::ArrayView<uint8_t> data,
60 bool voice_activity,
61 uint8_t audio_level);
danilchap1edb7ab2016-04-20 05:25:10 -070062};
63
64class TransmissionOffset {
65 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +010066 using value_type = int32_t;
danilchap1edb7ab2016-04-20 05:25:10 -070067 static constexpr RTPExtensionType kId = kRtpExtensionTransmissionTimeOffset;
68 static constexpr uint8_t kValueSizeBytes = 3;
Steve Antond14d9f72017-07-21 10:59:39 -070069 static constexpr const char kUri[] = "urn:ietf:params:rtp-hdrext:toffset";
danilchape2a01772016-10-28 07:08:58 -070070
danilchap978504e2017-04-06 01:03:53 -070071 static bool Parse(rtc::ArrayView<const uint8_t> data, int32_t* rtp_time);
erikvargae6b16192017-05-11 02:36:32 -070072 static size_t ValueSize(int32_t rtp_time) { return kValueSizeBytes; }
Danil Chapovalov9bf31582018-06-18 13:48:20 +020073 static bool Write(rtc::ArrayView<uint8_t> data, int32_t rtp_time);
danilchap1edb7ab2016-04-20 05:25:10 -070074};
75
76class TransportSequenceNumber {
77 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +010078 using value_type = uint16_t;
danilchap1edb7ab2016-04-20 05:25:10 -070079 static constexpr RTPExtensionType kId = kRtpExtensionTransportSequenceNumber;
80 static constexpr uint8_t kValueSizeBytes = 2;
Steve Antond14d9f72017-07-21 10:59:39 -070081 static constexpr const char kUri[] =
danilchap6ba58d62016-11-09 05:46:39 -080082 "http://www.ietf.org/id/"
83 "draft-holmer-rmcat-transport-wide-cc-extensions-01";
danilchap978504e2017-04-06 01:03:53 -070084 static bool Parse(rtc::ArrayView<const uint8_t> data, uint16_t* value);
erikvargae6b16192017-05-11 02:36:32 -070085 static size_t ValueSize(uint16_t value) { return kValueSizeBytes; }
Danil Chapovalov9bf31582018-06-18 13:48:20 +020086 static bool Write(rtc::ArrayView<uint8_t> data, uint16_t value);
danilchap1edb7ab2016-04-20 05:25:10 -070087};
88
89class VideoOrientation {
90 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +010091 using value_type = VideoRotation;
danilchap1edb7ab2016-04-20 05:25:10 -070092 static constexpr RTPExtensionType kId = kRtpExtensionVideoRotation;
93 static constexpr uint8_t kValueSizeBytes = 1;
Steve Antond14d9f72017-07-21 10:59:39 -070094 static constexpr const char kUri[] = "urn:3gpp:video-orientation";
danilchape2a01772016-10-28 07:08:58 -070095
danilchap978504e2017-04-06 01:03:53 -070096 static bool Parse(rtc::ArrayView<const uint8_t> data, VideoRotation* value);
erikvargae6b16192017-05-11 02:36:32 -070097 static size_t ValueSize(VideoRotation) { return kValueSizeBytes; }
Danil Chapovalov9bf31582018-06-18 13:48:20 +020098 static bool Write(rtc::ArrayView<uint8_t> data, VideoRotation value);
danilchap978504e2017-04-06 01:03:53 -070099 static bool Parse(rtc::ArrayView<const uint8_t> data, uint8_t* value);
erikvargae6b16192017-05-11 02:36:32 -0700100 static size_t ValueSize(uint8_t value) { return kValueSizeBytes; }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200101 static bool Write(rtc::ArrayView<uint8_t> data, uint8_t value);
danilchap1edb7ab2016-04-20 05:25:10 -0700102};
103
Danil Chapovalov08b03512016-09-07 15:08:13 +0200104class PlayoutDelayLimits {
105 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100106 using value_type = PlayoutDelay;
Danil Chapovalov08b03512016-09-07 15:08:13 +0200107 static constexpr RTPExtensionType kId = kRtpExtensionPlayoutDelay;
108 static constexpr uint8_t kValueSizeBytes = 3;
Steve Antond14d9f72017-07-21 10:59:39 -0700109 static constexpr const char kUri[] =
danilchap6ba58d62016-11-09 05:46:39 -0800110 "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay";
danilchape2a01772016-10-28 07:08:58 -0700111
Danil Chapovalov08b03512016-09-07 15:08:13 +0200112 // Playout delay in milliseconds. A playout delay limit (min or max)
113 // has 12 bits allocated. This allows a range of 0-4095 values which
114 // translates to a range of 0-40950 in milliseconds.
115 static constexpr int kGranularityMs = 10;
116 // Maximum playout delay value in milliseconds.
117 static constexpr int kMaxMs = 0xfff * kGranularityMs; // 40950.
118
danilchap978504e2017-04-06 01:03:53 -0700119 static bool Parse(rtc::ArrayView<const uint8_t> data,
120 PlayoutDelay* playout_delay);
erikvargae6b16192017-05-11 02:36:32 -0700121 static size_t ValueSize(const PlayoutDelay&) {
122 return kValueSizeBytes;
123 }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200124 static bool Write(rtc::ArrayView<uint8_t> data,
125 const PlayoutDelay& playout_delay);
Danil Chapovalov08b03512016-09-07 15:08:13 +0200126};
127
ilnik00d802b2017-04-11 10:34:31 -0700128class VideoContentTypeExtension {
129 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100130 using value_type = VideoContentType;
ilnik00d802b2017-04-11 10:34:31 -0700131 static constexpr RTPExtensionType kId = kRtpExtensionVideoContentType;
132 static constexpr uint8_t kValueSizeBytes = 1;
Steve Antond14d9f72017-07-21 10:59:39 -0700133 static constexpr const char kUri[] =
ilnik00d802b2017-04-11 10:34:31 -0700134 "http://www.webrtc.org/experiments/rtp-hdrext/video-content-type";
135
136 static bool Parse(rtc::ArrayView<const uint8_t> data,
137 VideoContentType* content_type);
erikvargae6b16192017-05-11 02:36:32 -0700138 static size_t ValueSize(VideoContentType) {
139 return kValueSizeBytes;
140 }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200141 static bool Write(rtc::ArrayView<uint8_t> data,
142 VideoContentType content_type);
ilnik00d802b2017-04-11 10:34:31 -0700143};
144
ilnik04f4d122017-06-19 07:18:55 -0700145class VideoTimingExtension {
146 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100147 using value_type = VideoSendTiming;
ilnik04f4d122017-06-19 07:18:55 -0700148 static constexpr RTPExtensionType kId = kRtpExtensionVideoTiming;
sprangba050a62017-08-18 02:51:12 -0700149 static constexpr uint8_t kValueSizeBytes = 13;
Steve Antond14d9f72017-07-21 10:59:39 -0700150 static constexpr const char kUri[] =
ilnik04f4d122017-06-19 07:18:55 -0700151 "http://www.webrtc.org/experiments/rtp-hdrext/video-timing";
152
ilnik2edc6842017-07-06 03:06:50 -0700153 static bool Parse(rtc::ArrayView<const uint8_t> data,
154 VideoSendTiming* timing);
155 static size_t ValueSize(const VideoSendTiming&) { return kValueSizeBytes; }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200156 static bool Write(rtc::ArrayView<uint8_t> data,
157 const VideoSendTiming& timing);
ilnik04f4d122017-06-19 07:18:55 -0700158
159 static size_t ValueSize(uint16_t time_delta_ms, uint8_t idx) {
160 return kValueSizeBytes;
161 }
162 // Writes only single time delta to position idx.
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200163 static bool Write(rtc::ArrayView<uint8_t> data,
164 uint16_t time_delta_ms,
165 uint8_t idx);
ilnik04f4d122017-06-19 07:18:55 -0700166};
167
Johnny Leee0c8b232018-09-11 16:50:49 -0400168class FrameMarkingExtension {
169 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100170 using value_type = FrameMarking;
Johnny Leee0c8b232018-09-11 16:50:49 -0400171 static constexpr RTPExtensionType kId = kRtpExtensionFrameMarking;
172 static constexpr const char kUri[] =
173 "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07";
174
175 static bool Parse(rtc::ArrayView<const uint8_t> data,
176 FrameMarking* frame_marking);
177 static size_t ValueSize(const FrameMarking& frame_marking);
178 static bool Write(rtc::ArrayView<uint8_t> data,
179 const FrameMarking& frame_marking);
180
181 private:
182 static bool IsScalable(uint8_t temporal_id, uint8_t layer_id);
183};
184
Johannes Kron09d65882018-11-27 14:36:41 +0100185class ColorSpaceExtension {
Johannes Kronad1d9f02018-11-09 11:12:36 +0100186 public:
Johannes Kron09d65882018-11-27 14:36:41 +0100187 using value_type = ColorSpace;
188 static constexpr RTPExtensionType kId = kRtpExtensionColorSpace;
Johannes Kronad1d9f02018-11-09 11:12:36 +0100189 static constexpr uint8_t kValueSizeBytes = 30;
Johannes Kron09d65882018-11-27 14:36:41 +0100190 static constexpr uint8_t kValueSizeBytesWithoutHdrMetadata = 4;
Johannes Krond0b69a82018-12-03 14:18:53 +0100191 static constexpr const char kUri[] =
192 "http://www.webrtc.org/experiments/rtp-hdrext/color-space";
Johannes Kronad1d9f02018-11-09 11:12:36 +0100193
194 static bool Parse(rtc::ArrayView<const uint8_t> data,
Johannes Kron09d65882018-11-27 14:36:41 +0100195 ColorSpace* color_space);
196 static size_t ValueSize(const ColorSpace& color_space) {
197 return color_space.hdr_metadata() ? kValueSizeBytes
198 : kValueSizeBytesWithoutHdrMetadata;
199 }
Johannes Kronad1d9f02018-11-09 11:12:36 +0100200 static bool Write(rtc::ArrayView<uint8_t> data,
Johannes Kron09d65882018-11-27 14:36:41 +0100201 const ColorSpace& color_space);
Johannes Kronad1d9f02018-11-09 11:12:36 +0100202
203 private:
204 static constexpr int kChromaticityDenominator = 10000; // 0.0001 resolution.
205 static constexpr int kLuminanceMaxDenominator = 100; // 0.01 resolution.
206 static constexpr int kLuminanceMinDenominator = 10000; // 0.0001 resolution.
207 static size_t ParseChromaticity(const uint8_t* data,
208 HdrMasteringMetadata::Chromaticity* p);
209 static size_t ParseLuminance(const uint8_t* data, float* f, int denominator);
210 static size_t WriteChromaticity(uint8_t* data,
211 const HdrMasteringMetadata::Chromaticity& p);
212 static size_t WriteLuminance(uint8_t* data, float f, int denominator);
213};
214
Steve Antona3251dd2017-07-21 09:58:31 -0700215// Base extension class for RTP header extensions which are strings.
216// Subclasses must defined kId and kUri static constexpr members.
217class BaseRtpStringExtension {
218 public:
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100219 using value_type = std::string;
Steve Antonf0482ea2018-04-09 13:33:52 -0700220 // String RTP header extensions are limited to 16 bytes because it is the
221 // maximum length that can be encoded with one-byte header extensions.
222 static constexpr uint8_t kMaxValueSizeBytes = 16;
223
Steve Antona3251dd2017-07-21 09:58:31 -0700224 static bool Parse(rtc::ArrayView<const uint8_t> data,
225 StringRtpHeaderExtension* str);
226 static size_t ValueSize(const StringRtpHeaderExtension& str) {
227 return str.size();
228 }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200229 static bool Write(rtc::ArrayView<uint8_t> data,
230 const StringRtpHeaderExtension& str);
Steve Antona3251dd2017-07-21 09:58:31 -0700231
232 static bool Parse(rtc::ArrayView<const uint8_t> data, std::string* str);
233 static size_t ValueSize(const std::string& str) { return str.size(); }
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200234 static bool Write(rtc::ArrayView<uint8_t> data, const std::string& str);
Steve Antona3251dd2017-07-21 09:58:31 -0700235};
236
237class RtpStreamId : public BaseRtpStringExtension {
danilchapef8d7732017-04-19 02:59:48 -0700238 public:
239 static constexpr RTPExtensionType kId = kRtpExtensionRtpStreamId;
Steve Antond14d9f72017-07-21 10:59:39 -0700240 static constexpr const char kUri[] =
danilchapef8d7732017-04-19 02:59:48 -0700241 "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id";
danilchapef8d7732017-04-19 02:59:48 -0700242};
243
Steve Antona3251dd2017-07-21 09:58:31 -0700244class RepairedRtpStreamId : public BaseRtpStringExtension {
danilchapef8d7732017-04-19 02:59:48 -0700245 public:
246 static constexpr RTPExtensionType kId = kRtpExtensionRepairedRtpStreamId;
Steve Antond14d9f72017-07-21 10:59:39 -0700247 static constexpr const char kUri[] =
danilchapef8d7732017-04-19 02:59:48 -0700248 "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id";
Steve Antona3251dd2017-07-21 09:58:31 -0700249};
danilchapef8d7732017-04-19 02:59:48 -0700250
Steve Antona3251dd2017-07-21 09:58:31 -0700251class RtpMid : public BaseRtpStringExtension {
252 public:
253 static constexpr RTPExtensionType kId = kRtpExtensionMid;
Steve Antond14d9f72017-07-21 10:59:39 -0700254 static constexpr const char kUri[] = "urn:ietf:params:rtp-hdrext:sdes:mid";
danilchapef8d7732017-04-19 02:59:48 -0700255};
256
danilchap1edb7ab2016-04-20 05:25:10 -0700257} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200258#endif // MODULES_RTP_RTCP_SOURCE_RTP_HEADER_EXTENSIONS_H_