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/modules/rtp_rtcp/source/byte_io.h" | |
| 19 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.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 FEC packets stored inside ForwardErrorCorrection. | |
| 29 constexpr size_t kMaxFecPackets = kMaxMediaPackets; | |
| 30 | |
| 31 // Size (in bytes) of packet masks, given number of K bits set. | |
| 32 constexpr size_t kFlexfecPacketMaskSize0KBitsSet = 2; | |
| 33 constexpr size_t kFlexfecPacketMaskSize1KBitsSet = 6; | |
| 34 constexpr size_t kFlexfecPacketMaskSize2KBitsSet = 14; | |
| 35 | |
| 36 // Size (in bytes) of part of header which is not packet mask specific. | |
| 37 constexpr size_t kBaseHeaderSize = 12; | |
|
danilchap
2016/09/06 09:40:53
that doesn't include first SSRC and base Seq Num?
brandtr
2016/09/07 08:21:14
Because in the multistream case, there will be sev
| |
| 38 | |
| 39 // Size (in bytes) of part of header which is packet mask specific, | |
| 40 // given number of K-bits set. | |
| 41 constexpr size_t kAuxHeaderSize0KBitsSet = 6 + kFlexfecPacketMaskSize0KBitsSet; | |
|
danilchap
2016/09/06 09:40:53
not sure how this constants helps.
It seems it wou
brandtr
2016/09/07 08:21:14
I've simplified it a bit. I now distinguish the ba
| |
| 42 constexpr size_t kAuxHeaderSize1KBitsSet = 6 + kFlexfecPacketMaskSize1KBitsSet; | |
| 43 constexpr size_t kAuxHeaderSize2KBitsSet = 6 + kFlexfecPacketMaskSize2KBitsSet; | |
| 44 | |
| 45 constexpr size_t kPacketMaskOffset = kBaseHeaderSize + 6; | |
| 46 | |
| 47 // Here we count the K-bits as belonging to the packet mask. | |
| 48 // This can be used in conjunction with FlexfecHeaderWriter::MinPacketMaskSize, | |
| 49 // which calculates a bound on the needed packet mask size including K-bits, | |
| 50 // given a packet mask without K-bits. | |
| 51 size_t FlexfecHeaderSize(size_t packet_mask_size) { | |
| 52 RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSize2KBitsSet); | |
| 53 if (packet_mask_size <= kFlexfecPacketMaskSize0KBitsSet) { | |
| 54 return kBaseHeaderSize + kAuxHeaderSize0KBitsSet; | |
| 55 } else if (packet_mask_size <= kFlexfecPacketMaskSize1KBitsSet) { | |
| 56 return kBaseHeaderSize + kAuxHeaderSize1KBitsSet; | |
| 57 } | |
| 58 return kBaseHeaderSize + kAuxHeaderSize2KBitsSet; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 FlexfecHeaderReader::FlexfecHeaderReader() | |
| 64 : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {} | |
| 65 | |
| 66 FlexfecHeaderReader::~FlexfecHeaderReader() = default; | |
| 67 | |
| 68 // TODO(brandtr): Update this function when we support flexible masks, | |
| 69 // retransmissions, and/or several protected SSRCs. | |
| 70 bool FlexfecHeaderReader::ReadFecHeader( | |
| 71 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const { | |
| 72 bool f_bit = (fec_packet->pkt->data[0] & 0x80) != 0u; | |
|
danilchap
2016/09/06 09:40:53
will (win/android) compiler complain if you write
brandtr
2016/09/07 08:21:14
Not sure, trying without the 'u'.
If I remember c
danilchap
2016/09/07 12:41:17
!= 0 helps to see explicit conversion from bit to
| |
| 73 if (f_bit) { | |
| 74 return false; | |
| 75 } | |
| 76 bool r_bit = (fec_packet->pkt->data[0] & 0x40) != 0u; | |
| 77 if (r_bit) { | |
| 78 return false; | |
| 79 } | |
| 80 uint8_t ssrc_count = | |
| 81 ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]); | |
| 82 if (ssrc_count != 1u) { | |
| 83 return false; | |
| 84 } | |
| 85 uint32_t protected_ssrc = | |
| 86 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]); | |
| 87 uint16_t seq_num_base = | |
| 88 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[16]); | |
| 89 | |
| 90 // Read packet mask and pack it tight by removing the interleaved K bits. | |
| 91 // This destroys the FlexFEC standards compliance of the packet masks, | |
| 92 // but makes it compatible with the ULPFEC masks. | |
| 93 // | |
| 94 // We treat the mask parts as unsigned integers with host order endianness | |
| 95 // in order to simplify the bit shifting between bytes. | |
| 96 bool k_bit0 = false, k_bit1 = false, k_bit2 = false; | |
|
danilchap
2016/09/06 09:40:53
prefer 3 lines.
Also it might be good idea to decl
brandtr
2016/09/07 08:21:13
Done.
| |
| 97 uint16_t mask_part0 = | |
| 98 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[18]); | |
| 99 k_bit0 = (mask_part0 & (1 << 15)) != 0u; // Read K-bit 0. | |
| 100 mask_part0 <<= 1; // Shift away K-bit 0. | |
| 101 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->pkt->data[18], mask_part0); | |
|
danilchap
2016/09/06 09:40:53
Can Writing to the data you are reading from
in th
brandtr
2016/09/07 08:21:14
It will be in the future, when I do the refactorin
| |
| 102 if (!k_bit0) { | |
|
danilchap
2016/09/06 09:40:53
did you check if base/bitbuffer can make your code
brandtr
2016/09/07 08:21:13
I considered it, but I found the explicit masking
| |
| 103 uint32_t mask_part1 = | |
| 104 ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[20]); | |
| 105 k_bit1 = (mask_part1 & (1 << 31)) != 0u; // Read K-bit 1. | |
| 106 bool bit15 = (mask_part1 & (1 << 30)) != 0u; // Read bit 15. | |
| 107 if (bit15) { | |
| 108 fec_packet->pkt->data[19] |= 0x01; // Set bit 15. | |
| 109 } | |
| 110 mask_part1 <<= 2; // Shift away K-bit 1 and bit 15. | |
| 111 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->pkt->data[20], | |
| 112 mask_part1); | |
| 113 if (!k_bit1) { | |
| 114 uint64_t mask_part2 = | |
| 115 ByteReader<uint64_t>::ReadBigEndian(&fec_packet->pkt->data[24]); | |
| 116 uint64_t one = 1; // Ensures enough bits available for the bit shifts. | |
|
danilchap
2016/09/06 09:40:53
may be use 1ULL instead of this constant?
brandtr
2016/09/07 08:21:14
Done.
| |
| 117 k_bit2 = (mask_part2 & (one << 63)) != 0u; // Read K-bit 2. | |
| 118 if (!k_bit2) { | |
| 119 // Malformed header. | |
| 120 return false; | |
| 121 } | |
| 122 bool bit46 = (mask_part2 & (one << 62)) != 0u; // Read bit 46. | |
| 123 bool bit47 = (mask_part2 & (one << 61)) != 0u; // Read bit 47. | |
| 124 if (bit46) { | |
| 125 fec_packet->pkt->data[23] |= 0x01; // Set bit 46. | |
| 126 } | |
| 127 if (bit47) { | |
| 128 fec_packet->pkt->data[23] |= 0x02; // Set bit 47. | |
| 129 } | |
| 130 mask_part2 <<= 3; // Shift away K-bit 2, bit 46, and bit 47. | |
| 131 ByteWriter<uint64_t>::WriteBigEndian(&fec_packet->pkt->data[24], | |
| 132 mask_part2); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // Store "ULPFECized" packet mask info. | |
| 137 size_t packet_mask_size; | |
| 138 if (k_bit0) { | |
| 139 packet_mask_size = kFlexfecPacketMaskSize0KBitsSet; | |
| 140 } else if (k_bit1) { | |
| 141 packet_mask_size = kFlexfecPacketMaskSize1KBitsSet; | |
| 142 } else { | |
| 143 packet_mask_size = kFlexfecPacketMaskSize2KBitsSet; | |
| 144 } | |
| 145 fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size); | |
| 146 fec_packet->protected_ssrc = protected_ssrc; | |
| 147 fec_packet->seq_num_base = seq_num_base; | |
| 148 fec_packet->packet_mask_offset = kPacketMaskOffset; | |
| 149 fec_packet->packet_mask_size = packet_mask_size; | |
| 150 | |
| 151 // In FlexFEC, all media packets are protected in their entirety. | |
| 152 fec_packet->protection_length = | |
| 153 fec_packet->pkt->length - fec_packet->fec_header_size; | |
| 154 | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 FlexfecHeaderWriter::FlexfecHeaderWriter() | |
| 159 : FecHeaderWriter(kMaxMediaPackets, | |
| 160 kMaxFecPackets, | |
| 161 kBaseHeaderSize + kAuxHeaderSize2KBitsSet) {} | |
| 162 | |
| 163 FlexfecHeaderWriter::~FlexfecHeaderWriter() = default; | |
| 164 | |
| 165 size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask, | |
| 166 size_t packet_mask_size) const { | |
| 167 bool ulpfec_l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet); | |
|
danilchap
2016/09/06 09:40:53
does code in this file rely on packet_mask_size be
brandtr
2016/09/07 08:21:14
Done.
| |
| 168 if (ulpfec_l_bit) { | |
| 169 // The packet mask is 48 bits long. | |
| 170 bool bit46 = (packet_mask[5] & 0x01) != 0u; | |
|
danilchap
2016/09/06 09:40:53
can you operator on bit46 and bit47 togehter?
if (
brandtr
2016/09/07 08:21:13
Done.
| |
| 171 bool bit47 = (packet_mask[5] & 0x02) != 0u; | |
| 172 if (bit46 || bit47) { | |
| 173 return kFlexfecPacketMaskSize2KBitsSet; | |
| 174 } | |
| 175 return kFlexfecPacketMaskSize1KBitsSet; | |
| 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 kFlexfecPacketMaskSize1KBitsSet; | |
| 182 } | |
| 183 return kFlexfecPacketMaskSize0KBitsSet; | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const { | |
| 188 return FlexfecHeaderSize(packet_mask_size); | |
| 189 } | |
| 190 | |
| 191 // This function adapts the precomputed ULPFEC packet masks to the | |
| 192 // FlexFEC header standard. Note that the header size is computed by | |
| 193 // FecHeaderSize(), so in this function we can be sure that we are | |
| 194 // writing in space that is intended for the header. | |
| 195 // | |
| 196 // TODO(brandtr): Update this function when we support offset-based masks, | |
| 197 // retransmissions, and protecting multiple SSRCs. | |
| 198 void FlexfecHeaderWriter::FinalizeFecHeader( | |
| 199 uint32_t ssrc, | |
| 200 uint16_t seq_num_base, | |
| 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, SSRC base, sequence number base. | |
| 207 ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], 1u); | |
| 208 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], ssrc); | |
| 209 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[16], seq_num_base); | |
| 210 // Adapt ULPFEC packet mask to FlexFEC header. | |
| 211 // | |
| 212 // We treat the mask parts as unsigned integers with host order endianness | |
| 213 // in order to simplify the bit shifting between bytes. | |
| 214 bool ulpfec_l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet); | |
| 215 if (ulpfec_l_bit) { | |
| 216 // The packet mask is 48 bits long. | |
| 217 uint16_t tmp_mask_part0 = | |
| 218 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); | |
| 219 uint32_t tmp_mask_part1 = | |
| 220 ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]); | |
| 221 | |
| 222 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. | |
| 223 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0); | |
| 224 tmp_mask_part1 >>= 2; // Shift, thus clearing K-bit 1 and bit 15. | |
| 225 ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[20], tmp_mask_part1); | |
| 226 bool bit15 = (packet_mask[1] & 0x01) != 0u; | |
| 227 if (bit15) { | |
| 228 fec_packet->data[20] |= 0x40; // Set bit 15. | |
| 229 } | |
| 230 bool bit46 = (packet_mask[5] & 0x01) != 0u; | |
| 231 bool bit47 = (packet_mask[5] & 0x02) != 0u; | |
| 232 if (!bit46 && !bit47) { | |
| 233 fec_packet->data[20] |= 0x80; // Set K-bit 1. | |
| 234 } else { | |
| 235 memset(&fec_packet->data[24], 0u, 8); // Clear all trailing bits. | |
| 236 fec_packet->data[24] |= 0x80; // Set K-bit 2. | |
| 237 if (bit46) { | |
| 238 fec_packet->data[24] |= 0x40; // Set bit 46. | |
| 239 } | |
| 240 if (bit47) { | |
| 241 fec_packet->data[24] |= 0x20; // Set bit 47. | |
| 242 } | |
| 243 } | |
| 244 } else { | |
| 245 RTC_DCHECK_EQ(packet_mask_size, kUlpfecPacketMaskSizeLBitClear); | |
| 246 // The packet mask is 16 bits long. | |
| 247 uint16_t tmp_mask_part0 = | |
| 248 ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]); | |
| 249 | |
| 250 tmp_mask_part0 >>= 1; // Shift, thus clearing K-bit 0. | |
| 251 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[18], tmp_mask_part0); | |
| 252 bool bit15 = (packet_mask[1] & 0x01) != 0u; | |
| 253 if (!bit15) { | |
| 254 fec_packet->data[18] |= 0x80; // Set K-bit 0. | |
| 255 } else { | |
| 256 memset(&fec_packet->data[20], 0u, 4); // Clear all trailing bits. | |
| 257 fec_packet->data[20] |= 0x80; // Set K-bit 1. | |
| 258 fec_packet->data[20] |= 0x40; // Set bit 15. | |
| 259 } | |
| 260 } | |
| 261 } | |
| 262 | |
| 263 } // namespace webrtc | |
| OLD | NEW |