1 /*
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
11 #include "modules/rtp_rtcp/source/ulpfec_header_reader_writer.h"
12
13 #include <string.h>
14
15 #include "api/scoped_refptr.h"
16 #include "modules/rtp_rtcp/source/byte_io.h"
17 #include "modules/rtp_rtcp/source/forward_error_correction_internal.h"
18 #include "rtc_base/checks.h"
19
20 namespace webrtc {
21
22 namespace {
23
24 // Maximum number of media packets that can be protected in one batch.
25 constexpr size_t kMaxMediaPackets = 48;
26
27 // Maximum number of media packets tracked by FEC decoder.
28 // Maintain a sufficiently larger tracking window than `kMaxMediaPackets`
29 // to account for packet reordering in pacer/ network.
30 constexpr size_t kMaxTrackedMediaPackets = 4 * kMaxMediaPackets;
31
32 // Maximum number of FEC packets stored inside ForwardErrorCorrection.
33 constexpr size_t kMaxFecPackets = kMaxMediaPackets;
34
35 // FEC Level 0 header size in bytes.
36 constexpr size_t kFecLevel0HeaderSize = 10;
37
38 // FEC Level 1 (ULP) header size in bytes (L bit is set).
39 constexpr size_t kFecLevel1HeaderSizeLBitSet = 2 + kUlpfecPacketMaskSizeLBitSet;
40
41 // FEC Level 1 (ULP) header size in bytes (L bit is cleared).
42 constexpr size_t kFecLevel1HeaderSizeLBitClear =
43 2 + kUlpfecPacketMaskSizeLBitClear;
44
45 constexpr size_t kPacketMaskOffset = kFecLevel0HeaderSize + 2;
46
UlpfecHeaderSize(size_t packet_mask_size)47 size_t UlpfecHeaderSize(size_t packet_mask_size) {
48 RTC_DCHECK_LE(packet_mask_size, kUlpfecPacketMaskSizeLBitSet);
49 if (packet_mask_size <= kUlpfecPacketMaskSizeLBitClear) {
50 return kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitClear;
51 } else {
52 return kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitSet;
53 }
54 }
55
56 } // namespace
57
UlpfecHeaderReader()58 UlpfecHeaderReader::UlpfecHeaderReader()
59 : FecHeaderReader(kMaxTrackedMediaPackets, kMaxFecPackets) {}
60
61 UlpfecHeaderReader::~UlpfecHeaderReader() = default;
62
ReadFecHeader(ForwardErrorCorrection::ReceivedFecPacket * fec_packet) const63 bool UlpfecHeaderReader::ReadFecHeader(
64 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
65 uint8_t* data = fec_packet->pkt->data.MutableData();
66 if (fec_packet->pkt->data.size() < kPacketMaskOffset) {
67 return false; // Truncated packet.
68 }
69 bool l_bit = (data[0] & 0x40) != 0u;
70 size_t packet_mask_size =
71 l_bit ? kUlpfecPacketMaskSizeLBitSet : kUlpfecPacketMaskSizeLBitClear;
72 fec_packet->fec_header_size = UlpfecHeaderSize(packet_mask_size);
73 uint16_t seq_num_base = ByteReader<uint16_t>::ReadBigEndian(&data[2]);
74 fec_packet->protected_ssrc = fec_packet->ssrc; // Due to RED.
75 fec_packet->seq_num_base = seq_num_base;
76 fec_packet->packet_mask_offset = kPacketMaskOffset;
77 fec_packet->packet_mask_size = packet_mask_size;
78 fec_packet->protection_length =
79 ByteReader<uint16_t>::ReadBigEndian(&data[10]);
80
81 // Store length recovery field in temporary location in header.
82 // This makes the header "compatible" with the corresponding
83 // FlexFEC location of the length recovery field, thus simplifying
84 // the XORing operations.
85 memcpy(&data[2], &data[8], 2);
86
87 return true;
88 }
89
UlpfecHeaderWriter()90 UlpfecHeaderWriter::UlpfecHeaderWriter()
91 : FecHeaderWriter(kMaxMediaPackets,
92 kMaxFecPackets,
93 kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitSet) {}
94
95 UlpfecHeaderWriter::~UlpfecHeaderWriter() = default;
96
97 // TODO(brandtr): Consider updating this implementation (which actually
98 // returns a bound on the sequence number spread), if logic is added to
99 // UlpfecHeaderWriter::FinalizeFecHeader to truncate packet masks which end
100 // in a string of zeroes. (Similar to how it is done in the FlexFEC case.)
MinPacketMaskSize(const uint8_t * packet_mask,size_t packet_mask_size) const101 size_t UlpfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
102 size_t packet_mask_size) const {
103 return packet_mask_size;
104 }
105
FecHeaderSize(size_t packet_mask_size) const106 size_t UlpfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
107 return UlpfecHeaderSize(packet_mask_size);
108 }
109
FinalizeFecHeader(uint32_t,uint16_t seq_num_base,const uint8_t * packet_mask,size_t packet_mask_size,ForwardErrorCorrection::Packet * fec_packet) const110 void UlpfecHeaderWriter::FinalizeFecHeader(
111 uint32_t /* media_ssrc */,
112 uint16_t seq_num_base,
113 const uint8_t* packet_mask,
114 size_t packet_mask_size,
115 ForwardErrorCorrection::Packet* fec_packet) const {
116 uint8_t* data = fec_packet->data.MutableData();
117 // Set E bit to zero.
118 data[0] &= 0x7f;
119 // Set L bit based on packet mask size. (Note that the packet mask
120 // can only take on two discrete values.)
121 bool l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet);
122 if (l_bit) {
123 data[0] |= 0x40; // Set the L bit.
124 } else {
125 RTC_DCHECK_EQ(packet_mask_size, kUlpfecPacketMaskSizeLBitClear);
126 data[0] &= 0xbf; // Clear the L bit.
127 }
128 // Copy length recovery field from temporary location.
129 memcpy(&data[8], &data[2], 2);
130 // Write sequence number base.
131 ByteWriter<uint16_t>::WriteBigEndian(&data[2], seq_num_base);
132 // Protection length is set to entire packet. (This is not
133 // required in general.)
134 const size_t fec_header_size = FecHeaderSize(packet_mask_size);
135 ByteWriter<uint16_t>::WriteBigEndian(
136 &data[10], fec_packet->data.size() - fec_header_size);
137 // Copy the packet mask.
138 memcpy(&data[12], packet_mask, packet_mask_size);
139 }
140
141 } // namespace webrtc
142