Remove CodecInst pt.2

The following APIs on AudioCodingModule are deprecated with this CL:
  static int NumberOfCodecs();
  static int Codec(int, CodecInst*);
  static int Codec(const char*, CodecInst*, int, size_t);
  static int Codec(const char*, int, size_t);
  absl::optional<CodecInst> SendCodec() const;
  bool RegisterReceiveCodec(int, const SdpAudioFormat&);
  int RegisterExternalReceiveCodec(int, AudioDecoder*, int, int, const std::string&);
  int UnregisterReceiveCodec(uint8_t);
  int32_t ReceiveCodec(CodecInst*);
  absl::optional<SdpAudioFormat> ReceiveFormat();

As well as this method on RtpRtcp module:
  int32_t RegisterSendPayload(const CodecInst&);

Bug: webrtc:7626
Change-Id: I1230732136f1fe9048cf74afdeab767ca57ac9ce
Reviewed-on: https://webrtc-review.googlesource.com/c/113816
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26025}
diff --git a/modules/audio_coding/acm2/acm_receiver.cc b/modules/audio_coding/acm2/acm_receiver.cc
index d3af7c0..ea84036 100644
--- a/modules/audio_coding/acm2/acm_receiver.cc
+++ b/modules/audio_coding/acm2/acm_receiver.cc
@@ -20,7 +20,6 @@
 #include "api/audio_codecs/audio_decoder.h"
 #include "modules/audio_coding/acm2/acm_resampler.h"
 #include "modules/audio_coding/acm2/call_statistics.h"
-#include "modules/audio_coding/acm2/rent_a_codec.h"
 #include "modules/audio_coding/neteq/include/neteq.h"
 #include "modules/audio_coding/neteq/neteq_decoder_enum.h"
 #include "modules/include/module_common_types.h"
@@ -62,7 +61,10 @@
 
 absl::optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
   rtc::CritScope lock(&crit_sect_);
