Voe::Channel: Turned GetPlayoutFrequency into GetRtpTimestampRateHz.

This gets rid of a bit of codec-specific code in VoE.

BUG=webrtc:5805

Review-Url: https://codereview.webrtc.org/2355483003
Cr-Original-Commit-Position: refs/heads/master@{#14614}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: e280cdeb744c2e0c40fa1da24dce59436eda2e39
diff --git a/modules/audio_coding/acm2/acm_receiver.cc b/modules/audio_coding/acm2/acm_receiver.cc
index 0b19310..73518b8 100644
--- a/modules/audio_coding/acm2/acm_receiver.cc
+++ b/modules/audio_coding/acm2/acm_receiver.cc
@@ -98,6 +98,8 @@
       }
     } else {
       last_audio_decoder_ = ci;
+      last_audio_format_ = neteq_->GetDecoderFormat(ci->pltype);
+      RTC_DCHECK(last_audio_format_);
       last_packet_sample_rate_hz_ = rtc::Optional<int>(ci->plfreq);
     }
 
@@ -263,6 +265,7 @@
   rtc::CritScope lock(&crit_sect_);
   neteq_->RemoveAllPayloadTypes();
   last_audio_decoder_ = rtc::Optional<CodecInst>();
+  last_audio_format_ = rtc::Optional<SdpAudioFormat>();
   last_packet_sample_rate_hz_ = rtc::Optional<int>();
 }
 
@@ -275,6 +278,7 @@
   }
   if (last_audio_decoder_ && payload_type == last_audio_decoder_->pltype) {
     last_audio_decoder_ = rtc::Optional<CodecInst>();
+    last_audio_format_ = rtc::Optional<SdpAudioFormat>();
     last_packet_sample_rate_hz_ = rtc::Optional<int>();
   }
   return 0;
@@ -297,6 +301,11 @@
   return 0;
 }
 
