-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RtpPacket.hpp
642 lines (510 loc) · 13.9 KB
/
RtpPacket.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#ifndef MS_RTC_RTP_PACKET_HPP
#define MS_RTC_RTP_PACKET_HPP
#include "common.hpp"
#include "Utils.hpp"
#include "RTC/Codecs/PayloadDescriptorHandler.hpp"
#include <absl/container/flat_hash_map.h>
#include <array>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
using json = nlohmann::json;
namespace RTC
{
// Max MTU size.
constexpr size_t MtuSize{ 1500u };
// MID header extension max length (just used when setting/updating MID
// extension).
constexpr uint8_t MidMaxLength{ 8u };
class RtpPacket
{
public:
/* Struct for RTP header. */
struct Header
{
#if defined(MS_LITTLE_ENDIAN)
uint8_t csrcCount : 4;
uint8_t extension : 1;
uint8_t padding : 1;
uint8_t version : 2;
uint8_t payloadType : 7;
uint8_t marker : 1;
#elif defined(MS_BIG_ENDIAN)
uint8_t version : 2;
uint8_t padding : 1;
uint8_t extension : 1;
uint8_t csrcCount : 4;
uint8_t marker : 1;
uint8_t payloadType : 7;
#endif
uint16_t sequenceNumber;
uint32_t timestamp;
uint32_t ssrc;
};
private:
/* Struct for RTP header extension. */
struct HeaderExtension
{
uint16_t id;
uint16_t length; // Size of value in multiples of 4 bytes.
uint8_t value[1];
};
private:
/* Struct for One-Byte extension. */
struct OneByteExtension
{
#if defined(MS_LITTLE_ENDIAN)
uint8_t len : 4;
uint8_t id : 4;
#elif defined(MS_BIG_ENDIAN)
uint8_t id : 4;
uint8_t len : 4;
#endif
uint8_t value[1];
};
private:
/* Struct for Two-Bytes extension. */
struct TwoBytesExtension
{
uint8_t id;
uint8_t len;
uint8_t value[1];
};
public:
/* Struct for replacing and setting header extensions. */
struct GenericExtension
{
GenericExtension(uint8_t id, uint8_t len, uint8_t* value) : id(id), len(len), value(value){};
uint8_t id;
uint8_t len;
uint8_t* value;
};
public:
/* Struct with frame-marking information. */
struct FrameMarking
{
#if defined(MS_LITTLE_ENDIAN)
uint8_t tid : 3;
uint8_t base : 1;
uint8_t discardable : 1;
uint8_t independent : 1;
uint8_t end : 1;
uint8_t start : 1;
#elif defined(MS_BIG_ENDIAN)
uint8_t start : 1;
uint8_t end : 1;
uint8_t independent : 1;
uint8_t discardable : 1;
uint8_t base : 1;
uint8_t tid : 3;
#endif
uint8_t lid;
uint8_t tl0picidx;
};
public:
static const size_t HeaderSize{ 12 };
static bool IsRtp(const uint8_t* data, size_t len)
{
// NOTE: RtcpPacket::IsRtcp() must always be called before this method.
auto header = const_cast<Header*>(reinterpret_cast<const Header*>(data));
// clang-format off
return (
(len >= HeaderSize) &&
// DOC: https://tools.ietf.org/html/draft-ietf-avtcore-rfc5764-mux-fixes
(data[0] > 127 && data[0] < 192) &&
// RTP Version must be 2.
(header->version == 2)
);
// clang-format on
}
static RtpPacket* Parse(const uint8_t* data, size_t len);
private:
RtpPacket(
Header* header,
HeaderExtension* headerExtension,
const uint8_t* payload,
size_t payloadLength,
uint8_t payloadPadding,
size_t size);
public:
~RtpPacket();
void Dump() const;
void FillJson(json& jsonObject) const;
const uint8_t* GetData() const
{
return (const uint8_t*)this->header;
}
size_t GetSize() const
{
return this->size;
}
uint8_t GetPayloadType() const
{
return this->header->payloadType;
}
void SetPayloadType(uint8_t payloadType)
{
this->header->payloadType = payloadType;
}
bool HasMarker() const
{
return this->header->marker;
}
void SetMarker(bool marker)
{
this->header->marker = marker;
}
void SetPayloadPaddingFlag(bool flag)
{
this->header->padding = flag;
}
uint16_t GetSequenceNumber() const
{
return uint16_t{ ntohs(this->header->sequenceNumber) };
}
void SetSequenceNumber(uint16_t seq)
{
this->header->sequenceNumber = uint16_t{ htons(seq) };
}
uint32_t GetTimestamp() const
{
return uint32_t{ ntohl(this->header->timestamp) };
}
void SetTimestamp(uint32_t timestamp)
{
this->header->timestamp = uint32_t{ htonl(timestamp) };
}
uint32_t GetSsrc() const
{
return uint32_t{ ntohl(this->header->ssrc) };
}
void SetSsrc(uint32_t ssrc)
{
this->header->ssrc = uint32_t{ htonl(ssrc) };
}
bool HasHeaderExtension() const
{
return (this->headerExtension ? true : false);
}
// After calling this method, all the extension ids are reset to 0.
void SetExtensions(uint8_t type, const std::vector<GenericExtension>& extensions);
uint16_t GetHeaderExtensionId() const
{
if (!this->headerExtension)
return 0u;
return uint16_t{ ntohs(this->headerExtension->id) };
}
size_t GetHeaderExtensionLength() const
{
if (!this->headerExtension)
return 0u;
return static_cast<size_t>(ntohs(this->headerExtension->length) * 4);
}
uint8_t* GetHeaderExtensionValue() const
{
if (!this->headerExtension)
return nullptr;
return this->headerExtension->value;
}
bool HasOneByteExtensions() const
{
return GetHeaderExtensionId() == 0xBEDE;
}
bool HasTwoBytesExtensions() const
{
return (GetHeaderExtensionId() & 0b1111111111110000) == 0b0001000000000000;
}
void SetMidExtensionId(uint8_t id)
{
this->midExtensionId = id;
}
void SetRidExtensionId(uint8_t id)
{
this->ridExtensionId = id;
}
void SetRepairedRidExtensionId(uint8_t id)
{
this->rridExtensionId = id;
}
void SetAbsSendTimeExtensionId(uint8_t id)
{
this->absSendTimeExtensionId = id;
}
void SetTransportWideCc01ExtensionId(uint8_t id)
{
this->transportWideCc01ExtensionId = id;
}
// NOTE: Remove once RFC.
void SetFrameMarking07ExtensionId(uint8_t id)
{
this->frameMarking07ExtensionId = id;
}
void SetFrameMarkingExtensionId(uint8_t id)
{
this->frameMarkingExtensionId = id;
}
void SetSsrcAudioLevelExtensionId(uint8_t id)
{
this->ssrcAudioLevelExtensionId = id;
}
void SetVideoOrientationExtensionId(uint8_t id)
{
this->videoOrientationExtensionId = id;
}
bool ReadMid(std::string& mid) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->midExtensionId, extenLen);
if (!extenValue || extenLen == 0u)
return false;
mid.assign(reinterpret_cast<const char*>(extenValue), static_cast<size_t>(extenLen));
return true;
}
void UpdateMid(const std::string& mid);
bool ReadRid(std::string& rid) const
{
// First try with the RID id then with the Repaired RID id.
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->ridExtensionId, extenLen);
if (extenValue && extenLen > 0u)
{
rid.assign(reinterpret_cast<const char*>(extenValue), static_cast<size_t>(extenLen));
return true;
}
extenValue = GetExtension(this->rridExtensionId, extenLen);
if (extenValue && extenLen > 0u)
{
rid.assign(reinterpret_cast<const char*>(extenValue), static_cast<size_t>(extenLen));
return true;
}
return false;
}
bool ReadAbsSendTime(uint32_t& absSendtime) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->absSendTimeExtensionId, extenLen);
if (!extenValue || extenLen != 3u)
return false;
absSendtime = Utils::Byte::Get3Bytes(extenValue, 0);
return true;
}
bool UpdateAbsSendTime(uint64_t ms)
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->absSendTimeExtensionId, extenLen);
if (!extenValue || extenLen != 3u)
return false;
auto absSendTime = Utils::Time::TimeMsToAbsSendTime(ms);
Utils::Byte::Set3Bytes(extenValue, 0, absSendTime);
return true;
}
bool ReadTransportWideCc01(uint16_t& wideSeqNumber) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->transportWideCc01ExtensionId, extenLen);
if (!extenValue || extenLen != 2u)
return false;
wideSeqNumber = Utils::Byte::Get2Bytes(extenValue, 0);
return true;
}
bool UpdateTransportWideCc01(uint16_t wideSeqNumber)
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->transportWideCc01ExtensionId, extenLen);
if (!extenValue || extenLen != 2u)
return false;
Utils::Byte::Set2Bytes(extenValue, 0, wideSeqNumber);
return true;
}
bool ReadFrameMarking(RtpPacket::FrameMarking** frameMarking, uint8_t& length) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->frameMarkingExtensionId, extenLen);
// NOTE: Remove this once framemarking draft becomes RFC.
if (!extenValue)
extenValue = GetExtension(this->frameMarking07ExtensionId, extenLen);
if (!extenValue || extenLen > 3u)
return false;
*frameMarking = reinterpret_cast<RtpPacket::FrameMarking*>(extenValue);
length = extenLen;
return true;
}
bool ReadSsrcAudioLevel(uint8_t& volume, bool& voice) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->ssrcAudioLevelExtensionId, extenLen);
if (!extenValue || extenLen != 1u)
return false;
volume = Utils::Byte::Get1Byte(extenValue, 0);
voice = (volume & (1 << 7)) ? true : false;
volume &= ~(1 << 7);
return true;
}
bool ReadVideoOrientation(bool& camera, bool& flip, uint16_t& rotation) const
{
uint8_t extenLen;
uint8_t* extenValue = GetExtension(this->videoOrientationExtensionId, extenLen);
if (!extenValue || extenLen != 1u)
return false;
uint8_t cvoByte = Utils::Byte::Get1Byte(extenValue, 0);
uint8_t cameraValue = ((cvoByte & 0b00001000) >> 3);
uint8_t flipValue = ((cvoByte & 0b00000100) >> 2);
uint8_t rotationValue = (cvoByte & 0b00000011);
camera = cameraValue ? true : false;
flip = flipValue ? true : false;
// Using counter clockwise values.
switch (rotationValue)
{
case 3:
rotation = 270;
break;
case 2:
rotation = 180;
break;
case 1:
rotation = 90;
break;
default:
rotation = 0;
}
return true;
}
bool HasExtension(uint8_t id) const
{
if (id == 0u)
{
return false;
}
else if (HasOneByteExtensions())
{
if (id > 14)
return false;
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
return this->oneByteExtensions[id - 1] != nullptr;
}
else if (HasTwoBytesExtensions())
{
auto it = this->mapTwoBytesExtensions.find(id);
if (it == this->mapTwoBytesExtensions.end())
return false;
auto* extension = it->second;
// In Two-Byte extensions value length may be zero. If so, return false.
if (extension->len == 0u)
return false;
return true;
}
else
{
return false;
}
}
uint8_t* GetExtension(uint8_t id, uint8_t& len) const
{
len = 0u;
if (id == 0u)
{
return nullptr;
}
else if (HasOneByteExtensions())
{
if (id > 14)
return nullptr;
// `-1` because we have 14 elements total 0..13 and `id` is in the range 1..14.
auto* extension = this->oneByteExtensions[id - 1];
if (!extension)
return nullptr;
// In One-Byte extensions value length 0 means 1.
len = extension->len + 1;
return extension->value;
}
else if (HasTwoBytesExtensions())
{
auto it = this->mapTwoBytesExtensions.find(id);
if (it == this->mapTwoBytesExtensions.end())
return nullptr;
auto* extension = it->second;
len = extension->len;
// In Two-Byte extensions value length may be zero. If so, return nullptr.
if (extension->len == 0u)
return nullptr;
return extension->value;
}
else
{
return nullptr;
}
}
bool SetExtensionLength(uint8_t id, uint8_t len);
uint8_t* GetPayload() const
{
return this->payloadLength != 0u ? this->payload : nullptr;
}
size_t GetPayloadLength() const
{
return this->payloadLength;
}
void SetPayloadLength(size_t length);
uint8_t GetPayloadPadding() const
{
return this->payloadPadding;
}
uint8_t GetSpatialLayer() const
{
if (!this->payloadDescriptorHandler)
return 0u;
return this->payloadDescriptorHandler->GetSpatialLayer();
}
uint8_t GetTemporalLayer() const
{
if (!this->payloadDescriptorHandler)
return 0u;
return this->payloadDescriptorHandler->GetTemporalLayer();
}
bool IsKeyFrame() const
{
if (!this->payloadDescriptorHandler)
return false;
return this->payloadDescriptorHandler->IsKeyFrame();
}
RtpPacket* Clone() const;
void RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq);
bool RtxDecode(uint8_t payloadType, uint32_t ssrc);
void SetPayloadDescriptorHandler(RTC::Codecs::PayloadDescriptorHandler* payloadDescriptorHandler)
{
this->payloadDescriptorHandler.reset(payloadDescriptorHandler);
}
bool ProcessPayload(RTC::Codecs::EncodingContext* context, bool& marker);
void RestorePayload();
void ShiftPayload(size_t payloadOffset, size_t shift, bool expand = true);
private:
void ParseExtensions();
private:
// Passed by argument.
Header* header{ nullptr };
uint8_t* csrcList{ nullptr };
HeaderExtension* headerExtension{ nullptr };
// There might be up to 14 one-byte header extensions
// (https://datatracker.ietf.org/doc/html/rfc5285#section-4.2), use std::array.
std::array<OneByteExtension*, 14> oneByteExtensions;
absl::flat_hash_map<uint8_t, TwoBytesExtension*> mapTwoBytesExtensions;
uint8_t midExtensionId{ 0u };
uint8_t ridExtensionId{ 0u };
uint8_t rridExtensionId{ 0u };
uint8_t absSendTimeExtensionId{ 0u };
uint8_t transportWideCc01ExtensionId{ 0u };
uint8_t frameMarking07ExtensionId{ 0u }; // NOTE: Remove once RFC.
uint8_t frameMarkingExtensionId{ 0u };
uint8_t ssrcAudioLevelExtensionId{ 0u };
uint8_t videoOrientationExtensionId{ 0u };
uint8_t* payload{ nullptr };
size_t payloadLength{ 0u };
uint8_t payloadPadding{ 0u };
size_t size{ 0u }; // Full size of the packet in bytes.
// Codecs
std::shared_ptr<Codecs::PayloadDescriptorHandler> payloadDescriptorHandler;
// Buffer where this packet is allocated, can be `nullptr` if packet was
// parsed from externally provided buffer.
uint8_t* buffer{ nullptr };
};
} // namespace RTC
#endif