-  return last_packet_sample_rate_hz_;
+  if (!last_decoder_) {
+    return absl::nullopt;
+  }
+  return last_decoder_->second.clockrate_hz;
 }
 
 int AcmReceiver::last_output_sample_rate_hz() const {
@@ -71,45 +73,44 @@
 
 int AcmReceiver::InsertPacket(const WebRtcRTPHeader& rtp_header,
                               rtc::ArrayView<const uint8_t> incoming_payload) {
-  uint32_t receive_timestamp = 0;
-  const RTPHeader* header = &rtp_header.header;  // Just a shorthand.
-
   if (incoming_payload.empty()) {
     neteq_->InsertEmptyPacket(rtp_header.header);
     return 0;
   }
 
+  const RTPHeader& header = rtp_header.header;  // Just a shorthand.
+  int payload_type = header.payloadType;
+  auto format = neteq_->GetDecoderFormat(payload_type);
+  if (format && absl::EqualsIgnoreCase(format->name, "red")) {
+    // This is a RED packet. Get the format of the audio codec.
+    payload_type = incoming_payload[0] & 0x7f;
+    format = neteq_->GetDecoderFormat(payload_type);
+  }
+  if (!format) {
+    RTC_LOG_F(LS_ERROR) << "Payload-type "
+                        << payload_type
+                        << " is not registered.";
+    return -1;
+  }
+
   {
     rtc::CritScope lock(&crit_sect_);
-
-    const absl::optional<CodecInst> ci =
-        RtpHeaderToDecoder(*header, incoming_payload[0]);
-    if (!ci) {
-      RTC_LOG_F(LS_ERROR) << "Payload-type "
-                          << static_cast<int>(header->payloadType)
-                          << " is not registered.";
-      return -1;
-    }
-    receive_timestamp = NowInTimestamp(ci->plfreq);
-
-    if (absl::EqualsIgnoreCase(ci->plname, "cn")) {
-      if (last_audio_decoder_ && last_audio_decoder_->channels > 1) {
+    if (absl::EqualsIgnoreCase(format->name, "cn")) {
+      if (last_decoder_ && last_decoder_->second.num_channels > 1) {
         // This is a CNG and the audio codec is not mono, so skip pushing in
         // packets into NetEq.
         return 0;
       }
     } else {
-      last_audio_decoder_ = ci;
-      last_audio_format_ = neteq_->GetDecoderFormat(ci->pltype);
-      RTC_DCHECK(last_audio_format_);
-      last_packet_sample_rate_hz_ = ci->plfreq;
+      RTC_DCHECK(format);
+      last_decoder_ = std::make_pair(payload_type, *format);
     }
   }  // |crit_sect_| is released.
 
-  if (neteq_->InsertPacket(rtp_header.header, incoming_payload,
-                           receive_timestamp) < 0) {
+  uint32_t receive_timestamp = NowInTimestamp(format->clockrate_hz);
+  if (neteq_->InsertPacket(header, incoming_payload, receive_timestamp) < 0) {
     RTC_LOG(LERROR) << "AcmReceiver::InsertPacket "
-                    << static_cast<int>(header->payloadType)
+                    << static_cast<int>(header.payloadType)
                     << " Failed to insert packet";
     return -1;
   }
@@ -186,85 +187,6 @@
   neteq_->SetCodecs(codecs);
 }
 
-int32_t AcmReceiver::AddCodec(int acm_codec_id,
-                              uint8_t payload_type,
-                              size_t channels,
-                              int /*sample_rate_hz*/,
-                              AudioDecoder* audio_decoder,
-                              const std::string& name) {
-  // TODO(kwiberg): This function has been ignoring the |sample_rate_hz|
-  // argument for a long time. Arguably, it should simply be removed.
-
-  const auto neteq_decoder = [acm_codec_id, channels]() -> NetEqDecoder {
-    if (acm_codec_id == -1)
-      return NetEqDecoder::kDecoderArbitrary;  // External decoder.
-    const absl::optional<RentACodec::CodecId> cid =
-        RentACodec::CodecIdFromIndex(acm_codec_id);
-    RTC_DCHECK(cid) << "Invalid codec index: " << acm_codec_id;
-    const absl::optional<NetEqDecoder> ned =
-        RentACodec::NetEqDecoderFromCodecId(*cid, channels);
-    RTC_DCHECK(ned) << "Invalid codec ID: " << static_cast<int>(*cid);
-    return *ned;
-  }();
-  const absl::optional<SdpAudioFormat> new_format =
-      NetEqDecoderToSdpAudioFormat(neteq_decoder);
-
-  rtc::CritScope lock(&crit_sect_);
-
-  const auto old_format = neteq_->GetDecoderFormat(payload_type);
-  if (old_format && new_format && *old_format == *new_format) {
-    // Re-registering the same codec. Do nothing and return.
-    return 0;
-  }
-
-  if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
-    RTC_LOG(LERROR) << "Cannot remove payload "
-                    << static_cast<int>(payload_type);
-    return -1;
-  }
-
-  int ret_val;
-  if (!audio_decoder) {
-    ret_val = neteq_->RegisterPayloadType(neteq_decoder, name, payload_type);
-  } else {
-    ret_val = neteq_->RegisterExternalDecoder(audio_decoder, neteq_decoder,
-                                              name, payload_type);
-  }
-  if (ret_val != NetEq::kOK) {
-    RTC_LOG(LERROR) << "AcmReceiver::AddCodec " << acm_codec_id
-                    << static_cast<int>(payload_type)
-                    << " channels: " << channels;
-    return -1;
-  }
-  return 0;
-}
-
-bool AcmReceiver::AddCodec(int rtp_payload_type,
-                           const SdpAudioFormat& audio_format) {
-  const auto old_format = neteq_->GetDecoderFormat(rtp_payload_type);
-  if (old_format && *old_format == audio_format) {
-    // Re-registering the same codec. Do nothing and return.
-    return true;
-  }
-
-  if (neteq_->RemovePayloadType(rtp_payload_type) != NetEq::kOK) {
-    RTC_LOG(LERROR)
-        << "AcmReceiver::AddCodec: Could not remove existing decoder"
-           " for payload type "
-        << rtp_payload_type;
-    return false;
-  }
-
-  const bool success =
-      neteq_->RegisterPayloadType(rtp_payload_type, audio_format);
-  if (!success) {
-    RTC_LOG(LERROR) << "AcmReceiver::AddCodec failed for payload type "
-                    << rtp_payload_type << ", decoder format "
-                    << rtc::ToString(audio_format);
-  }
-  return success;
-}
-
 void AcmReceiver::FlushBuffers() {
   neteq_->FlushBuffers();
 }
@@ -272,24 +194,7 @@
 void AcmReceiver::RemoveAllCodecs() {
   rtc::CritScope lock(&crit_sect_);
   neteq_->RemoveAllPayloadTypes();
-  last_audio_decoder_ = absl::nullopt;
-  last_audio_format_ = absl::nullopt;
-  last_packet_sample_rate_hz_ = absl::nullopt;
-}
-
-int AcmReceiver::RemoveCodec(uint8_t payload_type) {
-  rtc::CritScope lock(&crit_sect_);
-  if (neteq_->RemovePayloadType(payload_type) != NetEq::kOK) {
-    RTC_LOG(LERROR) << "AcmReceiver::RemoveCodec "
-                    << static_cast<int>(payload_type);
-    return -1;
-  }
-  if (last_audio_decoder_ && payload_type == last_audio_decoder_->pltype) {
-    last_audio_decoder_ = absl::nullopt;
-    last_audio_format_ = absl::nullopt;
-    last_packet_sample_rate_hz_ = absl::nullopt;
-  }
-  return 0;
+  last_decoder_ = absl::nullopt;
 }
 
 absl::optional<uint32_t> AcmReceiver::GetPlayoutTimestamp() {
@@ -304,18 +209,14 @@
   return neteq_->TargetDelayMs();
 }
 
-int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
+absl::optional<std::pair<int, SdpAudioFormat>>
+    AcmReceiver::LastDecoder() const {
   rtc::CritScope lock(&crit_sect_);
-  if (!last_audio_decoder_) {
-    return -1;
+  if (!last_decoder_) {
+    return absl::nullopt;
   }
-  *codec = *last_audio_decoder_;
-  return 0;
-}
-
-absl::optional<SdpAudioFormat> AcmReceiver::LastAudioFormat() const {
-  rtc::CritScope lock(&crit_sect_);
-  return last_audio_format_;
+  RTC_DCHECK_NE(-1, last_decoder_->first);  // Payload type should be valid.
+  return last_decoder_;
 }
 
 void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
@@ -354,26 +255,6 @@
       neteq_operations_and_state.packet_buffer_flushes;
 }
 
-int AcmReceiver::DecoderByPayloadType(uint8_t payload_type,
-                                      CodecInst* codec) const {
-  rtc::CritScope lock(&crit_sect_);
-  const absl::optional<CodecInst> ci = neteq_->GetDecoder(payload_type);
-  if (ci) {
-    *codec = *ci;
-    return 0;
-  } else {
-    RTC_LOG(LERROR) << "AcmReceiver::DecoderByPayloadType "
-                    << static_cast<int>(payload_type);
-    return -1;
-  }
-}
-
-absl::optional<SdpAudioFormat> AcmReceiver::DecoderByPayloadType(
-    int payload_type) const {
-  rtc::CritScope lock(&crit_sect_);
-  return neteq_->GetDecoderFormat(payload_type);
-}
-
 int AcmReceiver::EnableNack(size_t max_nack_list_size) {
   neteq_->EnableNack(max_nack_list_size);
   return 0;
@@ -393,19 +274,6 @@
   // TODO(turajs): Should NetEq Buffer be flushed?
 }
 
-const absl::optional<CodecInst> AcmReceiver::RtpHeaderToDecoder(
-    const RTPHeader& rtp_header,
-    uint8_t first_payload_byte) const {
-  const absl::optional<CodecInst> ci =
-      neteq_->GetDecoder(rtp_header.payloadType);
-  if (ci && absl::EqualsIgnoreCase(ci->plname, "red")) {
-    // This is a RED packet. Get the payload of the audio codec.
-    return neteq_->GetDecoder(first_payload_byte & 0x7f);
-  } else {
-    return ci;
-  }
-}
-
 uint32_t AcmReceiver::NowInTimestamp(int decoder_sampling_rate) const {
   // Down-cast the time to (32-6)-bit since we only care about
   // the least significant bits. (32-6) bits cover 2^(32-6) = 67108864 ms.