1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_STREAMING_RTP_DEFINES_H_
6 #define CAST_STREAMING_RTP_DEFINES_H_
7 
8 #include <stdint.h>
9 
10 #include "cast/streaming/constants.h"
11 
12 namespace openscreen {
13 namespace cast {
14 
15 // Note: Cast Streaming uses a subset of the messages in the RTP/RTCP
16 // specification, but also adds some of its own extensions. See:
17 // https://tools.ietf.org/html/rfc3550
18 
19 // Uniquely identifies one packet within a frame. These are sequence numbers,
20 // starting at 0. Each Cast RTP packet also includes the "last ID" so that a
21 // receiver always knows the range of valid FramePacketIds for a given frame.
22 using FramePacketId = uint16_t;
23 
24 // A special FramePacketId value meant to represent "all packets lost" in Cast
25 // RTCP Feedback messages.
26 constexpr FramePacketId kAllPacketsLost = 0xffff;
27 constexpr FramePacketId kMaxAllowedFramePacketId = kAllPacketsLost - 1;
28 
29 // The maximum size of any RTP or RTCP packet, in bytes. The calculation below
30 // is: Standard Ethernet MTU bytes minus IP header bytes minus UDP header bytes.
31 // The remainder is available for RTP/RTCP packet data (header + payload).
32 //
33 // A nice explanation of this: https://jvns.ca/blog/2017/02/07/mtu/
34 //
35 // Constants are provided here for UDP over IPv4 and IPv6 on Ethernet. Other
36 // transports and network mediums will need additional consideration, alternate
37 // calculations. Note that MTU is dynamic, depending on the path the packets
38 // take between two endpoints (the 1500 here is just a commonly-used value for
39 // LAN Ethernet).
40 constexpr int kMaxRtpPacketSizeForIpv4UdpOnEthernet = 1500 - 20 - 8;
41 constexpr int kMaxRtpPacketSizeForIpv6UdpOnEthernet = 1500 - 40 - 8;
42 
43 // The Cast RTP packet header:
44 //
45 //  0                   1                   2                   3
46 //  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
47 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ^
48 // |V=2|P|X| CC=0  |M|      PT     |      sequence number          | |
49 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+RTP
50 // +                         RTP timestamp                         |Spec
51 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
52 // +         synchronization source (SSRC) identifier              | v
53 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
54 // |K|R| EXT count |  FID          |              PID              | ^
55 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+Cast
56 // |             Max PID           |  optional fields, extensions,  Spec
57 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  then payload...                v
58 //
59 // Byte 0:  Version 2, no padding, no RTP extensions, no CSRCs.
60 // Byte 1:  Marker bit indicates whether this is the last packet, followed by a
61 //          7-bit payload type.
62 // Byte 12: Key Frame bit, followed by "RFID will be provided" bit, followed by
63 //          6 bits specifying the number of extensions that will be provided.
64 
65 // The minimum-possible valid size of a Cast RTP packet (i.e., no optional
66 // fields, extensions, nor payload).
67 constexpr int kRtpPacketMinValidSize = 18;
68 
69 // All Cast RTP packets must carry the version 2 flag, not use padding, not use
70 // RTP extensions, and have zero CSRCs.
71 constexpr uint8_t kRtpRequiredFirstByte = 0b10000000;
72 
73 // Bitmasks to isolate fields within byte 2 of the Cast RTP header.
74 constexpr uint8_t kRtpMarkerBitMask = 0b10000000;
75 constexpr uint8_t kRtpPayloadTypeMask = 0b01111111;
76 
77 // Describes the content being transported over RTP streams. These are Cast
78 // Streaming specific assignments, within the "dynamic" range provided by
79 // IANA. Note that this Cast Streaming implementation does not manipulate
80 // already-encoded data, and so these payload types are only "informative" in
81 // purpose and can be used to check for corruption while parsing packets.
82 enum class RtpPayloadType : uint8_t {
83   kNull = 0,
84 
85   kAudioFirst = 96,
86   kAudioOpus = 96,
87   kAudioAac = 97,
88   kAudioPcm16 = 98,
89   kAudioVarious = 99,  // Codec being used is not fixed.
90 
91   kVideoFirst = 100,
92   kVideoVp8 = 100,
93   kVideoH264 = 101,
94   kVideoVarious = 102,  // Codec being used is not fixed.
95   kVideoLast = 102,
96 
97   // Some AndroidTV receivers require the payload type for audio to be 127, and
98   // video to be 96; regardless of the codecs actually being used. This is
99   // definitely out-of-spec, and inconsistent with the audio versus video range
100   // of values, but must be taken into account for backwards-compatibility.
101   // TODO(crbug.com/1127978): RTP payload types need to represent actual type,
102   // as well as have options for new codecs like VP9.
103   kAudioHackForAndroidTV = 127,
104   kVideoHackForAndroidTV = 96,
105 };
106 
107 // NOTE: currently we match the legacy Chrome sender's behavior of always
108 // sending the audio and video hacks for AndroidTV, however we should migrate
109 // to using proper rtp payload types. New payload types for new codecs, such
110 // as VP9, should also be defined.
111 // TODO(crbug.com/1127978): RTP payload types need to represent actual type,
112 // as well as have options for new codecs like VP9.
113 RtpPayloadType GetPayloadType(AudioCodec codec);
114 RtpPayloadType GetPayloadType(VideoCodec codec);
115 
116 // Returns true if the |raw_byte| can be type-casted to a RtpPayloadType, and is
117 // also not RtpPayloadType::kNull. The caller should mask the byte, to select
118 // the lower 7 bits, if applicable.
119 bool IsRtpPayloadType(uint8_t raw_byte);
120 
121 // Bitmasks to isolate fields within byte 12 of the Cast RTP header.
122 constexpr uint8_t kRtpKeyFrameBitMask = 0b10000000;
123 constexpr uint8_t kRtpHasReferenceFrameIdBitMask = 0b01000000;
124 constexpr uint8_t kRtpExtensionCountMask = 0b00111111;
125 
126 // Cast extensions. This implementation supports only the Adaptive Latency
127 // extension, and ignores all others:
128 //
129 //  0                   1                   2                   3
130 //  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
131 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 // |  TYPE = 1 | Ext data SIZE = 2 |Playout Delay (unsigned millis)|
133 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 //
135 // The Adaptive Latency extension permits changing the fixed end-to-end playout
136 // delay of a single RTP stream.
137 constexpr uint8_t kAdaptiveLatencyRtpExtensionType = 1;
138 constexpr int kNumExtensionDataSizeFieldBits = 10;
139 
140 // RTCP Common Header:
141 //
142 //  0                   1                   2                   3
143 //  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
144 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 // |V=2|P|RC/Subtyp|  Packet Type  |            Length             |
146 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 constexpr int kRtcpCommonHeaderSize = 4;
148 // All RTCP packets must carry the version 2 flag and not use padding.
149 constexpr uint8_t kRtcpRequiredVersionAndPaddingBits = 0b100;
150 constexpr int kRtcpReportCountFieldNumBits = 5;
151 
152 // https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml
153 enum class RtcpPacketType : uint8_t {
154   kNull = 0,
155 
156   kSenderReport = 200,
157   kReceiverReport = 201,
158   kSourceDescription = 202,
159   kApplicationDefined = 204,
160   kPayloadSpecific = 206,
161   kExtendedReports = 207,
162 };
163 
164 // Returns true if the |raw_byte| can be type-casted to a RtcpPacketType, and is
165 // also not RtcpPacketType::kNull.
166 bool IsRtcpPacketType(uint8_t raw_byte);
167 
168 // Supported subtype values in the RTCP Common Header when the packet type is
169 // kApplicationDefined or kPayloadSpecific.
170 enum class RtcpSubtype : uint8_t {
171   kNull = 0,
172 
173   kPictureLossIndicator = 1,
174   kReceiverLog = 2,
175   kFeedback = 15,
176 };
177 
178 // RTCP Sender Report:
179 //
180 //  0                   1                   2                   3
181 //  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
182 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
183 // |                        SSRC of Sender                         |
184 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
185 // |                                                               |
186 // |                         NTP Timestamp                         |
187 // |                                                               |
188 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
189 // |                         RTP Timestamp                         |
190 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
191 // |                     Sender's Packet Count                     |
192 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
193 // |                     Sender's Octet Count                      |
194 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
195 //        ...Followed by zero or more "Report Blocks"...
196 constexpr int kRtcpSenderReportSize = 24;
197 
198 // RTCP Receiver Report:
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 // |                       SSRC of Receiver                        |
204 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205 //        ...Followed by zero or more "Report Blocks"...
206 constexpr int kRtcpReceiverReportSize = 4;
207 
208 // RTCP Report Block. For Cast Streaming, zero or one of these accompanies a
209 // Sender or Receiver Report, which is different than the RTCP spec (which
210 // allows zero or more).
211 //
212 //  0                   1                   2                   3
213 //  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
214 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215 // |                           "To" SSRC                           |
216 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 // | Fraction Lost |       Cumulative Number of Packets Lost       |
218 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 // |      [32-bit extended] Highest Sequence Number Received       |
220 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221 // | Interarrival Jitter Mean Absolute Deviation (in RTP Timebase) |
222 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 // |    Middle 32-bits of NTP Timestamp from last Sender Report    |
224 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
225 // |     Delay since last Sender Report (1/65536 sec timebase)     |
226 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
227 constexpr int kRtcpReportBlockSize = 24;
228 constexpr int kRtcpCumulativePacketsFieldNumBits = 24;
229 
230 // Cast Feedback Message:
231 //
232 //  0                   1                   2                   3
233 //  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
234 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235 // |                       SSRC of Receiver                        |
236 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237 // |                        SSRC of Sender                         |
238 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
239 // |               Unique identifier 'C' 'A' 'S' 'T'               |
240 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
241 // | CkPt Frame ID | # Loss Fields | Current Playout Delay (msec)  |
242 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
243 constexpr int kRtcpFeedbackHeaderSize = 16;
244 constexpr uint32_t kRtcpCastIdentifierWord =
245     (uint32_t{'C'} << 24) | (uint32_t{'A'} << 16) | (uint32_t{'S'} << 8) |
246     uint32_t{'T'};
247 //
248 // "Checkpoint Frame ID" indicates that all frames prior to and including this
249 // one have been fully received. Unfortunately, the Frame ID is truncated to its
250 // lower 8 bits in the packet, and 8 bits is not really enough: If a RTCP packet
251 // is received very late (e.g., more than 1.2 seconds late for 100 FPS audio),
252 // the Checkpoint Frame ID here will be mis-interpreted as representing a
253 // higher-numbered frame than what was intended. This could make the sender's
254 // tracking of "completely received" frames inconsistent, and Cast Streaming
255 // would live-lock. However, this design issue has been baked into the spec and
256 // millions of deployments over several years, and so there's no changing it
257 // now. See kMaxUnackedFrames in constants.h.
258 //
259 // "# Loss fields" indicates the number of packet-level NACK words, 0 to 255:
260 //
261 //  0                   1                   2                   3
262 //  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
263 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264 // | w/in Frame ID | Lost Frame Packet ID          | PID BitVector |
265 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 constexpr int kRtcpFeedbackLossFieldSize = 4;
267 //
268 // "Within Frame ID" is a truncated-to-8-bits frame ID field and, when
269 // bit-expanded should always be interpreted to represent a value greater than
270 // the Checkpoint Frame ID. "Lost Frame Packet ID" is either a specific packet
271 // (within the frame) that has not been received, or kAllPacketsLost to indicate
272 // none the packets for the frame have been received yet. In the former case,
273 // "PID Bit Vector" then represents which of the next 8 packets are also
274 // missing.
275 //
276 // Finally, all of the above is optionally followed by a frame-level ACK bit
277 // vector:
278 //
279 //  0                   1                   2                   3
280 //  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
281 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
282 // |               Unique identifier 'C' 'S' 'T' '2'               |
283 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
284 // |Feedback Count | # BVectOctets | ACK BitVect (2 to 254 bytes)...
285 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ → zero-padded to word boundary
286 constexpr int kRtcpFeedbackAckHeaderSize = 6;
287 constexpr uint32_t kRtcpCst2IdentifierWord =
288     (uint32_t{'C'} << 24) | (uint32_t{'S'} << 16) | (uint32_t{'T'} << 8) |
289     uint32_t{'2'};
290 constexpr int kRtcpMinAckBitVectorOctets = 2;
291 constexpr int kRtcpMaxAckBitVectorOctets = 254;
292 //
293 // "Feedback Count" is a wrap-around counter indicating the number of Cast
294 // Feedbacks that have been sent before this one. "# Bit Vector Octets"
295 // indicates the number of bytes of ACK bit vector following. Cast RTCP
296 // alignment/padding requirements (to 4-byte boundaries) dictates the following
297 // rules for generating the ACK bit vector:
298 //
299 //   1. There must be at least 2 bytes of ACK bit vector, if only to pad the 6
300 //      byte header with two more bytes.
301 //   2. If more than 2 bytes are needed, they must be added 4 at a time to
302 //      maintain the 4-byte alignment of the overall RTCP packet.
303 //   3. The total number of octets may not exceed 255; but, because of #2, 254
304 //      is effectively the limit.
305 //   4. The first bit in the first octet represents "Checkpoint Frame ID" plus
306 //      two. "Plus two" and not "plus one" because otherwise the "Checkpoint
307 //      Frame ID" should have been a greater value!
308 
309 // RTCP Extended Report:
310 //
311 //  0                   1                   2                   3
312 //  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
313 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314 // |                     SSRC of Report Author                     |
315 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
316 constexpr int kRtcpExtendedReportHeaderSize = 4;
317 //
318 // ...followed by zero or more Blocks:
319 //
320 //  0                   1                   2                   3
321 //  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
322 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
323 // |  Block Type   | Reserved = 0  |       Block Length            |
324 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325 // |           ..."Block Length" words of report data...           |
326 // +                                                               +
327 // +                                                               +
328 constexpr int kRtcpExtendedReportBlockHeaderSize = 4;
329 //
330 // Cast Streaming only uses Receiver Reference Time Reports:
331 // https://tools.ietf.org/html/rfc3611#section-4.4. So, the entire block would
332 // be:
333 //
334 //  0                   1                   2                   3
335 //  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
336 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337 // | Block Type=4  | Reserved = 0  |       Block Length = 2        |
338 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
339 // |                         NTP Timestamp                         |
340 // |                                                               |
341 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
342 constexpr uint8_t kRtcpReceiverReferenceTimeReportBlockType = 4;
343 constexpr int kRtcpReceiverReferenceTimeReportBlockSize = 8;
344 
345 // Cast Picture Loss Indicator Message:
346 //
347 //  0                   1                   2                   3
348 //  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
349 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
350 // |                       SSRC of Receiver                        |
351 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
352 // |                        SSRC of Sender                         |
353 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
354 constexpr int kRtcpPictureLossIndicatorHeaderSize = 8;
355 
356 }  // namespace cast
357 }  // namespace openscreen
358 
359 #endif  // CAST_STREAMING_RTP_DEFINES_H_
360