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