Tweak ChannelSend interface, to make it closer to ChannelSendProxy

This is a preparation for deleting ChannelSendProxy. Signature is
changed on a couple of methods. Unused methods
EnableAudioNetworkAdaptor, DisableAudioNetworkAdaptor,
SetReceiverFrameLengthRange and RtpRtcpModulePtr are deleted. Some
methods are demoted to private: SendData, SendRtp, SendRtcp,
PreferredSampleRate, Sending, and OnOverheadChanged.

Bug: webrtc:9801
Change-Id: I982e72418a32e66fb5de410350b1bfebd9a3219c
Reviewed-on: https://webrtc-review.googlesource.com/c/110605
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25666}
diff --git a/audio/channel_send.cc b/audio/channel_send.cc
index a4a4977..09482a4 100644
--- a/audio/channel_send.cc
+++ b/audio/channel_send.cc
@@ -33,6 +33,7 @@
 #include "rtc_base/format_macros.h"
 #include "rtc_base/location.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/numerics/safe_conversions.h"
 #include "rtc_base/rate_limiter.h"
 #include "rtc_base/task_queue.h"
 #include "rtc_base/thread_checker.h"
@@ -557,10 +558,8 @@
   // End of modules shutdown
 }
 
-int32_t ChannelSend::StartSend() {
-  if (channel_state_.Get().sending) {
-    return 0;
-  }
+void ChannelSend::StartSend() {
+  RTC_DCHECK(!channel_state_.Get().sending);
   channel_state_.SetSending(true);
 
   // Resume the previous sequence number which was reset by StopSend(). This
@@ -569,19 +568,13 @@
     _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
   }
   _rtpRtcpModule->SetSendingMediaStatus(true);
-  if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
-    RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to start sending";
-    _rtpRtcpModule->SetSendingMediaStatus(false);
-    rtc::CritScope cs(&_callbackCritSect);
-    channel_state_.SetSending(false);
-    return -1;
-  }
+  int ret = _rtpRtcpModule->SetSendingStatus(true);
+  RTC_DCHECK_EQ(0, ret);
   {
     // It is now OK to start posting tasks to the encoder task queue.
     rtc::CritScope cs(&encoder_queue_lock_);
     encoder_queue_is_active_ = true;
   }
-  return 0;
 }
 
 void ChannelSend::StopSend() {
@@ -716,47 +709,20 @@
   });
 }
 
-bool ChannelSend::EnableAudioNetworkAdaptor(const std::string& config_string) {
-  bool success = false;
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      success =
-          (*encoder)->EnableAudioNetworkAdaptor(config_string, event_log_);
-    }
-  });
-  return success;
-}
-
-void ChannelSend::DisableAudioNetworkAdaptor() {
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder)
-      (*encoder)->DisableAudioNetworkAdaptor();
-  });
-}
-
-void ChannelSend::SetReceiverFrameLengthRange(int min_frame_length_ms,
-                                              int max_frame_length_ms) {
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
-                                              max_frame_length_ms);
-    }
-  });
-}
-
 void ChannelSend::RegisterTransport(Transport* transport) {
   rtc::CritScope cs(&_callbackCritSect);
   _transportPtr = transport;
 }
 
-int32_t ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
+// TODO(nisse): Delete always-true return value.
+bool ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
   // Deliver RTCP packet to RTP/RTCP module for parsing
   _rtpRtcpModule->IncomingRtcpPacket(data, length);
 
   int64_t rtt = GetRTT();
   if (rtt == 0) {
     // Waiting for valid RTT.
-    return 0;
+    return true;
   }
 
   int64_t nack_window_ms = rtt;
@@ -772,8 +738,7 @@
     if (*encoder)
       (*encoder)->OnReceivedRtt(rtt);
   });
-
-  return 0;
+  return true;
 }
 
 void ChannelSend::SetInputMute(bool enable) {
@@ -786,24 +751,24 @@
   return input_mute_;
 }
 
-int ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) {
+bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) {
   RTC_DCHECK_LE(0, event);
   RTC_DCHECK_GE(255, event);
   RTC_DCHECK_LE(0, duration_ms);
   RTC_DCHECK_GE(65535, duration_ms);
   if (!Sending()) {
-    return -1;
+    return false;
   }
   if (_rtpRtcpModule->SendTelephoneEventOutband(
           event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
     RTC_DLOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event";
-    return -1;
+    return false;
   }
-  return 0;
+  return true;
 }
 
-int ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
-                                                  int payload_frequency) {
+bool ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
+                                                   int payload_frequency) {
   RTC_DCHECK_LE(0, payload_type);
   RTC_DCHECK_GE(127, payload_type);
   CodecInst codec = {0};
@@ -816,23 +781,20 @@
       RTC_DLOG(LS_ERROR)
           << "SetSendTelephoneEventPayloadType() failed to register "
              "send payload type";
-      return -1;
+      return false;
     }
   }