+rtc::Optional<SdpAudioFormat> AcmReceiver::LastAudioFormat() const {
+  rtc::CritScope lock(&crit_sect_);
+  return last_audio_format_;
+}
+
 void AcmReceiver::GetNetworkStatistics(NetworkStatistics* acm_stat) {
   NetEqNetworkStatistics neteq_stat;
   // NetEq function always returns zero, so we don't check the return value.
diff --git a/modules/audio_coding/acm2/acm_receiver.h b/modules/audio_coding/acm2/acm_receiver.h
index a9550fb..6415074 100644
--- a/modules/audio_coding/acm2/acm_receiver.h
+++ b/modules/audio_coding/acm2/acm_receiver.h
@@ -209,6 +209,8 @@
   //
   int LastAudioCodec(CodecInst* codec) const;
 
+  rtc::Optional<SdpAudioFormat> LastAudioFormat() const;
+
   //
   // Get a decoder given its registered payload-type.
   //
@@ -273,6 +275,7 @@
 
   rtc::CriticalSection crit_sect_;
   rtc::Optional<CodecInst> last_audio_decoder_ GUARDED_BY(crit_sect_);
+  rtc::Optional<SdpAudioFormat> last_audio_format_ GUARDED_BY(crit_sect_);
   ACMResampler resampler_ GUARDED_BY(crit_sect_);
   std::unique_ptr<int16_t[]> last_audio_buffer_ GUARDED_BY(crit_sect_);
   CallStatistics call_stats_ GUARDED_BY(crit_sect_);
diff --git a/modules/audio_coding/acm2/audio_coding_module.cc b/modules/audio_coding/acm2/audio_coding_module.cc
index a845c01..ee1034b 100644
--- a/modules/audio_coding/acm2/audio_coding_module.cc
+++ b/modules/audio_coding/acm2/audio_coding_module.cc
@@ -138,6 +138,8 @@
   // Get current received codec.
   int ReceiveCodec(CodecInst* current_codec) const override;
 
+  rtc::Optional<SdpAudioFormat> ReceiveFormat() const override;
+
   // Incoming packet from network parsed and ready for decode.
   int IncomingPacket(const uint8_t* incoming_payload,
                      const size_t payload_length,
@@ -1087,6 +1089,11 @@
   return receiver_.LastAudioCodec(current_codec);
 }
 
+rtc::Optional<SdpAudioFormat> AudioCodingModuleImpl::ReceiveFormat() const {
+  rtc::CritScope lock(&acm_crit_sect_);
+  return receiver_.LastAudioFormat();
+}
+
 // Incoming packet from network parsed and ready for decode.
 int AudioCodingModuleImpl::IncomingPacket(const uint8_t* incoming_payload,
                                           const size_t payload_length,
diff --git a/modules/audio_coding/include/audio_coding_module.h b/modules/audio_coding/include/audio_coding_module.h
index e62bbd7..a0dafec 100644
--- a/modules/audio_coding/include/audio_coding_module.h
+++ b/modules/audio_coding/include/audio_coding_module.h
@@ -553,6 +553,17 @@
   virtual int32_t ReceiveCodec(CodecInst* curr_receive_codec) const = 0;
 
   ///////////////////////////////////////////////////////////////////////////
+  // rtc::Optional<SdpAudioFormat> ReceiveFormat()
+  // Get the format associated with last received payload.
+  //
+  // Return value:
+  //    An SdpAudioFormat describing the format associated with the last
+  //    received payload.
+  //    An empty Optional if no payload has yet been received.
+  //
+  virtual rtc::Optional<SdpAudioFormat> ReceiveFormat() const = 0;
+
+  ///////////////////////////////////////////////////////////////////////////
   // int32_t IncomingPacket()
   // Call this function to insert a parsed RTP packet into ACM.
   //
diff --git a/voice_engine/channel.cc b/voice_engine/channel.cc
index 0965963..6dcef5f 100644
--- a/voice_engine/channel.cc
+++ b/voice_engine/channel.cc
@@ -718,7 +718,7 @@
         rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
     audioFrame->elapsed_time_ms_ =
         (unwrap_timestamp - capture_start_rtp_time_stamp_) /
-        (GetPlayoutFrequency() / 1000);
+        (GetRtpTimestampRateHz() / 1000);
 
     {
       rtc::CritScope lock(&ts_stats_lock_);
@@ -3162,7 +3162,7 @@
   uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
 
   // Remove the playout delay.
-  playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
+  playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
 
   WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
                "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
@@ -3222,25 +3222,15 @@
   return error;
 }
 
-int32_t Channel::GetPlayoutFrequency() const {
-  int32_t playout_frequency = audio_coding_->PlayoutFrequency();
-  CodecInst current_recive_codec;
-  if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
-    if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
-      // Even though the actual sampling rate for G.722 audio is
-      // 16,000 Hz, the RTP clock rate for the G722 payload format is
-      // 8,000 Hz because that value was erroneously assigned in
-      // RFC 1890 and must remain unchanged for backward compatibility.
-      playout_frequency = 8000;
-    } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
-      // We are resampling Opus internally to 32,000 Hz until all our
-      // DSP routines can operate at 48,000 Hz, but the RTP clock
-      // rate for the Opus payload format is standardized to 48,000 Hz,
-      // because that is the maximum supported decoding sampling rate.
-      playout_frequency = 48000;
-    }
-  }
-  return playout_frequency;
+int Channel::GetRtpTimestampRateHz() const {
+  const auto format = audio_coding_->ReceiveFormat();
+  // Default to the playout frequency if we've not gotten any packets yet.
+  // TODO(ossu): Zero clockrate can only happen if we've added an external
+  // decoder for a format we don't support internally. Remove once that way of
+  // adding decoders is gone!
+  return (format && format->clockrate_hz != 0)
+             ? format->clockrate_hz
+             : audio_coding_->PlayoutFrequency();
 }
 
 int64_t Channel::GetRTT(bool allow_associate_channel) const {
diff --git a/voice_engine/channel.h b/voice_engine/channel.h
index fc123e3..bf279d8 100644
--- a/voice_engine/channel.h
+++ b/voice_engine/channel.h
@@ -446,7 +446,7 @@
                                 RTPExtensionType type,
                                 unsigned char id);
 
-  int32_t GetPlayoutFrequency() const;
+  int GetRtpTimestampRateHz() const;
   int64_t GetRTT(bool allow_associate_channel) const;
 
   rtc::CriticalSection _fileCritSect;