Have AudioSendStream register CNG payload types with the RtpRtcpModule.

TBR=kwiberg@webrtc.org # Turn perf-bots green again

BUG=webrtc:5806

Review-Url: https://codereview.webrtc.org/2844803003
Cr-Original-Commit-Position: refs/heads/master@{#17911}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: 3b9ff38d8a10d46f69680c5634b4814fb02378e2
diff --git a/audio/audio_send_stream.cc b/audio/audio_send_stream.cc
index 28541ca..d413a96 100644
--- a/audio/audio_send_stream.cc
+++ b/audio/audio_send_stream.cc
@@ -28,7 +28,7 @@
 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
 #include "webrtc/modules/congestion_controller/include/send_side_congestion_controller.h"
 #include "webrtc/modules/pacing/paced_sender.h"
-#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
+#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
 #include "webrtc/voice_engine/channel_proxy.h"
 #include "webrtc/voice_engine/include/voe_base.h"
 #include "webrtc/voice_engine/transmit_mixer.h"
@@ -421,6 +421,10 @@
     cng_config.speech_encoder = std::move(encoder);
     cng_config.vad_mode = Vad::kVadNormal;
     encoder.reset(new AudioEncoderCng(std::move(cng_config)));
+
+    stream->RegisterCngPayloadType(
+        *spec.cng_payload_type,
+        new_config.send_codec_spec->format.clockrate_hz);
   }
 
   stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type,
@@ -498,6 +502,14 @@
     return;
   }
 
+  // Register the CNG payload type if it's been added, don't do anything if CNG
+  // is removed. Payload types must not be redefined.
+  if (new_config.send_codec_spec->cng_payload_type) {
+    stream->RegisterCngPayloadType(
+        *new_config.send_codec_spec->cng_payload_type,
+        new_config.send_codec_spec->format.clockrate_hz);
+  }
+
   // Wrap or unwrap the encoder in an AudioEncoderCNG.
   stream->channel_proxy_->ModifyEncoder(
       [&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
@@ -572,5 +584,21 @@
   thread_sync_event.Wait(rtc::Event::kForever);
 }
 
+void AudioSendStream::RegisterCngPayloadType(int payload_type,
+                                             int clockrate_hz) {
+  RtpRtcp* rtpRtcpModule = nullptr;
+  RtpReceiver* rtpReceiver = nullptr;  // Unused, but required for call.
+  channel_proxy_->GetRtpRtcp(&rtpRtcpModule, &rtpReceiver);
+  const CodecInst codec = {payload_type, "CN", clockrate_hz, 0, 1, 0};
+  if (rtpRtcpModule->RegisterSendPayload(codec) != 0) {
+    rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
+    if (rtpRtcpModule->RegisterSendPayload(codec) != 0) {
+      LOG(LS_ERROR) << "RegisterCngPayloadType() failed to register CN to "
+                       "RTP/RTCP module";
+    }
+  }
+}
+
+
 }  // namespace internal
 }  // namespace webrtc
diff --git a/audio/audio_send_stream.h b/audio/audio_send_stream.h
index 56e099f..dcc28c9 100644
--- a/audio/audio_send_stream.h
+++ b/audio/audio_send_stream.h
@@ -93,6 +93,8 @@
   void ConfigureBitrateObserver(int min_bitrate_bps, int max_bitrate_bps);
   void RemoveBitrateObserver();
 
+  void RegisterCngPayloadType(int payload_type, int clockrate_hz);
+
   rtc::ThreadChecker worker_thread_checker_;
   rtc::ThreadChecker pacer_thread_checker_;
   rtc::TaskQueue* worker_queue_;
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index 3fbbb62..dba83ca 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -25,6 +25,7 @@
 #include "webrtc/modules/congestion_controller/include/mock/mock_congestion_observer.h"
 #include "webrtc/modules/congestion_controller/include/send_side_congestion_controller.h"
 #include "webrtc/modules/pacing/paced_sender.h"
+#include "webrtc/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
 #include "webrtc/modules/rtp_rtcp/mocks/mock_rtcp_rtt_stats.h"
 #include "webrtc/test/gtest.h"
 #include "webrtc/test/mock_voe_channel_proxy.h"
@@ -217,6 +218,12 @@
   void SetupDefaultChannelProxy(bool audio_bwe_enabled) {
     using testing::StrEq;
     channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
+    EXPECT_CALL(*channel_proxy_, GetRtpRtcp(_, _))
+        .WillRepeatedly(Invoke(
+            [this](RtpRtcp** rtp_rtcp_module, RtpReceiver** rtp_receiver) {
+              *rtp_rtcp_module = &this->rtp_rtcp_;
+              *rtp_receiver = nullptr;  // Not deemed necessary for tests yet.
+            }));
     EXPECT_CALL(*channel_proxy_, SetRTCPStatus(true)).Times(1);
     EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kSsrc)).Times(1);
     EXPECT_CALL(*channel_proxy_, SetRTCP_CNAME(StrEq(kCName))).Times(1);
@@ -321,6 +328,7 @@
   AudioProcessing::AudioProcessingStatistics audio_processing_stats_;
   FakeRtpTransportController fake_transport_;
   MockRtcEventLog event_log_;
+  MockRtpRtcp rtp_rtcp_;
   MockRtcpRttStats rtcp_rtt_stats_;
   testing::NiceMock<MockLimitObserver> limit_observer_;
   BitrateAllocator bitrate_allocator_;