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/modules/rtp_rtcp/source/byte_io.h" |
| 19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Maximum number of media packets that can be protected in one batch. |
| 24 constexpr size_t kMaxMediaPackets = 48; // Since we are reusing ULPFEC masks. |
| 25 |
| 26 // Maximum number of FEC packets stored inside ForwardErrorCorrection. |
| 27 constexpr size_t kMaxFecPackets = kMaxMediaPackets; |
| 28 |
| 29 // Size (in bytes) of packet masks, given number of K bits set. |
| 30 constexpr size_t kFlexfecPacketMaskSize0K = 2; |
| 31 constexpr size_t kFlexfecPacketMaskSize1K = 6; |
| 32 constexpr size_t kFlexfecPacketMaskSize2K = 14; |
| 33 |
| 34 // Size (in bytes) of part of header which is not packet mask specific. |
| 35 constexpr size_t kBaseHeaderSize = 12; |
| 36 |
| 37 // Size (in bytes) of part of header which is packet mask specific, |
| 38 // given number of K-bits set. |
| 39 constexpr size_t kAuxHeaderSize0K = 6 + kFlexfecPacketMaskSize0K; |
| 40 constexpr size_t kAuxHeaderSize1K = 6 + kFlexfecPacketMaskSize1K; |
| 41 constexpr size_t kAuxHeaderSize2K = 6 + kFlexfecPacketMaskSize2K; |
| 42 |
| 43 // Here we count the K-bits as belonging to the packet mask. |
| 44 // This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize, |
| 45 // which calculates a bound on the needed packet mask size including K-bits, |
| 46 // given a packet mask without K-bits. |
| 47 size_t FecHeaderSize(size_t packet_mask_size) { |
| 48 RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSize2K); |
| 49 if (packet_mask_size <= kFlexfecPacketMaskSize0K) { |
| 50 return kBaseHeaderSize + kAuxHeaderSize0K; |
| 51 } else if (packet_mask_size <= kFlexfecPacketMaskSize1K) { |
| 52 return kBaseHeaderSize + kAuxHeaderSize1K; |
| 53 } else { |
| 54 return kBaseHeaderSize + kAuxHeaderSize2K; |
| 55 } |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
| 60 namespace webrtc { |
| 61 |
| 62 FlexfecHeaderReader::FlexfecHeaderReader() |
| 63 : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {} |
| 64 |
| 65 FlexfecHeaderReader::~FlexfecHeaderReader() = default; |
| 66 |
| 67 // TODO(brandtr): Update this function when we support flexible masks, |
| 68 // retransmissions, and/or several protected SSRCs. |
| 69 bool FlexfecHeaderReader::ReadFecHeader( |
| 70 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const { |
| 71 bool f_bit = (fec_packet->pkt->data[0] & 0x80) != 0u; |
| 72 if (f_bit) { |
| 73 return false; |
| 74 } |
| 75 bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0u; |
| 76 if (r_bit) { |
| 77 return false; |
| 78 } |
| 79 uint8_t ssrc_count = |
| 80 ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]); |
| 81 if (ssrc_count != 1u) { |
| 82 return false; |
| 83 } |
| 84 uint32_t ssrc = |
| 85 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]); |
| 86 uint16_t seq_num_base = |
| 87 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]); |
| 88 |
| 89 // Read packet mask and pack it tight by removing the interleaved K bits. |
| 90 // This destroys the FlexFEC standards compliance of the packet masks, |
| 91 // but makes it compatible with the ULPFEC masks. |
| 92 // |
| 93 // We treat the mask parts as unsigned integers with host order endianness |
| 94 // in order to simplify the bit shifting between bytes. |
| 95 bool k_bit0 = false, k_bit1 = false, k_bit2 = false; |
| 96 uint16_t mask_part0 = |
| 97 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[18]); |
| 98 k_bit0 = (mask_part0 & (1 << 15)) != 0u; // Read K-bit 0. |
| 99 mask_part0 <<= 1; // Shift away K-bit 0. |
| 100 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->pkt->data[18], mask_part0); |
| 101 if (!k_bit0) { |
| 102 uint32_t mask_part1 = |
| 103 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[20]); |
| 104 k_bit1 = (mask_part1 & (1 << 31)) != 0u; // Read K-bit 1. |
| 105 bool bit15 = (mask_part1 & (1 << 30)) != 0u; // Read bit 15. |
| 106 if (bit15) { |
| 107 fec_packet->pkt->data[19] |= 0x01; // Set bit 15. |
| 108 } |
| 109 mask_part1 <<= 2; // Shift away K-bit 1 and bit 15. |
| 110 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->pkt->data[20], |
| 111 mask_part1); |
| 112 if (!k_bit1) { |
| 113 uint64_t mask_part2 = |
| 114 ByteReader<uint64_t>::ReadBigEndian(&fec_packet->pkt->data[24]); |
| 115 uint64_t one = 1; // Ensures enough bits available for the bit shifts. |
| 116 k_bit2 = (mask_part2 & (one << 63)) != 0u; // Read K-bit 2. |
| 117 if (!k_bit2) { |
| 118 // Malformed header. |
| 119 return false; |
| 120 } |
| 121 bool bit46 = (mask_part2 & (one << 62)) != 0u; // Read bit 46. |
| 122 bool bit47 = (mask_part2 & (one << 61)) != 0u; // Read bit 47. |
| 123 if (bit46) { |
| 124 fec_packet->pkt->data[23] |= 0x01; // Set bit 46. |
| 125 } |
| 126 if (bit47) { |
| 127 fec_packet->pkt->data[23] |= 0x02; // Set bit 47. |
| 128 } |
| 129 mask_part2 <<= 3; // Shift away K-bit 2, bit 46, and bit 47. |
| 130 ByteWriter<uint64_t>::WriteBigEndian(&fec_packet->pkt->data[24], |
| 131 mask_part2); |
| 132 } |
| 133 } |
| 134 |
| 135 // Store "ULPFECized" packet mask info. |
| 136 size_t packet_mask_size; |
| 137 if (k_bit0) { |
| 138 packet_mask_size = kFlexfecPacketMaskSize0K; |
| 139 } else if (k_bit1) { |
| 140 packet_mask_size = kFlexfecPacketMaskSize1K; |
| 141 } else { |
| 142 packet_mask_size = kFlexfecPacketMaskSize2K; |
| 143 } |
| 144 fec_packet->fec_header_size = FecHeaderSize(packet_mask_size); |
| 145 |
| 146 const size_t kPacketMaskOffset = kBaseHeaderSize + 6; |
| 147 fec_packet->packet_mask_info[ssrc] = |
| 148 std::make_tuple(seq_num_base, kPacketMaskOffset, packet_mask_size); |
| 149 |
| 150 // In FlexFEC, all media packets are protected in their entirety. |
| 151 fec_packet->protection_length = |
| 152 fec_packet->pkt->length - fec_packet->fec_header_size; |
| 153 |
| 154 return true; |
| 155 } |
| 156 |
| 157 FlexfecHeaderWriter::FlexfecHeaderWriter() |
| 158 : FecHeaderWriter(kMaxMediaPackets, |
| 159 kMaxFecPackets, |
| 160 kBaseHeaderSize + kAuxHeaderSize2K) {} |
| 161 |
| 162 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default; |
| 163 |
| 164 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask, |
| 165 size_t packet_mask_size) const { |
| 166 bool ulpfec_l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet); |
| 167 if (ulpfec_l_bit) { |
| 168 // The packet mask is 48 bits long. |
| 169 bool bit46 = (packet_mask[5] & 0x01) != 0u; |
| 170 bool bit47 = (packet_mask[5] & 0x02) != 0u; |
| 171 if (bit46 || bit47) { |
| 172 return kFlexfecPacketMaskSize2K; |
| 173 } else { |
| 174 return kFlexfecPacketMaskSize1K; |
| 175 } |
| 176 } else { |
| 177 RTC_DCHECK_EQ(packet_mask_size, kUlpfecPacketMaskSizeLBitClear); |
| 178 // The packet mask is 16 bits long. |
| 179 bool bit15 = (packet_mask[1] & 0x01) != 0u; |
| 180 if (bit15) { |
| 181 return kFlexfecPacketMaskSize1K; |
| 182 } else { |
| 183 return kFlexfecPacketMaskSize0K; |
| 184 } |
| 185 } |
| 186 } |
| 187 |
| 188 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const { |
| 189 return ::FecHeaderSize(packet_mask_size); |
| 190 } |
| 191 |
| 192 // This function adapts the precomputed ULPFEC packet masks to the |
| 193 // FlexFEC header standard. Note that the header size is computed by |
| 194 // FecHeaderSize(), so in this function we can be sure that we are |
| 195 // writing in space that is intended for the header. |
| 196 // |
| 197 // TODO(brandtr): Update this function when we support offset-based masks, |
| 198 // retransmissions, and protecting multiple SSRCs. |
| 199 void FlexfecHeaderWriter::FinalizeFecHeader( |
| 200 const ForwardErrorCorrection::PacketList& media_packets, |
| 201 const uint8_t* packet_mask, |
| 202 size_t packet_mask_size, |
| 203 ForwardErrorCorrection::Packet* fec_packet) const { |
| 204 fec_packet->data[0] &= 0x7f; // Clear F bit. |
| 205 fec_packet->data[0] &= 0xbf; // Clear R bit. |
| 206 // Write SSRC count. |
| 207 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], 1u); |
| 208 RTC_DCHECK(!media_packets.empty()); |
| 209 ForwardErrorCorrection::Packet* first_media_packet = |
| 210 media_packets.front().get(); |
| 211 RTC_DCHECK(first_media_packet); |
| 212 // TODO(brandtr): Get rid of these 'Parse' function calls, |
| 213 // when the Packet class has been refactored. |
| 214 uint32_t ssrc_base = |
| 215 ForwardErrorCorrection::ParseSsrc(first_media_packet->data); |
| 216 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], ssrc_base); |
| 217 uint16_t seq_num_base = |
| 218 ForwardErrorCorrection::ParseSequenceNumber(first_media_packet->data); |
| 219 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base); |
| 220 // Adapt ULPFEC packet mask to FlexFEC header. |
| 221 // |
| 222 // We treat the mask parts as unsigned integers with host order endianness |
| 223 // in order to simplify the bit shifting between bytes. |
| 224 bool ulpfec_l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet); |
| 225 if (ulpfec_l_bit) { |
| 226 // The packet mask is 48 bits long. |
| 227 uint16_t tmp_mask_part0 = |
| 228 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); |
| 229 uint32_t tmp_mask_part1 = |
| 230 ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]); |
| 231 |
| 232 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. |
| 233 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0); |
| 234 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15. |
| 235 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[20], tmp_mask_part1); |
| 236 bool bit15 = (packet_mask[1] & 0x01) != 0u; |
| 237 if (bit15) { |
| 238 fec_packet->data[20] |= 0x40; // Set bit 15. |
| 239 } |
| 240 bool bit46 = (packet_mask[5] & 0x01) != 0u; |
| 241 bool bit47 = (packet_mask[5] & 0x02) != 0u; |
| 242 if (!bit46 && !bit47) { |
| 243 fec_packet->data[20] |= 0x80; // Set K-bit 1. |
| 244 } else { |
| 245 memset(&fec_packet->data[24], 0u, 8); // Clear all trailing bits. |
| 246 fec_packet->data[24] |= 0x80; // Set K-bit 2. |
| 247 if (bit46) { |
| 248 fec_packet->data[24] |= 0x40; // Set bit 46. |
| 249 } |
| 250 if (bit47) { |
| 251 fec_packet->data[24] |= 0x20; // Set bit 47. |
| 252 } |
| 253 } |
| 254 } else { |
| 255 RTC_DCHECK_EQ(packet_mask_size, kUlpfecPacketMaskSizeLBitClear); |
| 256 // The packet mask is 16 bits long. |
| 257 uint16_t tmp_mask_part0 = |
| 258 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); |
| 259 |
| 260 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. |
| 261 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0); |
| 262 bool bit15 = (packet_mask[1] & 0x01) != 0u; |
| 263 if (!bit15) { |
| 264 fec_packet->data[18] |= 0x80; // Set K-bit 0. |
| 265 } else { |
| 266 memset(&fec_packet->data[20], 0u, 4); // Clear all trailing bits. |
| 267 fec_packet->data[20] |= 0x80; // Set K-bit 1. |
| 268 fec_packet->data[20] |= 0x40; // Set bit 15. |
| 269 } |
| 270 } |
| 271 } |
| 272 |
| 273 } // namespace webrtc |
OLD | NEW |