-  return 0;
+  return true;
 }
 
-int ChannelSend::SetLocalSSRC(unsigned int ssrc) {
-  if (channel_state_.Get().sending) {
-    RTC_DLOG(LS_ERROR) << "SetLocalSSRC() already sending";
-    return -1;
-  }
+void ChannelSend::SetLocalSSRC(unsigned int ssrc) {
+  RTC_DCHECK(!channel_state_.Get().sending);
+
   if (media_transport_) {
     rtc::CritScope cs(&media_transport_lock_);
     media_transport_channel_id_ = ssrc;
   }
   _rtpRtcpModule->SetSSRC(ssrc);
-  return 0;
 }
 
 void ChannelSend::SetMid(const std::string& mid, int extension_id) {
@@ -845,10 +807,10 @@
   _rtpRtcpModule->SetExtmapAllowMixed(extmap_allow_mixed);
 }
 
-int ChannelSend::SetSendAudioLevelIndicationStatus(bool enable,
-                                                   unsigned char id) {
+void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) {
   _includeAudioLevelIndication = enable;
-  return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
+  int ret = SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
+  RTC_DCHECK_EQ(0, ret);
 }
 
 void ChannelSend::EnableSendTransportSequenceNumber(int id) {
@@ -895,31 +857,23 @@
   _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
 }
 
-int ChannelSend::SetRTCP_CNAME(const char cName[256]) {
-  if (_rtpRtcpModule->SetCNAME(cName) != 0) {
-    RTC_DLOG(LS_ERROR) << "SetRTCP_CNAME() failed to set RTCP CNAME";
-    return -1;
-  }
-  return 0;
+void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) {
+  // Note: SetCNAME() accepts a c string of length at most 255.
+  const std::string c_name_limited(c_name.substr(0, 255));
+  int ret = _rtpRtcpModule->SetCNAME(c_name_limited.c_str()) != 0;
+  RTC_DCHECK_EQ(0, ret) << "SetRTCP_CNAME() failed to set RTCP CNAME";
 }
 
-int ChannelSend::GetRemoteRTCPReportBlocks(
-    std::vector<ReportBlock>* report_blocks) {
-  if (report_blocks == NULL) {
-    RTC_DLOG(LS_ERROR) << "GetRemoteRTCPReportBlock()s invalid report_blocks.";
-    return -1;
-  }
-
+std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const {
   // Get the report blocks from the latest received RTCP Sender or Receiver
   // Report. Each element in the vector contains the sender's SSRC and a
   // report block according to RFC 3550.
   std::vector<RTCPReportBlock> rtcp_report_blocks;
-  if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
-    return -1;
-  }
 
-  if (rtcp_report_blocks.empty())
-    return 0;
+  int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks);
+  RTC_DCHECK_EQ(0, ret);
+
+  std::vector<ReportBlock> report_blocks;
 
   std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
   for (; it != rtcp_report_blocks.end(); ++it) {
@@ -933,19 +887,15 @@
     report_block.interarrival_jitter = it->jitter;
     report_block.last_SR_timestamp = it->last_sender_report_timestamp;
     report_block.delay_since_last_SR = it->delay_since_last_sender_report;
-    report_blocks->push_back(report_block);
+    report_blocks.push_back(report_block);
   }
-  return 0;
+  return report_blocks;
 }
 
-int ChannelSend::GetRTPStatistics(CallSendStatistics& stats) {
-  // --- RtcpStatistics
-
-  // --- RTT
+CallSendStatistics ChannelSend::GetRTCPStatistics() const {
+  CallSendStatistics stats = {0};
   stats.rttMs = GetRTT();
 
-  // --- Data counters
-
   size_t bytesSent(0);
   uint32_t packetsSent(0);
 
@@ -958,7 +908,7 @@
   stats.bytesSent = bytesSent;
   stats.packetsSent = packetsSent;
 
-  return 0;
+  return stats;
 }
 
 void ChannelSend::SetNACKStatus(bool enable, int maxNumberOfPackets) {
@@ -1063,11 +1013,14 @@
 
 int ChannelSend::SetSendRtpHeaderExtension(bool enable,
                                            RTPExtensionType type,
-                                           unsigned char id) {
+                                           int id) {
   int error = 0;
   _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
   if (enable) {
-    error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
+    // TODO(nisse): RtpRtcp::RegisterSendRtpHeaderExtension to take an int
+    // argument. Currently it wants an uint8_t.
+    error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(
+        type, rtc::dchecked_cast<uint8_t>(id));
   }
   return error;
 }