Move ownership of RTPSenderAudio to ChannelSend.

This change takes out responsibility for packetization from the
RtpRtcp class, and deletes the method RtpRtcp::SendOutgoingData.

Video packetization was similarly moved in cl
https://webrtc-review.googlesource.com/c/src/+/123187

Bug: webrtc:7135
Change-Id: I0953125a5ca22a2ce51761b83693e0bb8ea74cd8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/125721
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27000}
diff --git a/audio/audio_send_stream.cc b/audio/audio_send_stream.cc
index d3ec157..e12e245 100644
--- a/audio/audio_send_stream.cc
+++ b/audio/audio_send_stream.cc
@@ -796,8 +796,7 @@
 
 void AudioSendStream::RegisterCngPayloadType(int payload_type,
                                              int clockrate_hz) {
-  rtp_rtcp_module_->RegisterAudioSendPayload(payload_type, "CN", clockrate_hz,
-                                             1, 0);
+  channel_send_->RegisterCngPayloadType(payload_type, clockrate_hz);
 }
 }  // namespace internal
 }  // namespace webrtc
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index 1d4d5b7..df5ee30 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -450,6 +450,7 @@
             stolen_encoder = std::move(*encoder);
             return true;
           }));
+  EXPECT_CALL(*helper.channel_send(), RegisterCngPayloadType(105, 8000));
 
   auto send_stream = helper.CreateAudioSendStream();
 
@@ -501,6 +502,8 @@
   EXPECT_CALL(*helper.channel_send(), SetEncoderForMock(_, _))
       .WillOnce(Return());
 
+  EXPECT_CALL(*helper.channel_send(), RegisterCngPayloadType(105, 8000));
+
   helper.config().send_codec_spec =
       AudioSendStream::Config::SendCodecSpec(9, kG722Format);
   helper.config().send_codec_spec->cng_payload_type = 105;
diff --git a/audio/channel_send.cc b/audio/channel_send.cc
index 705827d..196911a 100644
--- a/audio/channel_send.cc
+++ b/audio/channel_send.cc
@@ -129,6 +129,8 @@
   // Used by AudioSendStream.
   RtpRtcp* GetRtpRtcp() const override;
 
