Add new method AcmReceiver::last_packet_sample_rate_hz()

This change allows us to delete AcmReceiver::last_audio_codec_id().

BUG=webrtc:3520

Review URL: https://codereview.webrtc.org/1467183002

Cr-Commit-Position: refs/heads/master@{#10756}
diff --git a/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc b/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc
index 1775029..6c28933 100644
--- a/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc
+++ b/webrtc/modules/audio_coding/main/acm2/acm_receiver.cc
@@ -156,6 +156,11 @@
   return neteq_->LeastRequiredDelayMs();
 }
 
+rtc::Optional<int> AcmReceiver::last_packet_sample_rate_hz() const {
+  CriticalSectionScoped lock(crit_sect_.get());
+  return last_packet_sample_rate_hz_;
+}
+
 int AcmReceiver::last_output_sample_rate_hz() const {
   return neteq_->last_output_sample_rate_hz();
 }
@@ -190,6 +195,7 @@
         decoder->acm_codec_id !=
             *RentACodec::CodecIndexFromId(RentACodec::CodecId::kAVT)) {
       last_audio_decoder_ = decoder;
+      last_packet_sample_rate_hz_ = rtc::Optional<int>(decoder->sample_rate_hz);
     }
 
   }  // |crit_sect_| is released.
@@ -392,6 +398,7 @@
 
   // No codec is registered, invalidate last audio decoder.
   last_audio_decoder_ = nullptr;
+  last_packet_sample_rate_hz_ = rtc::Optional<int>();
   return ret_val;
 }
 
@@ -405,8 +412,10 @@
     LOG(LERROR) << "AcmReceiver::RemoveCodec" << static_cast<int>(payload_type);
     return -1;
   }
-  if (last_audio_decoder_ == &it->second)
+  if (last_audio_decoder_ == &it->second) {
     last_audio_decoder_ = nullptr;
+    last_packet_sample_rate_hz_ = rtc::Optional<int>();
+  }
   decoders_.erase(it);
   return 0;
 }
@@ -420,11 +429,6 @@
   return neteq_->GetPlayoutTimestamp(timestamp);
 }
 
-int AcmReceiver::last_audio_codec_id() const {
-  CriticalSectionScoped lock(crit_sect_.get());
-  return last_audio_decoder_ ? last_audio_decoder_->acm_codec_id : -1;
-}
-
 int AcmReceiver::LastAudioCodec(CodecInst* codec) const {
   CriticalSectionScoped lock(crit_sect_.get());
   if (!last_audio_decoder_) {
diff --git a/webrtc/modules/audio_coding/main/acm2/acm_receiver.h b/webrtc/modules/audio_coding/main/acm2/acm_receiver.h
index f02605b..bcedacd 100644
--- a/webrtc/modules/audio_coding/main/acm2/acm_receiver.h
+++ b/webrtc/modules/audio_coding/main/acm2/acm_receiver.h
@@ -15,6 +15,7 @@
 #include <vector>
 
 #include "webrtc/base/array_view.h"
+#include "webrtc/base/optional.h"
 #include "webrtc/base/scoped_ptr.h"
 #include "webrtc/base/thread_annotations.h"
 #include "webrtc/common_audio/vad/include/webrtc_vad.h"
@@ -154,6 +155,12 @@
   //
   void ResetInitialDelay();
 
+  // Returns the sample rate of the decoder associated with the last incoming
+  // packet. If no packet of a registered non-CNG codec has been received, the
+  // return value is empty. Also, if the decoder was unregistered since the last
+  // packet was inserted, the return value is empty.
+  rtc::Optional<int> last_packet_sample_rate_hz() const;
+
   // Returns last_output_sample_rate_hz from the NetEq instance.
   int last_output_sample_rate_hz() const;
 
@@ -213,13 +220,6 @@
   bool GetPlayoutTimestamp(uint32_t* timestamp);
 
   //
-  // Return the index of the codec associated with the last non-CNG/non-DTMF
-  // received payload. If no non-CNG/non-DTMF payload is received -1 is
-  // returned.
-  //
-  int last_audio_codec_id() const;  // TODO(turajs): can be inline.
-
-  //
   // Get the audio codec associated with the last non-CNG/non-DTMF received
   // payload. If no non-CNG/non-DTMF packet is received -1 is returned,
   // otherwise return 0.
@@ -295,6 +295,7 @@
   bool vad_enabled_;
   Clock* clock_;  // TODO(henrik.lundin) Make const if possible.
   bool resampled_last_output_frame_ GUARDED_BY(crit_sect_);
+  rtc::Optional<int> last_packet_sample_rate_hz_ GUARDED_BY(crit_sect_);
 };
 
 }  // namespace acm2
diff --git a/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc b/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc
index 3bda116..8f43ac4 100644
--- a/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc
+++ b/webrtc/modules/audio_coding/main/acm2/acm_receiver_unittest_oldapi.cc
@@ -330,7 +330,7 @@
 
   // Has received, only, DTX. Last Audio codec is undefined.
   EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec));
-  EXPECT_EQ(-1, receiver_->last_audio_codec_id());
+  EXPECT_FALSE(receiver_->last_packet_sample_rate_hz());
 
   for (auto id : kCodecId) {
     const CodecIdInst c(id);
@@ -344,7 +344,8 @@
     // of type "speech."
     ASSERT_TRUE(packet_sent_);
     ASSERT_EQ(kAudioFrameSpeech, last_frame_type_);
-    EXPECT_EQ(c.id, receiver_->last_audio_codec_id());
+    EXPECT_EQ(rtc::Optional<int>(c.inst.plfreq),
+              receiver_->last_packet_sample_rate_hz());
 
     // Set VAD on to send DTX. Then check if the "Last Audio codec" returns
     // the expected codec.
@@ -356,7 +357,8 @@
       InsertOnePacketOfSilence(c.id);
       ASSERT_TRUE(packet_sent_);
     }
-    EXPECT_EQ(c.id, receiver_->last_audio_codec_id());
+    EXPECT_EQ(rtc::Optional<int>(c.inst.plfreq),
+              receiver_->last_packet_sample_rate_hz());
     EXPECT_EQ(0, receiver_->LastAudioCodec(&codec));
     EXPECT_TRUE(CodecsEqual(c.inst, codec));
   }
diff --git a/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc b/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc
index 19ae4cb..5d18bda 100644
--- a/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc
+++ b/webrtc/modules/audio_coding/main/acm2/audio_coding_module_impl.cc
@@ -525,14 +525,9 @@
 
 // Get current receive frequency.
 int AudioCodingModuleImpl::ReceiveFrequency() const {
-  WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceAudioCoding, id_,
-               "ReceiveFrequency()");
-
-  CriticalSectionScoped lock(acm_crit_sect_.get());
-
-  auto codec_id = RentACodec::CodecIdFromIndex(receiver_.last_audio_codec_id());
-  return codec_id ? RentACodec::CodecInstById(*codec_id)->plfreq
-                  : receiver_.last_output_sample_rate_hz();
+  const auto last_packet_sample_rate = receiver_.last_packet_sample_rate_hz();
+  return last_packet_sample_rate ? *last_packet_sample_rate
+                                 : receiver_.last_output_sample_rate_hz();
 }
 
 // Get current playout frequency.