• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/flexfec_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 #include "rtc_base/logging.h"
20 
21 namespace webrtc {
22 
23 namespace {
24 
25 // Maximum number of media packets that can be protected in one batch.
26 constexpr size_t kMaxMediaPackets = 48;  // Since we are reusing ULPFEC masks.
27 
28 // Maximum number of media packets tracked by FEC decoder.
29 // Maintain a sufficiently larger tracking window than `kMaxMediaPackets`
30 // to account for packet reordering in pacer/ network.
31 constexpr size_t kMaxTrackedMediaPackets = 4 * kMaxMediaPackets;
32 
33 // Maximum number of FEC packets stored inside ForwardErrorCorrection.
34 constexpr size_t kMaxFecPackets = kMaxMediaPackets;
35 
36 // Size (in bytes) of packet masks, given number of K bits set.
37 constexpr size_t kFlexfecPacketMaskSizes[] = {2, 6, 14};
38 
39 // Size (in bytes) of part of header which is not packet mask specific.
40 constexpr size_t kBaseHeaderSize = 12;
41 
42 // Size (in bytes) of part of header which is stream specific.
43 constexpr size_t kStreamSpecificHeaderSize = 6;
44 
45 // Size (in bytes) of header, given the single stream packet mask size, i.e.
46 // the number of K-bits set.
47 constexpr size_t kHeaderSizes[] = {
48     kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[0],
49     kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[1],
50     kBaseHeaderSize + kStreamSpecificHeaderSize + kFlexfecPacketMaskSizes[2]};
51 
52 // We currently only support single-stream protection.
53 // TODO(brandtr): Update this when we support multistream protection.
54 constexpr uint8_t kSsrcCount = 1;
55 
56 // There are three reserved bytes that MUST be set to zero in the header.
57 constexpr uint32_t kReservedBits = 0;
58 
59 // TODO(brandtr): Update this when we support multistream protection.
60 constexpr size_t kPacketMaskOffset =
61     kBaseHeaderSize + kStreamSpecificHeaderSize;
62 
63 // Here we count the K-bits as belonging to the packet mask.
64 // This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize,
65 // which calculates a bound on the needed packet mask size including K-bits,
66 // given a packet mask without K-bits.
FlexfecHeaderSize(size_t packet_mask_size)67 size_t FlexfecHeaderSize(size_t packet_mask_size) {
68   RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSizes[2]);
69   if (packet_mask_size <= kFlexfecPacketMaskSizes[0]) {
70     return kHeaderSizes[0];
71   } else if (packet_mask_size <= kFlexfecPacketMaskSizes[1]) {
72     return kHeaderSizes[1];
73   }
74   return kHeaderSizes[2];
75 }
76 
77 }  // namespace
78 
FlexfecHeaderReader()79 FlexfecHeaderReader::FlexfecHeaderReader()
80     : FecHeaderReader(kMaxTrackedMediaPackets, kMaxFecPackets) {}
81 
82 FlexfecHeaderReader::~FlexfecHeaderReader() = default;
83 
84 // TODO(brandtr): Update this function when we support flexible masks,
85 // retransmissions, and/or several protected SSRCs.
ReadFecHeader(ForwardErrorCorrection::ReceivedFecPacket * fec_packet) const86 bool FlexfecHeaderReader::ReadFecHeader(
87     ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
88   if (fec_packet->pkt->data.size() <=
89       kBaseHeaderSize + kStreamSpecificHeaderSize) {
90     RTC_LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
91     return false;
92   }
93   uint8_t* const data = fec_packet->pkt->data.MutableData();
94   bool r_bit = (data[0] & 0x80) != 0;
95   if (r_bit) {
96     RTC_LOG(LS_INFO)
97         << "FlexFEC packet with retransmission bit set. We do not yet "
98            "support this, thus discarding the packet.";
99     return false;
100   }
101   bool f_bit = (data[0] & 0x40) != 0;
102   if (f_bit) {
103     RTC_LOG(LS_INFO)
104         << "FlexFEC packet with inflexible generator matrix. We do "
105            "not yet support this, thus discarding packet.";
106     return false;
107   }
108   uint8_t ssrc_count = ByteReader<uint8_t>::ReadBigEndian(&data[8]);
109   if (ssrc_count != 1) {
110     RTC_LOG(LS_INFO)
111         << "FlexFEC packet protecting multiple media SSRCs. We do not "
112            "yet support this, thus discarding packet.";
113     return false;
114   }
115   uint32_t protected_ssrc = ByteReader<uint32_t>::ReadBigEndian(&data[12]);
116   uint16_t seq_num_base = ByteReader<uint16_t>::ReadBigEndian(&data[16]);
117 
118   // Parse the FlexFEC packet mask and remove the interleaved K-bits.
119   // (See FEC header schematic in flexfec_header_reader_writer.h.)
120   // We store the packed packet mask in-band, which "destroys" the standards
121   // compliance of the header. That is fine though, since the code that
122   // reads from the header (from this point and onwards) is aware of this.
123   // TODO(brandtr): When the FEC packet classes have been refactored, store
124   // the packed packet masks out-of-band, thus leaving the FlexFEC header as is.
125   //
126   // We treat the mask parts as unsigned integers with host order endianness
127   // in order to simplify the bit shifting between bytes.
128   if (fec_packet->pkt->data.size() < kHeaderSizes[0]) {
129     RTC_LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
130     return false;
131   }
132   uint8_t* const packet_mask = data + kPacketMaskOffset;
133   bool k_bit0 = (packet_mask[0] & 0x80) != 0;
134   uint16_t mask_part0 = ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
135   // Shift away K-bit 0, implicitly clearing the last bit.
136   mask_part0 <<= 1;
137   ByteWriter<uint16_t>::WriteBigEndian(&packet_mask[0], mask_part0);
138   size_t packet_mask_size;
139   if (k_bit0) {
140     // The first K-bit is set, and the packet mask is thus only 2 bytes long.
141     // We have now read the entire FEC header, and the rest of the packet
142     // is payload.
143     packet_mask_size = kFlexfecPacketMaskSizes[0];
144   } else {
145     if (fec_packet->pkt->data.size() < kHeaderSizes[1]) {
146       return false;
147     }
148     bool k_bit1 = (packet_mask[2] & 0x80) != 0;
149     // We have already shifted the first two bytes of the packet mask one step
150     // to the left, thus removing K-bit 0. We will now shift the next four bytes
151     // of the packet mask two steps to the left. (One step for the removed
152     // K-bit 0, and one step for the to be removed K-bit 1).
153     uint8_t bit15 = (packet_mask[2] >> 6) & 0x01;
154     packet_mask[1] |= bit15;
155     uint32_t mask_part1 = ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
156     // Shift away K-bit 1 and bit 15, implicitly clearing the last two bits.
157     mask_part1 <<= 2;
158     ByteWriter<uint32_t>::WriteBigEndian(&packet_mask[2], mask_part1);
159     if (k_bit1) {
160       // The first K-bit is clear, but the second K-bit is set. The packet
161       // mask is thus 6 bytes long.  We have now read the entire FEC header,
162       // and the rest of the packet is payload.
163       packet_mask_size = kFlexfecPacketMaskSizes[1];
164     } else {
165       if (fec_packet->pkt->data.size() < kHeaderSizes[2]) {
166         RTC_LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
167         return false;
168       }
169       bool k_bit2 = (packet_mask[6] & 0x80) != 0;
170       if (k_bit2) {
171         // The first and second K-bits are clear, but the third K-bit is set.
172         // The packet mask is thus 14 bytes long. We have now read the entire
173         // FEC header, and the rest of the packet is payload.
174         packet_mask_size = kFlexfecPacketMaskSizes[2];
175       } else {
176         RTC_LOG(LS_WARNING)
177             << "Discarding FlexFEC packet with malformed header.";
178         return false;
179       }
180       // At this point, K-bits 0 and 1 have been removed, and the front-most
181       // part of the FlexFEC packet mask has been packed accordingly. We will
182       // now shift the remaning part of the packet mask three steps to the left.
183       // This corresponds to the (in total) three K-bits, which have been
184       // removed.
185       uint8_t tail_bits = (packet_mask[6] >> 5) & 0x03;
186       packet_mask[5] |= tail_bits;
187       uint64_t mask_part2 =
188           ByteReader<uint64_t>::ReadBigEndian(&packet_mask[6]);
189       // Shift away K-bit 2, bit 46, and bit 47, implicitly clearing the last
190       // three bits.
191       mask_part2 <<= 3;
192       ByteWriter<uint64_t>::WriteBigEndian(&packet_mask[6], mask_part2);
193     }
194   }
195 
196   // Store "ULPFECized" packet mask info.
197   fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size);
198   fec_packet->protected_ssrc = protected_ssrc;
199   fec_packet->seq_num_base = seq_num_base;
200   fec_packet->packet_mask_offset = kPacketMaskOffset;
201   fec_packet->packet_mask_size = packet_mask_size;
202 
203   // In FlexFEC, all media packets are protected in their entirety.
204   fec_packet->protection_length =
205       fec_packet->pkt->data.size() - fec_packet->fec_header_size;
206 
207   return true;
208 }
209 
FlexfecHeaderWriter()210 FlexfecHeaderWriter::FlexfecHeaderWriter()
211     : FecHeaderWriter(kMaxMediaPackets, kMaxFecPackets, kHeaderSizes[2]) {}
212 
213 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default;
214 
MinPacketMaskSize(const uint8_t * packet_mask,size_t packet_mask_size) const215 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
216                                               size_t packet_mask_size) const {
217   if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear &&
218       (packet_mask[1] & 0x01) == 0) {
219     // Packet mask is 16 bits long, with bit 15 clear.
220     // It can be used as is.
221     return kFlexfecPacketMaskSizes[0];
222   } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
223     // Packet mask is 16 bits long, with bit 15 set.
224     // We must expand the packet mask with zeros in the FlexFEC header.
225     return kFlexfecPacketMaskSizes[1];
226   } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet &&
227              (packet_mask[5] & 0x03) == 0) {
228     // Packet mask is 48 bits long, with bits 46 and 47 clear.
229     // It can be used as is.
230     return kFlexfecPacketMaskSizes[1];
231   } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
232     // Packet mask is 48 bits long, with at least one of bits 46 and 47 set.
233     // We must expand it with zeros.
234     return kFlexfecPacketMaskSizes[2];
235   }
236   RTC_DCHECK_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
237                           << ".";
238   return kFlexfecPacketMaskSizes[2];
239 }
240 
FecHeaderSize(size_t packet_mask_size) const241 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
242   return FlexfecHeaderSize(packet_mask_size);
243 }
244 
245 // This function adapts the precomputed ULPFEC packet masks to the
246 // FlexFEC header standard. Note that the header size is computed by
247 // FecHeaderSize(), so in this function we can be sure that we are
248 // writing in space that is intended for the header.
249 //
250 // TODO(brandtr): Update this function when we support offset-based masks,
251 // retransmissions, and protecting multiple SSRCs.
FinalizeFecHeader(uint32_t media_ssrc,uint16_t seq_num_base,const uint8_t * packet_mask,size_t packet_mask_size,ForwardErrorCorrection::Packet * fec_packet) const252 void FlexfecHeaderWriter::FinalizeFecHeader(
253     uint32_t media_ssrc,
254     uint16_t seq_num_base,
255     const uint8_t* packet_mask,
256     size_t packet_mask_size,
257     ForwardErrorCorrection::Packet* fec_packet) const {
258   uint8_t* data = fec_packet->data.MutableData();
259   data[0] &= 0x7f;  // Clear R bit.
260   data[0] &= 0xbf;  // Clear F bit.
261   ByteWriter<uint8_t>::WriteBigEndian(&data[8], kSsrcCount);
262   ByteWriter<uint32_t, 3>::WriteBigEndian(&data[9], kReservedBits);
263   ByteWriter<uint32_t>::WriteBigEndian(&data[12], media_ssrc);
264   ByteWriter<uint16_t>::WriteBigEndian(&data[16], seq_num_base);
265   // Adapt ULPFEC packet mask to FlexFEC header.
266   //
267   // We treat the mask parts as unsigned integers with host order endianness
268   // in order to simplify the bit shifting between bytes.
269   uint8_t* const written_packet_mask = data + kPacketMaskOffset;
270   if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
271     // The packet mask is 48 bits long.
272     uint16_t tmp_mask_part0 =
273         ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
274     uint32_t tmp_mask_part1 =
275         ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
276 
277     tmp_mask_part0 >>= 1;  // Shift, thus clearing K-bit 0.
278     ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
279                                          tmp_mask_part0);
280     tmp_mask_part1 >>= 2;  // Shift, thus clearing K-bit 1 and bit 15.
281     ByteWriter<uint32_t>::WriteBigEndian(&written_packet_mask[2],
282                                          tmp_mask_part1);
283     bool bit15 = (packet_mask[1] & 0x01) != 0;
284     if (bit15)
285       written_packet_mask[2] |= 0x40;  // Set bit 15.
286     bool bit46 = (packet_mask[5] & 0x02) != 0;
287     bool bit47 = (packet_mask[5] & 0x01) != 0;
288     if (!bit46 && !bit47) {
289       written_packet_mask[2] |= 0x80;  // Set K-bit 1.
290     } else {
291       memset(&written_packet_mask[6], 0, 8);  // Clear all trailing bits.
292       written_packet_mask[6] |= 0x80;         // Set K-bit 2.
293       if (bit46)
294         written_packet_mask[6] |= 0x40;  // Set bit 46.
295       if (bit47)
296         written_packet_mask[6] |= 0x20;  // Set bit 47.
297     }
298   } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
299     // The packet mask is 16 bits long.
300     uint16_t tmp_mask_part0 =
301         ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
302 
303     tmp_mask_part0 >>= 1;  // Shift, thus clearing K-bit 0.
304     ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
305                                          tmp_mask_part0);
306     bool bit15 = (packet_mask[1] & 0x01) != 0;
307     if (!bit15) {
308       written_packet_mask[0] |= 0x80;  // Set K-bit 0.
309     } else {
310       memset(&written_packet_mask[2], 0U, 4);  // Clear all trailing bits.
311       written_packet_mask[2] |= 0x80;          // Set K-bit 1.
312       written_packet_mask[2] |= 0x40;          // Set bit 15.
313     }
314   } else {
315     RTC_DCHECK_NOTREACHED()
316         << "Incorrect packet mask size: " << packet_mask_size << ".";
317   }
318 }
319 
320 }  // namespace webrtc
321