+  void RegisterCngPayloadType(int payload_type, int payload_frequency) override;
+
   // DTMF.
   bool SendTelephoneEventOutband(int event, int duration_ms) override;
   void SetSendTelephoneEventPayloadType(int payload_type,
@@ -236,6 +238,7 @@
   RtcEventLog* const event_log_;
 
   std::unique_ptr<RtpRtcp> _rtpRtcpModule;
+  std::unique_ptr<RTPSenderAudio> rtp_sender_audio_;
 
   std::unique_ptr<AudioCodingModule> audio_coding_;
   uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
@@ -520,10 +523,10 @@
                                   const RTPFragmentationHeader* fragmentation) {
   RTC_DCHECK_RUN_ON(encoder_queue_);
   if (_includeAudioLevelIndication) {
-    // Store current audio level in the RTP/RTCP module.
+    // Store current audio level in the RTP sender.
     // The level will be used in combination with voice-activity state
     // (frameType) to add an RTP header extension
-    _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
+    rtp_sender_audio_->SetAudioLevel(rms_level_.Average());
   }
 
   // E2EE Custom Audio Frame Encryption (This is optional).
@@ -559,14 +562,24 @@
 
   // Push data from ACM to RTP/RTCP-module to deliver audio frame for
   // packetization.
+  if (!_rtpRtcpModule->OnSendingRtpFrame(timeStamp,
+                                         // Leaving the time when this frame was
+                                         // received from the capture device as
+                                         // undefined for voice for now.
+                                         -1, payloadType,
+                                         /*force_sender_report=*/false)) {
+    return false;
+  }
+
+  // RTCPSender has it's own copy of the timestamp offset, added in
+  // RTCPSender::BuildSR, hence we must not add the in the offset for the above
+  // call.
+  // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine
+  // knowledge of the offset to a single place.
+  const uint32_t rtp_timestamp = timeStamp + _rtpRtcpModule->StartTimestamp();
   // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
-  if (!_rtpRtcpModule->SendOutgoingData((FrameType&)frameType, payloadType,
-                                        timeStamp,
-                                        // Leaving the time when this frame was
-                                        // received from the capture device as
-                                        // undefined for voice for now.
-                                        -1, payload.data(), payload.size(),
-                                        fragmentation, nullptr, nullptr)) {
+  if (!rtp_sender_audio_->SendAudio(frameType, payloadType, rtp_timestamp,
+                                    payload.data(), payload.size())) {
     RTC_DLOG(LS_ERROR)
         << "ChannelSend::SendData() failed to send data to RTP/RTCP module";
     return -1;
@@ -687,6 +700,7 @@
 
   configuration.clock = clock;
   configuration.audio = true;
+  configuration.clock = Clock::GetRealTimeClock();
   configuration.outgoing_transport = rtp_transport;
 
   configuration.paced_sender = rtp_packet_sender_proxy_.get();
@@ -703,6 +717,9 @@
   _rtpRtcpModule = RtpRtcp::Create(configuration);
   _rtpRtcpModule->SetSendingMediaStatus(false);
 
+  rtp_sender_audio_ = absl::make_unique<RTPSenderAudio>(
+      configuration.clock, _rtpRtcpModule->RtpSender());
+
   // We want to invoke the 'TargetRateObserver' and |OnOverheadChanged|
   // callbacks after the audio_coding_ is fully initialized.
   if (media_transport_) {
@@ -797,11 +814,11 @@
 
   // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
   // as well as some other things, so we collect this info and send it along.
-  _rtpRtcpModule->RegisterAudioSendPayload(payload_type,
-                                           "audio",
-                                           encoder->RtpTimestampRateHz(),
-                                           encoder->NumChannels(),
-                                           0);
+  _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type,
+                                               encoder->RtpTimestampRateHz());
+  rtp_sender_audio_->RegisterAudioPayload("audio", payload_type,
+                                          encoder->RtpTimestampRateHz(),
+                                          encoder->NumChannels(), 0);
 
   if (media_transport_) {
     rtc::CritScope cs(&media_transport_lock_);
@@ -926,21 +943,29 @@
   if (!sending_) {
     return false;
   }
-  if (_rtpRtcpModule->SendTelephoneEventOutband(
+  if (rtp_sender_audio_->SendTelephoneEvent(
           event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
-    RTC_DLOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event";
+    RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event";
     return false;
   }
   return true;
 }
 
+void ChannelSend::RegisterCngPayloadType(int payload_type,
+                                         int payload_frequency) {
+  _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
+  rtp_sender_audio_->RegisterAudioPayload("CN", payload_type, payload_frequency,
+                                          1, 0);
+}
+
 void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
                                                    int payload_frequency) {
   RTC_DCHECK_RUN_ON(&worker_thread_checker_);
   RTC_DCHECK_LE(0, payload_type);
   RTC_DCHECK_GE(127, payload_type);
-  _rtpRtcpModule->RegisterAudioSendPayload(payload_type, "telephone-event",
-                                           payload_frequency, 0, 0);
+  _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
+  rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type,
+                                          payload_frequency, 0, 0);
 }
 
 void ChannelSend::SetLocalSSRC(uint32_t ssrc) {
diff --git a/audio/channel_send.h b/audio/channel_send.h
index 7149bbd..0957035 100644
--- a/audio/channel_send.h
+++ b/audio/channel_send.h
@@ -20,6 +20,7 @@
 #include "api/crypto/crypto_options.h"
 #include "api/media_transport_interface.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp.h"
+#include "modules/rtp_rtcp/source/rtp_sender_audio.h"
 #include "rtc_base/function_view.h"
 #include "rtc_base/task_queue.h"
 
@@ -81,6 +82,8 @@
   virtual void ResetSenderCongestionControlObjects() = 0;
   virtual std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const = 0;
   virtual ANAStats GetANAStatistics() const = 0;
+  virtual void RegisterCngPayloadType(int payload_type,
+                                      int payload_frequency) = 0;
   virtual void SetSendTelephoneEventPayloadType(int payload_type,
                                                 int payload_frequency) = 0;
   virtual bool SendTelephoneEventOutband(int event, int duration_ms) = 0;
diff --git a/audio/mock_voe_channel_proxy.h b/audio/mock_voe_channel_proxy.h
index 1b2f96e..cf2fe88 100644
--- a/audio/mock_voe_channel_proxy.h
+++ b/audio/mock_voe_channel_proxy.h
@@ -95,6 +95,8 @@
   MOCK_CONST_METHOD0(GetRTCPStatistics, CallSendStatistics());
   MOCK_CONST_METHOD0(GetRemoteRTCPReportBlocks, std::vector<ReportBlock>());
   MOCK_CONST_METHOD0(GetANAStatistics, ANAStats());
+  MOCK_METHOD2(RegisterCngPayloadType,
+               void(int payload_type, int payload_frequency));
   MOCK_METHOD2(SetSendTelephoneEventPayloadType,
                void(int payload_type, int payload_frequency));
   MOCK_METHOD2(SendTelephoneEventOutband, bool(int event, int duration_ms));
diff --git a/modules/rtp_rtcp/include/rtp_rtcp.h b/modules/rtp_rtcp/include/rtp_rtcp.h
index 8e1f98b..34ccfeb 100644
--- a/modules/rtp_rtcp/include/rtp_rtcp.h
+++ b/modules/rtp_rtcp/include/rtp_rtcp.h
@@ -146,11 +146,6 @@
   // FEC/ULP/RED overhead (when FEC is enabled).
   virtual size_t MaxRtpPacketSize() const = 0;
 
-  virtual void RegisterAudioSendPayload(int payload_type,
-                                        absl::string_view payload_name,
-                                        int frequency,
-                                        int channels,
-                                        int rate) = 0;
   virtual void RegisterSendPayloadFrequency(int payload_type,
                                             int payload_frequency) = 0;
 
@@ -257,27 +252,6 @@
   virtual RTPSender* RtpSender() = 0;
   virtual const RTPSender* RtpSender() const = 0;
 
-  // Used by the codec module to deliver a video or audio frame for
-  // packetization.
-  // |frame_type|    - type of frame to send
-  // |payload_type|  - payload type of frame to send
-  // |timestamp|     - timestamp of frame to send
-  // |payload_data|  - payload buffer of frame to send
-  // |payload_size|  - size of payload buffer to send
-  // |fragmentation| - fragmentation offset data for fragmented frames such
-  //                   as layers or RED
-  // |transport_frame_id_out| - set to RTP timestamp.
-  // Returns true on success.
-  virtual bool SendOutgoingData(FrameType frame_type,
-                                int8_t payload_type,
-                                uint32_t timestamp,
-                                int64_t capture_time_ms,
-                                const uint8_t* payload_data,
-                                size_t payload_size,
-                                const RTPFragmentationHeader* fragmentation,
-                                const RTPVideoHeader* rtp_video_header,
-                                uint32_t* transport_frame_id_out) = 0;
-
   // Record that a frame is about to be sent. Returns true on success, and false
   // if the module isn't ready to send.
   virtual bool OnSendingRtpFrame(uint32_t timestamp,
@@ -432,23 +406,6 @@
       const VideoBitrateAllocation& bitrate) = 0;
 
   // **************************************************************************
-  // Audio
-  // **************************************************************************
-
-  // Sends a TelephoneEvent tone using RFC 2833 (4733).
-  // Returns -1 on failure else 0.
-  virtual int32_t SendTelephoneEventOutband(uint8_t key,
-                                            uint16_t time_ms,
-                                            uint8_t level) = 0;
-
-  // Store the audio level in dBov for header-extension-for-audio-level-
-  // indication.
-  // This API shall be called before transmision of an RTP packet to ensure
-  // that the |level| part of the extended RTP header is updated.
-  // return -1 on failure else 0.
-  virtual int32_t SetAudioLevel(uint8_t level_dbov) = 0;
-
-  // **************************************************************************
   // Video
   // **************************************************************************
 
diff --git a/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h b/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h
index 7d4f0f1..668d527 100644
--- a/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h
+++ b/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h
@@ -37,12 +37,6 @@
   MOCK_METHOD1(SetRemoteSSRC, void(uint32_t ssrc));
   MOCK_METHOD1(SetMaxRtpPacketSize, void(size_t size));
   MOCK_CONST_METHOD0(MaxRtpPacketSize, size_t());
-  MOCK_METHOD5(RegisterAudioSendPayload,
-               void(int payload_type,
-                    absl::string_view payload_name,
-                    int frequency,
-                    int channels,
-                    int rate));
   MOCK_METHOD2(RegisterSendPayloadFrequency,
                void(int payload_type, int frequency));
   MOCK_METHOD1(DeRegisterSendPayload, int32_t(int8_t payload_type));
@@ -87,16 +81,6 @@
                           uint32_t* nack_rate));
   MOCK_CONST_METHOD1(EstimatedReceiveBandwidth,
                      int(uint32_t* available_bandwidth));
-  MOCK_METHOD9(SendOutgoingData,
-               bool(FrameType frame_type,
-                    int8_t payload_type,
-                    uint32_t timestamp,
-                    int64_t capture_time_ms,
-                    const uint8_t* payload_data,
-                    size_t payload_size,
-                    const RTPFragmentationHeader* fragmentation,
-                    const RTPVideoHeader* rtp_video_header,
-                    uint32_t* frame_id_out));
   MOCK_METHOD4(OnSendingRtpFrame, bool(uint32_t, int64_t, int, bool));
   MOCK_METHOD5(TimeToSendPacket,
                bool(uint32_t ssrc,
@@ -162,9 +146,6 @@
   MOCK_METHOD1(RegisterRtcpStatisticsCallback, void(RtcpStatisticsCallback*));
   MOCK_METHOD0(GetRtcpStatisticsCallback, RtcpStatisticsCallback*());
   MOCK_METHOD1(SendFeedbackPacket, bool(const rtcp::TransportFeedback& packet));
-  MOCK_METHOD3(SendTelephoneEventOutband,
-               int32_t(uint8_t key, uint16_t time_ms, uint8_t level));
-  MOCK_METHOD1(SetAudioLevel, int32_t(uint8_t level_dbov));
   MOCK_METHOD1(SetTargetSendBitrate, void(uint32_t bitrate_bps));
   MOCK_METHOD1(SetKeyFrameRequestMethod, int32_t(KeyFrameRequestMethod method));
   MOCK_METHOD0(RequestKeyFrame, int32_t());
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl.cc b/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
index 4e06002..6d1c9e2 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
@@ -116,9 +116,7 @@
         configuration.extmap_allow_mixed,
         configuration.field_trials ? *configuration.field_trials
                                    : default_trials));
-    if (configuration.audio) {
-      audio_ = absl::make_unique<RTPSenderAudio>(clock_, rtp_sender_.get());
-    }
+
     // Make sure rtcp sender use same timestamp offset as rtp sender.
     rtcp_sender_.SetTimestampOffset(rtp_sender_->TimestampOffset());
 
@@ -270,17 +268,6 @@
   rtcp_receiver_.IncomingPacket(rtcp_packet, length);
 }
 
-void ModuleRtpRtcpImpl::RegisterAudioSendPayload(int payload_type,
-                                                 absl::string_view payload_name,
-                                                 int frequency,
-                                                 int channels,
-                                                 int rate) {
-  RTC_DCHECK(audio_);
-  rtcp_sender_.SetRtpClockRate(payload_type, frequency);
-  RTC_CHECK_EQ(0, audio_->RegisterAudioPayload(payload_name, payload_type,
-                                               frequency, channels, rate));
-}
-
 void ModuleRtpRtcpImpl::RegisterSendPayloadFrequency(int payload_type,
                                                      int payload_frequency) {
   rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency);
@@ -425,30 +412,6 @@
   rtp_sender_->SetAsPartOfAllocation(part_of_allocation);
 }
 
-bool ModuleRtpRtcpImpl::SendOutgoingData(
-    FrameType frame_type,
-    int8_t payload_type,
-    uint32_t time_stamp,
-    int64_t capture_time_ms,
-    const uint8_t* payload_data,
-    size_t payload_size,
-    const RTPFragmentationHeader* fragmentation,
-    const RTPVideoHeader* rtp_video_header,
-    uint32_t* transport_frame_id_out) {
-  OnSendingRtpFrame(time_stamp, capture_time_ms, payload_type,
-                    kVideoFrameKey == frame_type);
-
-  const uint32_t rtp_timestamp = time_stamp + rtp_sender_->TimestampOffset();
-  if (transport_frame_id_out)
-    *transport_frame_id_out = rtp_timestamp;
-
-  RTC_DCHECK(audio_);
-  RTC_DCHECK(fragmentation == nullptr);
-
-  return audio_->SendAudio(frame_type, payload_type, rtp_timestamp,
-                           payload_data, payload_size);
-}
-
 bool ModuleRtpRtcpImpl::OnSendingRtpFrame(uint32_t timestamp,
                                           int64_t capture_time_ms,
                                           int payload_type,
@@ -787,17 +750,6 @@
   return rtcp_sender_.SendFeedbackPacket(packet);
 }
 
-// Send a TelephoneEvent tone using RFC 2833 (4733).
-int32_t ModuleRtpRtcpImpl::SendTelephoneEventOutband(const uint8_t key,
-                                                     const uint16_t time_ms,
-                                                     const uint8_t level) {
-  return audio_ ? audio_->SendTelephoneEvent(key, time_ms, level) : -1;
-}
-
-int32_t ModuleRtpRtcpImpl::SetAudioLevel(const uint8_t level_d_bov) {
-  return audio_ ? audio_->SetAudioLevel(level_d_bov) : -1;
-}
-
 int32_t ModuleRtpRtcpImpl::SetKeyFrameRequestMethod(
     const KeyFrameRequestMethod method) {
   key_frame_req_method_ = method;
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl.h b/modules/rtp_rtcp/source/rtp_rtcp_impl.h
index 20eb179..4b1c927 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl.h
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl.h
@@ -32,8 +32,6 @@
 #include "modules/rtp_rtcp/source/rtcp_receiver.h"
 #include "modules/rtp_rtcp/source/rtcp_sender.h"
 #include "modules/rtp_rtcp/source/rtp_sender.h"
-#include "modules/rtp_rtcp/source/rtp_sender_audio.h"
-#include "modules/rtp_rtcp/source/rtp_sender_video.h"
 #include "rtc_base/critical_section.h"
 #include "rtc_base/gtest_prod_util.h"
 
@@ -64,12 +62,6 @@
   void SetRemoteSSRC(uint32_t ssrc) override;
 
   // Sender part.
-
-  void RegisterAudioSendPayload(int payload_type,
-                                absl::string_view payload_name,
-                                int frequency,
-                                int channels,
-                                int rate) override;
   void RegisterSendPayloadFrequency(int payload_type,
                                     int payload_frequency) override;
 
@@ -137,18 +129,6 @@
 
   void SetAsPartOfAllocation(bool part_of_allocation) override;
 
-  // Used by the codec module to deliver a video or audio frame for
-  // packetization.
-  bool SendOutgoingData(FrameType frame_type,
-                        int8_t payload_type,
-                        uint32_t time_stamp,
-                        int64_t capture_time_ms,
-                        const uint8_t* payload_data,
-                        size_t payload_size,
-                        const RTPFragmentationHeader* fragmentation,
-                        const RTPVideoHeader* rtp_video_header,
-                        uint32_t* transport_frame_id_out) override;
-
   bool OnSendingRtpFrame(uint32_t timestamp,
                          int64_t capture_time_ms,
                          int payload_type,
@@ -270,17 +250,6 @@
 
   bool RtcpXrRrtrStatus() const override;
 
-  // Audio part.
-
-  // Send a TelephoneEvent tone using RFC 2833 (4733).
-  int32_t SendTelephoneEventOutband(uint8_t key,
-                                    uint16_t time_ms,
-                                    uint8_t level) override;
-
-  // Store the audio level in d_bov for header-extension-for-audio-level-
-  // indication.
-  int32_t SetAudioLevel(uint8_t level_d_bov) override;
-
   // Video part.
 
   // Set method for requesting a new key frame.
@@ -346,7 +315,6 @@
   bool TimeToSendFullNackList(int64_t now) const;
 
   std::unique_ptr<RTPSender> rtp_sender_;
-  std::unique_ptr<RTPSenderAudio> audio_;
   RTCPSender rtcp_sender_;
   RTCPReceiver rtcp_receiver_;
 
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc b/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc
index da541a5..98067b2 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc
@@ -22,6 +22,7 @@
 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
+#include "modules/rtp_rtcp/source/rtp_sender_video.h"
 #include "rtc_base/rate_limiter.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
diff --git a/modules/rtp_rtcp/source/rtp_sender_audio.cc b/modules/rtp_rtcp/source/rtp_sender_audio.cc
index 56d0884..c049530 100644
--- a/modules/rtp_rtcp/source/rtp_sender_audio.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_audio.cc
@@ -47,7 +47,9 @@
 }  // namespace
 
 RTPSenderAudio::RTPSenderAudio(Clock* clock, RTPSender* rtp_sender)
-    : clock_(clock), rtp_sender_(rtp_sender) {}
+    : clock_(clock), rtp_sender_(rtp_sender) {
+  RTC_DCHECK(clock_);
+}
 
 RTPSenderAudio::~RTPSenderAudio() {}