<commit_before>/*
 *  Copyright (c) 2012 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 "video_engine/vie_receiver.h"

#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
#include "modules/rtp_rtcp/interface/rtp_rtcp.h"
#include "modules/utility/interface/rtp_dump.h"
#include "modules/video_coding/main/interface/video_coding.h"
#include "system_wrappers/interface/critical_section_wrapper.h"
#include "system_wrappers/interface/tick_util.h"
#include "system_wrappers/interface/trace.h"

namespace webrtc {

enum { kPacketOverheadBytes = 28 };

ViEReceiver::ViEReceiver(const int32_t channel_id,
                         VideoCodingModule* module_vcm,
                         RemoteBitrateEstimator* remote_bitrate_estimator)
    : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
      channel_id_(channel_id),
      rtp_rtcp_(NULL),
      vcm_(module_vcm),
      remote_bitrate_estimator_(remote_bitrate_estimator),
      external_decryption_(NULL),
      decryption_buffer_(NULL),
      rtp_dump_(NULL),
      receiving_(false) {
  assert(remote_bitrate_estimator);
}

ViEReceiver::~ViEReceiver() {
  if (decryption_buffer_) {
    delete[] decryption_buffer_;
    decryption_buffer_ = NULL;
  }
  if (rtp_dump_) {
    rtp_dump_->Stop();
    RtpDump::DestroyRtpDump(rtp_dump_);
    rtp_dump_ = NULL;
  }
}

int ViEReceiver::RegisterExternalDecryption(Encryption* decryption) {
  CriticalSectionScoped cs(receive_cs_.get());
  if (external_decryption_) {
    return -1;
  }
  decryption_buffer_ = new WebRtc_UWord8[kViEMaxMtu];
  if (decryption_buffer_ == NULL) {
    return -1;
  }
  external_decryption_ = decryption;
  return 0;
}

int ViEReceiver::DeregisterExternalDecryption() {
  CriticalSectionScoped cs(receive_cs_.get());
  if (external_decryption_ == NULL) {
    return -1;
  }
  external_decryption_ = NULL;
  return 0;
}

void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
  rtp_rtcp_ = module;
}

void ViEReceiver::RegisterSimulcastRtpRtcpModules(
    const std::list<RtpRtcp*>& rtp_modules) {
  CriticalSectionScoped cs(receive_cs_.get());
  rtp_rtcp_simulcast_.clear();

  if (!rtp_modules.empty()) {
    rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
                               rtp_modules.begin(),
                               rtp_modules.end());
  }
}

void ViEReceiver::IncomingRTPPacket(const WebRtc_Word8* rtp_packet,
                                    const WebRtc_Word32 rtp_packet_length,
                                    const char* from_ip,
                                    const WebRtc_UWord16 from_port) {
  InsertRTPPacket(rtp_packet, rtp_packet_length);
}

void ViEReceiver::IncomingRTCPPacket(const WebRtc_Word8* rtcp_packet,
                                     const WebRtc_Word32 rtcp_packet_length,
                                     const char* from_ip,
                                     const WebRtc_UWord16 from_port) {
  InsertRTCPPacket(rtcp_packet, rtcp_packet_length);
}

int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
                                   int rtp_packet_length) {
  if (!receiving_) {
    return -1;
  }
  return InsertRTPPacket((const WebRtc_Word8*) rtp_packet, rtp_packet_length);
}

int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
                                    int rtcp_packet_length) {
  if (!receiving_) {
    return -1;
  }
  return InsertRTCPPacket((const WebRtc_Word8*) rtcp_packet,
                          rtcp_packet_length);
}

WebRtc_Word32 ViEReceiver::OnReceivedPayloadData(
    const WebRtc_UWord8* payload_data, const WebRtc_UWord16 payload_size,
    const WebRtcRTPHeader* rtp_header) {
  if (rtp_header == NULL) {
    return 0;
  }

  // TODO(holmer): Make sure packets reconstructed using FEC are not passed to
  // the bandwidth estimator.
  // Add headers, ideally we would like to include for instance
  // Ethernet header here as well.
  const int packet_size = payload_size + kPacketOverheadBytes +
      rtp_header->header.headerLength + rtp_header->header.paddingLength;
  uint32_t compensated_timestamp = rtp_header->header.timestamp +
      rtp_header->extension.transmissionTimeOffset;
  remote_bitrate_estimator_->IncomingPacket(rtp_header->header.ssrc,
                                            packet_size,
                                            TickTime::MillisecondTimestamp(),
                                            compensated_timestamp);
  if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
    // Check this...
    return -1;
  }
  return 0;
}

void ViEReceiver::OnSendReportReceived(const WebRtc_Word32 id,
                                       const WebRtc_UWord32 senderSSRC,<commit_msg>Fixes a bitrate mismatch between sender and receiver.<commit_after>/*
 *  Copyright (c) 2012 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 "video_engine/vie_receiver.h"

#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
#include "modules/rtp_rtcp/interface/rtp_rtcp.h"
#include "modules/utility/interface/rtp_dump.h"
#include "modules/video_coding/main/interface/video_coding.h"
#include "system_wrappers/interface/critical_section_wrapper.h"
#include "system_wrappers/interface/tick_util.h"
#include "system_wrappers/interface/trace.h"

namespace webrtc {

ViEReceiver::ViEReceiver(const int32_t channel_id,
                         VideoCodingModule* module_vcm,
                         RemoteBitrateEstimator* remote_bitrate_estimator)
    : receive_cs_(CriticalSectionWrapper::CreateCriticalSection()),
      channel_id_(channel_id),
      rtp_rtcp_(NULL),
      vcm_(module_vcm),
      remote_bitrate_estimator_(remote_bitrate_estimator),
      external_decryption_(NULL),
      decryption_buffer_(NULL),
      rtp_dump_(NULL),
      receiving_(false) {
  assert(remote_bitrate_estimator);
}

ViEReceiver::~ViEReceiver() {
  if (decryption_buffer_) {
    delete[] decryption_buffer_;
    decryption_buffer_ = NULL;
  }
  if (rtp_dump_) {
    rtp_dump_->Stop();
    RtpDump::DestroyRtpDump(rtp_dump_);
    rtp_dump_ = NULL;
  }
}

int ViEReceiver::RegisterExternalDecryption(Encryption* decryption) {
  CriticalSectionScoped cs(receive_cs_.get());
  if (external_decryption_) {
    return -1;
  }
  decryption_buffer_ = new WebRtc_UWord8[kViEMaxMtu];
  if (decryption_buffer_ == NULL) {
    return -1;
  }
  external_decryption_ = decryption;
  return 0;
}

int ViEReceiver::DeregisterExternalDecryption() {
  CriticalSectionScoped cs(receive_cs_.get());
  if (external_decryption_ == NULL) {
    return -1;
  }
  external_decryption_ = NULL;
  return 0;
}

void ViEReceiver::SetRtpRtcpModule(RtpRtcp* module) {
  rtp_rtcp_ = module;
}

void ViEReceiver::RegisterSimulcastRtpRtcpModules(
    const std::list<RtpRtcp*>& rtp_modules) {
  CriticalSectionScoped cs(receive_cs_.get());
  rtp_rtcp_simulcast_.clear();

  if (!rtp_modules.empty()) {
    rtp_rtcp_simulcast_.insert(rtp_rtcp_simulcast_.begin(),
                               rtp_modules.begin(),
                               rtp_modules.end());
  }
}

void ViEReceiver::IncomingRTPPacket(const WebRtc_Word8* rtp_packet,
                                    const WebRtc_Word32 rtp_packet_length,
                                    const char* from_ip,
                                    const WebRtc_UWord16 from_port) {
  InsertRTPPacket(rtp_packet, rtp_packet_length);
}

void ViEReceiver::IncomingRTCPPacket(const WebRtc_Word8* rtcp_packet,
                                     const WebRtc_Word32 rtcp_packet_length,
                                     const char* from_ip,
                                     const WebRtc_UWord16 from_port) {
  InsertRTCPPacket(rtcp_packet, rtcp_packet_length);
}

int ViEReceiver::ReceivedRTPPacket(const void* rtp_packet,
                                   int rtp_packet_length) {
  if (!receiving_) {
    return -1;
  }
  return InsertRTPPacket((const WebRtc_Word8*) rtp_packet, rtp_packet_length);
}

int ViEReceiver::ReceivedRTCPPacket(const void* rtcp_packet,
                                    int rtcp_packet_length) {
  if (!receiving_) {
    return -1;
  }
  return InsertRTCPPacket((const WebRtc_Word8*) rtcp_packet,
                          rtcp_packet_length);
}

WebRtc_Word32 ViEReceiver::OnReceivedPayloadData(
    const WebRtc_UWord8* payload_data, const WebRtc_UWord16 payload_size,
    const WebRtcRTPHeader* rtp_header) {
  if (rtp_header == NULL) {
    return 0;
  }

  // TODO(holmer): Make sure packets reconstructed using FEC are not passed to
  // the bandwidth estimator.
  const int packet_size = payload_size + rtp_header->header.paddingLength;
  uint32_t compensated_timestamp = rtp_header->header.timestamp +
      rtp_header->extension.transmissionTimeOffset;
  remote_bitrate_estimator_->IncomingPacket(rtp_header->header.ssrc,
                                            packet_size,
                                            TickTime::MillisecondTimestamp(),
                                            compensated_timestamp);
  if (vcm_->IncomingPacket(payload_data, payload_size, *rtp_header) != 0) {
    // Check this...
    return -1;
  }
  return 0;
}

void ViEReceiver::OnSendReportReceived(const WebRtc_Word32 id,
                                       const WebRtc_UWord32 senderSSRC,
{'fraction_non_alphanumeric': 0.08502550765229569, 'fraction_numerical': 0.008402520756226868, 'mean_word_length': 2.86023166023166, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5611387', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3120, 'n_words': 604}