LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/modules/rtp_rtcp/source - flexfec_header_reader_writer.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 132 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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           0 : size_t FlexfecHeaderSize(size_t packet_mask_size) {
      64           0 :   RTC_DCHECK_LE(packet_mask_size, kFlexfecPacketMaskSizes[2]);
      65           0 :   if (packet_mask_size <= kFlexfecPacketMaskSizes[0]) {
      66           0 :     return kHeaderSizes[0];
      67           0 :   } else if (packet_mask_size <= kFlexfecPacketMaskSizes[1]) {
      68           0 :     return kHeaderSizes[1];
      69             :   }
      70           0 :   return kHeaderSizes[2];
      71             : }
      72             : 
      73             : }  // namespace
      74             : 
      75           0 : FlexfecHeaderReader::FlexfecHeaderReader()
      76           0 :     : 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           0 : bool FlexfecHeaderReader::ReadFecHeader(
      83             :     ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
      84           0 :   if (fec_packet->pkt->length <= kBaseHeaderSize + kStreamSpecificHeaderSize) {
      85           0 :     LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
      86           0 :     return false;
      87             :   }
      88           0 :   bool r_bit = (fec_packet->pkt->data[0] & 0x80) != 0;
      89           0 :   if (r_bit) {
      90           0 :     LOG(LS_INFO) << "FlexFEC packet with retransmission bit set. We do not yet "
      91           0 :                     "support this, thus discarding the packet.";
      92           0 :     return false;
      93             :   }
      94           0 :   bool f_bit = (fec_packet->pkt->data[0] & 0x40) != 0;
      95           0 :   if (f_bit) {
      96           0 :     LOG(LS_INFO) << "FlexFEC packet with inflexible generator matrix. We do "
      97           0 :                     "not yet support this, thus discarding packet.";
      98           0 :     return false;
      99             :   }
     100             :   uint8_t ssrc_count =
     101           0 :       ByteReader<uint8_t>::ReadBigEndian(&fec_packet->pkt->data[8]);
     102           0 :   if (ssrc_count != 1) {
     103           0 :     LOG(LS_INFO) << "FlexFEC packet protecting multiple media SSRCs. We do not "
     104           0 :                     "yet support this, thus discarding packet.";
     105           0 :     return false;
     106             :   }
     107             :   uint32_t protected_ssrc =
     108           0 :       ByteReader<uint32_t>::ReadBigEndian(&fec_packet->pkt->data[12]);
     109             :   uint16_t seq_num_base =
     110           0 :       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           0 :   if (fec_packet->pkt->length < kHeaderSizes[0]) {
     123           0 :     LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
     124           0 :     return false;
     125             :   }
     126           0 :   uint8_t* const packet_mask = fec_packet->pkt->data + kPacketMaskOffset;
     127           0 :   bool k_bit0 = (packet_mask[0] & 0x80) != 0;
     128           0 :   uint16_t mask_part0 = ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
     129             :   // Shift away K-bit 0, implicitly clearing the last bit.
     130           0 :   mask_part0 <<= 1;
     131           0 :   ByteWriter<uint16_t>::WriteBigEndian(&packet_mask[0], mask_part0);
     132             :   size_t packet_mask_size;
     133           0 :   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           0 :     packet_mask_size = kFlexfecPacketMaskSizes[0];
     138             :   } else {
     139           0 :     if (fec_packet->pkt->length < kHeaderSizes[1]) {
     140           0 :       return false;
     141             :     }
     142           0 :     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           0 :     uint8_t bit15 = (packet_mask[2] >> 6) & 0x01;
     148           0 :     packet_mask[1] |= bit15;
     149           0 :     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           0 :     mask_part1 <<= 2;
     152           0 :     ByteWriter<uint32_t>::WriteBigEndian(&packet_mask[2], mask_part1);
     153           0 :     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           0 :       packet_mask_size = kFlexfecPacketMaskSizes[1];
     158             :     } else {
     159           0 :       if (fec_packet->pkt->length < kHeaderSizes[2]) {
     160           0 :         LOG(LS_WARNING) << "Discarding truncated FlexFEC packet.";
     161           0 :         return false;
     162             :       }
     163           0 :       bool k_bit2 = (packet_mask[6] & 0x80) != 0;
     164           0 :       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           0 :         packet_mask_size = kFlexfecPacketMaskSizes[2];
     169             :       } else {
     170           0 :         LOG(LS_WARNING) << "Discarding FlexFEC packet with malformed header.";
     171           0 :         return false;
     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           0 :       uint8_t tail_bits = (packet_mask[6] >> 5) & 0x03;
     179           0 :       packet_mask[5] |= tail_bits;
     180             :       uint64_t mask_part2 =
     181           0 :           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           0 :       mask_part2 <<= 3;
     185           0 :       ByteWriter<uint64_t>::WriteBigEndian(&packet_mask[6], mask_part2);
     186             :     }
     187             :   }
     188             : 
     189             :   // Store "ULPFECized" packet mask info.
     190           0 :   fec_packet->fec_header_size = FlexfecHeaderSize(packet_mask_size);
     191           0 :   fec_packet->protected_ssrc = protected_ssrc;
     192           0 :   fec_packet->seq_num_base = seq_num_base;
     193           0 :   fec_packet->packet_mask_offset = kPacketMaskOffset;
     194           0 :   fec_packet->packet_mask_size = packet_mask_size;
     195             : 
     196             :   // In FlexFEC, all media packets are protected in their entirety.
     197           0 :   fec_packet->protection_length =
     198           0 :       fec_packet->pkt->length - fec_packet->fec_header_size;
     199             : 
     200           0 :   return true;
     201             : }
     202             : 
     203           0 : FlexfecHeaderWriter::FlexfecHeaderWriter()
     204           0 :     : FecHeaderWriter(kMaxMediaPackets, kMaxFecPackets, kHeaderSizes[2]) {}
     205             : 
     206             : FlexfecHeaderWriter::~FlexfecHeaderWriter() = default;
     207             : 
     208           0 : size_t FlexfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
     209             :                                               size_t packet_mask_size) const {
     210           0 :   if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear &&
     211           0 :       (packet_mask[1] & 0x01) == 0) {
     212             :     // Packet mask is 16 bits long, with bit 15 clear.
     213             :     // It can be used as is.
     214           0 :     return kFlexfecPacketMaskSizes[0];
     215           0 :   } 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           0 :     return kFlexfecPacketMaskSizes[1];
     219           0 :   } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet &&
     220           0 :              (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           0 :     return kFlexfecPacketMaskSizes[1];
     224           0 :   } 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           0 :     return kFlexfecPacketMaskSizes[2];
     228             :   }
     229           0 :   RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size << ".";
     230           0 :   return kFlexfecPacketMaskSizes[2];
     231             : }
     232             : 
     233           0 : size_t FlexfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
     234           0 :   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           0 : 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           0 :   fec_packet->data[0] &= 0x7f;  // Clear R bit.
     251           0 :   fec_packet->data[0] &= 0xbf;  // Clear F bit.
     252           0 :   ByteWriter<uint8_t>::WriteBigEndian(&fec_packet->data[8], kSsrcCount);
     253           0 :   ByteWriter<uint32_t, 3>::WriteBigEndian(&fec_packet->data[9], kReservedBits);
     254           0 :   ByteWriter<uint32_t>::WriteBigEndian(&fec_packet->data[12], media_ssrc);
     255           0 :   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           0 :   uint8_t* const written_packet_mask = fec_packet->data + kPacketMaskOffset;
     261           0 :   if (packet_mask_size == kUlpfecPacketMaskSizeLBitSet) {
     262             :     // The packet mask is 48 bits long.
     263             :     uint16_t tmp_mask_part0 =
     264           0 :         ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
     265             :     uint32_t tmp_mask_part1 =
     266           0 :         ByteReader<uint32_t>::ReadBigEndian(&packet_mask[2]);
     267             : 
     268           0 :     tmp_mask_part0 >>= 1;  // Shift, thus clearing K-bit 0.
     269           0 :     ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
     270           0 :                                          tmp_mask_part0);
     271           0 :     tmp_mask_part1 >>= 2;  // Shift, thus clearing K-bit 1 and bit 15.
     272           0 :     ByteWriter<uint32_t>::WriteBigEndian(&written_packet_mask[2],
     273           0 :                                          tmp_mask_part1);
     274           0 :     bool bit15 = (packet_mask[1] & 0x01) != 0;
     275           0 :     if (bit15)
     276           0 :       written_packet_mask[2] |= 0x40;  // Set bit 15.
     277           0 :     bool bit46 = (packet_mask[5] & 0x02) != 0;
     278           0 :     bool bit47 = (packet_mask[5] & 0x01) != 0;
     279           0 :     if (!bit46 && !bit47) {
     280           0 :       written_packet_mask[2] |= 0x80;  // Set K-bit 1.
     281             :     } else {
     282           0 :       memset(&written_packet_mask[6], 0, 8);  // Clear all trailing bits.
     283           0 :       written_packet_mask[6] |= 0x80;         // Set K-bit 2.
     284           0 :       if (bit46)
     285           0 :         written_packet_mask[6] |= 0x40;  // Set bit 46.
     286           0 :       if (bit47)
     287           0 :         written_packet_mask[6] |= 0x20;  // Set bit 47.
     288             :     }
     289           0 :   } else if (packet_mask_size == kUlpfecPacketMaskSizeLBitClear) {
     290             :     // The packet mask is 16 bits long.
     291             :     uint16_t tmp_mask_part0 =
     292           0 :         ByteReader<uint16_t>::ReadBigEndian(&packet_mask[0]);
     293             : 
     294           0 :     tmp_mask_part0 >>= 1;  // Shift, thus clearing K-bit 0.
     295           0 :     ByteWriter<uint16_t>::WriteBigEndian(&written_packet_mask[0],
     296           0 :                                          tmp_mask_part0);
     297           0 :     bool bit15 = (packet_mask[1] & 0x01) != 0;
     298           0 :     if (!bit15) {
     299           0 :       written_packet_mask[0] |= 0x80;  // Set K-bit 0.
     300             :     } else {
     301           0 :       memset(&written_packet_mask[2], 0U, 4);  // Clear all trailing bits.
     302           0 :       written_packet_mask[2] |= 0x80;          // Set K-bit 1.
     303           0 :       written_packet_mask[2] |= 0x40;          // Set bit 15.
     304             :     }
     305             :   } else {
     306           0 :     RTC_NOTREACHED() << "Incorrect packet mask size: " << packet_mask_size
     307           0 :                      << ".";
     308             :   }
     309           0 : }
     310             : 
     311             : }  // namespace webrtc

Generated by: LCOV version 1.13