<commit_before>/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <cassert>

#include "receiver_fec.h"
#include "rtp_receiver_video.h"
#include "rtp_utility.h"

// RFC 5109
namespace webrtc {
ReceiverFEC::ReceiverFEC(const WebRtc_Word32 id, RTPReceiverVideo* owner)
    : _owner(owner),
      _fec(new ForwardErrorCorrection(id)),
      _payloadTypeFEC(-1),
      _lastFECSeqNum(0),
      _frameComplete(true) {
}

ReceiverFEC::~ReceiverFEC() {
  // Clean up DecodeFEC()
  while (!_receivedPacketList.empty()){
    ForwardErrorCorrection::ReceivedPacket* receivedPacket =
        _receivedPacketList.front();
    delete receivedPacket->pkt;
    delete receivedPacket;
    _receivedPacketList.pop_front();
  }
  assert(_receivedPacketList.empty());

  if (_fec != NULL) {
    bool frameComplete = true;
    _fec->DecodeFEC(&_receivedPacketList, &_recoveredPacketList,_lastFECSeqNum,
                    frameComplete);
    delete _fec;
  }
}

void ReceiverFEC::SetPayloadTypeFEC(const WebRtc_Word8 payloadType) {
  _payloadTypeFEC = payloadType;
}

/*
    0                   1                    2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3  4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |F|   block PT  |  timestamp offset         |   block length    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


RFC 2198          RTP Payload for Redundant Audio Data    September 1997

   The bits in the header are specified as follows:

   F: 1 bit First bit in header indicates whether another header block
       follows.  If 1 further header blocks follow, if 0 this is the
       last header block.
       If 0 there is only 1 byte RED header

   block PT: 7 bits RTP payload type for this block.

   timestamp offset:  14 bits Unsigned offset of timestamp of this block
       relative to timestamp given in RTP header.  The use of an unsigned
       offset implies that redundant data must be sent after the primary
       data, and is hence a time to be subtracted from the current
       timestamp to determine the timestamp of the data for which this
       block is the redundancy.

   block length:  10 bits Length in bytes of the corresponding data
       block excluding header.
*/

WebRtc_Word32 ReceiverFEC::AddReceivedFECPacket(
    const WebRtcRTPHeader* rtpHeader,
    const WebRtc_UWord8* incomingRtpPacket,
    const WebRtc_UWord16 payloadDataLength,
    bool& FECpacket,
    bool oldPacket) {
  if (_payloadTypeFEC == -1) {
    return -1;
  }

  WebRtc_UWord8 REDHeaderLength = 1;

  // Add to list without RED header, aka a virtual RTP packet
  // we remove the RED header

  ForwardErrorCorrection::ReceivedPacket* receivedPacket =
      new ForwardErrorCorrection::ReceivedPacket;
  receivedPacket->pkt = new ForwardErrorCorrection::Packet;

  // get payload type from RED header
  WebRtc_UWord8 payloadType =
      incomingRtpPacket[rtpHeader->header.headerLength] & 0x7f;

  // use the payloadType to decide if it's FEC or coded data
  if(_payloadTypeFEC == payloadType) {
    receivedPacket->isFec = true;
    FECpacket = true;
    // We don't need to parse old FEC packets.
    // Old FEC packets are sent to jitter buffer as empty packets in the
    // callback in rtp_receiver_video.
    if (oldPacket)  return 0;
  } else {
    receivedPacket->isFec = false;
    FECpacket = false;
  }
  receivedPacket->lastMediaPktInFrame = rtpHeader->header.markerBit;
  receivedPacket->seqNum = rtpHeader->header.sequenceNumber;

  WebRtc_UWord16 blockLength = 0;
  if(incomingRtpPacket[rtpHeader->header.headerLength] & 0x80) {
    // f bit set in RED header
    REDHeaderLength = 4;
    WebRtc_UWord16 timestampOffset =
        (incomingRtpPacket[rtpHeader->header.headerLength + 1]) << 8;
    timestampOffset += incomingRtpPacket[rtpHeader->header.headerLength+2];
    timestampOffset = timestampOffset >> 2;
    if(timestampOffset != 0) {
      // sanity timestampOffset must be 0
      assert(false);
      return -1;
    }
    blockLength =
        (0x03 & incomingRtpPacket[rtpHeader->header.headerLength + 2]) << 8;
    blockLength += (incomingRtpPacket[rtpHeader->header.headerLength + 3]);

    // check next RED header
    if(incomingRtpPacket[rtpHeader->header.headerLength+4] & 0x80) {
      // more than 2 blocks in packet not supported
      assert(false);<commit_msg>Removing an assert for a case that can occur when corrupt packets are injected into voice engine. Review URL: https://webrtc-codereview.appspot.com/373004<commit_after>/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <cassert>

#include "receiver_fec.h"
#include "rtp_receiver_video.h"
#include "rtp_utility.h"

// RFC 5109
namespace webrtc {
ReceiverFEC::ReceiverFEC(const WebRtc_Word32 id, RTPReceiverVideo* owner)
    : _owner(owner),
      _fec(new ForwardErrorCorrection(id)),
      _payloadTypeFEC(-1),
      _lastFECSeqNum(0),
      _frameComplete(true) {
}

ReceiverFEC::~ReceiverFEC() {
  // Clean up DecodeFEC()
  while (!_receivedPacketList.empty()){
    ForwardErrorCorrection::ReceivedPacket* receivedPacket =
        _receivedPacketList.front();
    delete receivedPacket->pkt;
    delete receivedPacket;
    _receivedPacketList.pop_front();
  }
  assert(_receivedPacketList.empty());

  if (_fec != NULL) {
    bool frameComplete = true;
    _fec->DecodeFEC(&_receivedPacketList, &_recoveredPacketList,_lastFECSeqNum,
                    frameComplete);
    delete _fec;
  }
}

void ReceiverFEC::SetPayloadTypeFEC(const WebRtc_Word8 payloadType) {
  _payloadTypeFEC = payloadType;
}

/*
    0                   1                    2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3  4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |F|   block PT  |  timestamp offset         |   block length    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


RFC 2198          RTP Payload for Redundant Audio Data    September 1997

   The bits in the header are specified as follows:

   F: 1 bit First bit in header indicates whether another header block
       follows.  If 1 further header blocks follow, if 0 this is the
       last header block.
       If 0 there is only 1 byte RED header

   block PT: 7 bits RTP payload type for this block.

   timestamp offset:  14 bits Unsigned offset of timestamp of this block
       relative to timestamp given in RTP header.  The use of an unsigned
       offset implies that redundant data must be sent after the primary
       data, and is hence a time to be subtracted from the current
       timestamp to determine the timestamp of the data for which this
       block is the redundancy.

   block length:  10 bits Length in bytes of the corresponding data
       block excluding header.
*/

WebRtc_Word32 ReceiverFEC::AddReceivedFECPacket(
    const WebRtcRTPHeader* rtpHeader,
    const WebRtc_UWord8* incomingRtpPacket,
    const WebRtc_UWord16 payloadDataLength,
    bool& FECpacket,
    bool oldPacket) {
  if (_payloadTypeFEC == -1) {
    return -1;
  }

  WebRtc_UWord8 REDHeaderLength = 1;

  // Add to list without RED header, aka a virtual RTP packet
  // we remove the RED header

  ForwardErrorCorrection::ReceivedPacket* receivedPacket =
      new ForwardErrorCorrection::ReceivedPacket;
  receivedPacket->pkt = new ForwardErrorCorrection::Packet;

  // get payload type from RED header
  WebRtc_UWord8 payloadType =
      incomingRtpPacket[rtpHeader->header.headerLength] & 0x7f;

  // use the payloadType to decide if it's FEC or coded data
  if(_payloadTypeFEC == payloadType) {
    receivedPacket->isFec = true;
    FECpacket = true;
    // We don't need to parse old FEC packets.
    // Old FEC packets are sent to jitter buffer as empty packets in the
    // callback in rtp_receiver_video.
    if (oldPacket)  return 0;
  } else {
    receivedPacket->isFec = false;
    FECpacket = false;
  }
  receivedPacket->lastMediaPktInFrame = rtpHeader->header.markerBit;
  receivedPacket->seqNum = rtpHeader->header.sequenceNumber;

  WebRtc_UWord16 blockLength = 0;
  if(incomingRtpPacket[rtpHeader->header.headerLength] & 0x80) {
    // f bit set in RED header
    REDHeaderLength = 4;
    WebRtc_UWord16 timestampOffset =
        (incomingRtpPacket[rtpHeader->header.headerLength + 1]) << 8;
    timestampOffset += incomingRtpPacket[rtpHeader->header.headerLength+2];
    timestampOffset = timestampOffset >> 2;
    if(timestampOffset != 0) {
      // timestampOffset should be 0, however, this is a valid error case in
      // the event of garbage payload.
      return -1;
    }

    blockLength =
        (0x03 & incomingRtpPacket[rtpHeader->header.headerLength + 2]) << 8;
    blockLength += (incomingRtpPacket[rtpHeader->header.headerLength + 3]);

    // check next RED header
    if(incomingRtpPacket[rtpHeader->header.headerLength+4] & 0x80) {
      // more than 2 blocks in packet not supported
      assert(false);
{'fraction_non_alphanumeric': 0.09996911355914753, 'fraction_numerical': 0.022650056625141562, 'mean_word_length': 2.940770791075051, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3007070', 'n_tokens_mistral': 3180, 'n_tokens_neox': 2859, 'n_words': 973}