Refactor voe::Channel to not use RtpReceiver.

Analogous to https://webrtc-review.googlesource.com/c/src/+/92398, for
RtpVideoStreamReceiver.

Bug: webrtc:7135
Change-Id: I0639f9982da2ed80edbcf900cf14f8ae982ef80c
Reviewed-on: https://webrtc-review.googlesource.com/93820
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24309}
diff --git a/audio/audio_receive_stream.cc b/audio/audio_receive_stream.cc
index 3be50fd..b507afc 100644
--- a/audio/audio_receive_stream.cc
+++ b/audio/audio_receive_stream.cc
@@ -20,7 +20,6 @@
 #include "audio/conversion.h"
 #include "call/rtp_stream_receiver_controller_interface.h"
 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
-#include "modules/rtp_rtcp/include/rtp_receiver.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
@@ -73,9 +72,9 @@
       static_cast<internal::AudioState*>(audio_state);
   return absl::make_unique<voe::ChannelProxy>(absl::make_unique<voe::Channel>(
       module_process_thread, internal_audio_state->audio_device_module(),
-      nullptr /* RtcpRttStats */, event_log, config.jitter_buffer_max_packets,
-      config.jitter_buffer_fast_accelerate, config.decoder_factory,
-      config.codec_pair_id));
+      nullptr /* RtcpRttStats */, event_log, config.rtp.remote_ssrc,
+      config.jitter_buffer_max_packets, config.jitter_buffer_fast_accelerate,
+      config.decoder_factory, config.codec_pair_id));
 }
 }  // namespace
 
@@ -345,9 +344,7 @@
     channel_proxy->SetLocalSSRC(new_config.rtp.local_ssrc);
   }
 
-  if (first_time) {
-    channel_proxy->SetRemoteSSRC(new_config.rtp.remote_ssrc);
-  } else {
+  if (!first_time) {
     // Remote ssrc can't be changed mid-stream.
     RTC_DCHECK_EQ(old_config.rtp.remote_ssrc, new_config.rtp.remote_ssrc);
   }
diff --git a/audio/audio_receive_stream_unittest.cc b/audio/audio_receive_stream_unittest.cc
index 0c3189d..eb93bd0 100644
--- a/audio/audio_receive_stream_unittest.cc
+++ b/audio/audio_receive_stream_unittest.cc
@@ -83,7 +83,6 @@
 
     channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
     EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kLocalSsrc)).Times(1);
-    EXPECT_CALL(*channel_proxy_, SetRemoteSSRC(kRemoteSsrc)).Times(1);
     EXPECT_CALL(*channel_proxy_, SetNACKStatus(true, 15)).Times(1);
     EXPECT_CALL(*channel_proxy_,
                 RegisterReceiverCongestionControlObjects(&packet_router_))
diff --git a/audio/channel.cc b/audio/channel.cc
index c96184d..4f9a0f1 100644
--- a/audio/channel.cc
+++ b/audio/channel.cc
@@ -30,7 +30,6 @@
 #include "modules/pacing/packet_router.h"
 #include "modules/rtp_rtcp/include/receive_statistics.h"
 #include "modules/rtp_rtcp/include/rtp_payload_registry.h"
-#include "modules/rtp_rtcp/include/rtp_receiver.h"
 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
 #include "modules/utility/include/process_thread.h"
@@ -344,8 +343,7 @@
   }
 
   int64_t round_trip_time = 0;
-  _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
-                      NULL);
+  _rtpRtcpModule->RTT(remote_ssrc_, &round_trip_time, NULL, NULL, NULL);
 
   std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
   if (!nack_list.empty()) {
@@ -479,6 +477,7 @@
               rtcp_rtt_stats,
               rtc_event_log,
               0,
+              0,
               false,
               rtc::scoped_refptr<AudioDecoderFactory>(),
               absl::nullopt) {
@@ -490,6 +489,7 @@
                  AudioDeviceModule* audio_device_module,
                  RtcpRttStats* rtcp_rtt_stats,
                  RtcEventLog* rtc_event_log,
+                 uint32_t remote_ssrc,
                  size_t jitter_buffer_max_packets,
                  bool jitter_buffer_fast_playout,
                  rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
@@ -498,10 +498,7 @@
       rtp_payload_registry_(new RTPPayloadRegistry()),
       rtp_receive_statistics_(
           ReceiveStatistics::Create(Clock::GetRealTimeClock())),
-      rtp_receiver_(
-          RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
-                                           this,
-                                           rtp_payload_registry_.get())),
+      remote_ssrc_(remote_ssrc),
       _outputAudioLevel(),
       _timeStamp(0),  // This is just an offset, RTP module will add it's own
                       // random offset
@@ -561,7 +558,7 @@
 
   _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
   _rtpRtcpModule->SetSendingMediaStatus(false);
-
+  _rtpRtcpModule->SetRemoteSSRC(remote_ssrc_);
   Init();
 }
 
@@ -785,6 +782,22 @@
   });
 }
 
+std::vector<webrtc::RtpSource> Channel::GetSources() const {
+  int64_t now_ms = rtc::TimeMillis();
+  std::vector<RtpSource> sources;
+  {
+    rtc::CritScope cs(&rtp_sources_lock_);
+    sources = contributing_sources_.GetSources(now_ms);
+    if (last_received_rtp_system_time_ms_ >=
+        now_ms - ContributingSources::kHistoryMs) {
+      sources.emplace_back(*last_received_rtp_system_time_ms_, remote_ssrc_,
+                           RtpSourceType::SSRC);
+      sources.back().set_audio_level(last_received_rtp_audio_level_);
+    }
+  }
+  return sources;
+}
+
 void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
   if (use_twcc_plr_for_ana_)
     return;
@@ -833,7 +846,24 @@
   _transportPtr = transport;
 }
 
+// TODO(nisse): Move receive logic up to AudioReceiveStream.
 void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
+  int64_t now_ms = rtc::TimeMillis();
+  uint8_t audio_level;
+  bool voice_activity;
+  bool has_audio_level =
+      packet.GetExtension<::webrtc::AudioLevel>(&voice_activity, &audio_level);
+
+  {
+    rtc::CritScope cs(&rtp_sources_lock_);
+    last_received_rtp_timestamp_ = packet.Timestamp();
+    last_received_rtp_system_time_ms_ = now_ms;
+    if (has_audio_level)
+      last_received_rtp_audio_level_ = audio_level;
+    std::vector<uint32_t> csrcs = packet.Csrcs();
+    contributing_sources_.Update(now_ms, csrcs);
+  }
+
   RTPHeader header;
   packet.GetHeader(&header);
 
@@ -861,8 +891,16 @@
   if (!pl) {
     return false;
   }
-  return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
-                                          pl->typeSpecific);
+  WebRtcRTPHeader webrtc_rtp_header = {};
+  webrtc_rtp_header.header = header;
+
+  const size_t payload_data_length = payload_length - header.paddingLength;
+  if (payload_data_length == 0) {
+    webrtc_rtp_header.frameType = kEmptyFrame;
+    return OnReceivedPayloadData(nullptr, 0, &webrtc_rtp_header);
+  }
+  return OnReceivedPayloadData(payload, payload_data_length,
+                               &webrtc_rtp_header);
 }
 
 bool Channel::IsPacketRetransmitted(const RTPHeader& header) const {
@@ -989,19 +1027,15 @@
   return 0;
 }
 
-void Channel::SetRemoteSSRC(uint32_t ssrc) {
-  // Update ssrc so that NTP for AV sync can be updated.
-  _rtpRtcpModule->SetRemoteSSRC(ssrc);
-}
-
 void Channel::SetMid(const std::string& mid, int extension_id) {
   int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id);
   RTC_DCHECK_EQ(0, ret);
   _rtpRtcpModule->SetMid(mid);
 }
 
+// TODO(nisse): Pass ssrc in return value instead.
 int Channel::GetRemoteSSRC(unsigned int& ssrc) {
-  ssrc = rtp_receiver_->SSRC();
+  ssrc = remote_ssrc_;
   return 0;
 }
 
@@ -1119,7 +1153,7 @@
   // each received RTP packet.
   RtcpStatistics statistics;
   StreamStatistician* statistician =
-      rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
+      rtp_receive_statistics_->GetStatistician(remote_ssrc_);
   if (statistician) {
     // Recompute |fraction_lost| only if RTCP is off. If it's on, then
     // |fraction_lost| should only be recomputed when an RTCP SR or RR is sent.
@@ -1317,17 +1351,19 @@
 
 absl::optional<Syncable::Info> Channel::GetSyncInfo() const {
   Syncable::Info info;
-  if (!rtp_receiver_->GetLatestTimestamps(
-          &info.latest_received_capture_timestamp,
-          &info.latest_receive_time_ms)) {
-    return absl::nullopt;
-  }
   if (_rtpRtcpModule->RemoteNTP(&info.capture_time_ntp_secs,
                                 &info.capture_time_ntp_frac, nullptr, nullptr,
                                 &info.capture_time_source_clock) != 0) {
     return absl::nullopt;
   }
-
+  {
+    rtc::CritScope cs(&rtp_sources_lock_);
+    if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
+      return absl::nullopt;
+    }
+    info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
+    info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
+  }
   return info;
 }
 
@@ -1408,25 +1444,23 @@
     return rtt;
   }
 
-  uint32_t remoteSSRC = rtp_receiver_->SSRC();
   std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
   for (; it != report_blocks.end(); ++it) {
-    if (it->sender_ssrc == remoteSSRC)
+    if (it->sender_ssrc == remote_ssrc_)
       break;
   }
-  if (it == report_blocks.end()) {
-    // We have not received packets with SSRC matching the report blocks.
-    // To calculate RTT we try with the SSRC of the first report block.
-    // This is very important for send-only channels where we don't know
-    // the SSRC of the other end.
-    remoteSSRC = report_blocks[0].sender_ssrc;
-  }
+
+  // If we have not received packets with SSRC matching the report blocks, use
+  // the SSRC of the first report block for calculating the RTT. This is very
+  // important for send-only channels where we don't know the SSRC of the other
+  // end.
+  uint32_t ssrc =
+      (it == report_blocks.end()) ? report_blocks[0].sender_ssrc : remote_ssrc_;
 
   int64_t avg_rtt = 0;
   int64_t max_rtt = 0;
   int64_t min_rtt = 0;
-  if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
-      0) {
+  if (_rtpRtcpModule->RTT(ssrc, &rtt, &avg_rtt, &min_rtt, &max_rtt) != 0) {
     return 0;
   }
   return rtt;
diff --git a/audio/channel.h b/audio/channel.h
index 670223c..0676dba 100644
--- a/audio/channel.h
+++ b/audio/channel.h
@@ -21,6 +21,7 @@
 #include "api/audio_codecs/audio_encoder.h"
 #include "api/call/audio_sink.h"
 #include "api/call/transport.h"
+#include "api/rtpreceiverinterface.h"
 #include "audio/audio_level.h"
 #include "call/syncable.h"
 #include "common_types.h"  // NOLINT(build/include)
@@ -28,8 +29,8 @@
 #include "modules/audio_processing/rms_level.h"
 #include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
 #include "modules/rtp_rtcp/include/rtp_header_parser.h"
-#include "modules/rtp_rtcp/include/rtp_receiver.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp.h"
+#include "modules/rtp_rtcp/source/contributing_sources.h"
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/event.h"
 #include "rtc_base/task_queue.h"
@@ -156,6 +157,7 @@
           AudioDeviceModule* audio_device_module,
           RtcpRttStats* rtcp_rtt_stats,
           RtcEventLog* rtc_event_log,
+          uint32_t remote_ssrc,
           size_t jitter_buffer_max_packets,
           bool jitter_buffer_fast_playout,
           rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
@@ -223,7 +225,6 @@
 
   // RTP+RTCP
   int SetLocalSSRC(unsigned int ssrc);
-  void SetRemoteSSRC(uint32_t ssrc);
 
   void SetMid(const std::string& mid, int extension_id);
   int SetSendAudioLevelIndicationStatus(bool enable, unsigned char id);
@@ -299,9 +300,7 @@
 
   void OnRecoverableUplinkPacketLossRate(float recoverable_packet_loss_rate);
 
-  std::vector<RtpSource> GetSources() const {
-    return rtp_receiver_->GetSources();
-  }
+  std::vector<RtpSource> GetSources() const;
 
  private:
   class ProcessAndEncodeAudioTask;
@@ -343,8 +342,20 @@
 
   std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
   std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
-  std::unique_ptr<RtpReceiver> rtp_receiver_;
   std::unique_ptr<RtpRtcp> _rtpRtcpModule;
+  const uint32_t remote_ssrc_;
+
+  // Info for GetSources and GetSyncInfo is updated on network or worker thread,
+  // queried on the worker thread.
+  rtc::CriticalSection rtp_sources_lock_;
+  ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
+  absl::optional<uint32_t> last_received_rtp_timestamp_
+      RTC_GUARDED_BY(&rtp_sources_lock_);
+  absl::optional<int64_t> last_received_rtp_system_time_ms_
+      RTC_GUARDED_BY(&rtp_sources_lock_);
+  absl::optional<uint8_t> last_received_rtp_audio_level_
+      RTC_GUARDED_BY(&rtp_sources_lock_);
+
   std::unique_ptr<AudioCodingModule> audio_coding_;
   AudioSinkInterface* audio_sink_ = nullptr;
   AudioLevel _outputAudioLevel;
diff --git a/audio/channel_proxy.cc b/audio/channel_proxy.cc
index 78cc2fc..5e8181a 100644
--- a/audio/channel_proxy.cc
+++ b/audio/channel_proxy.cc
@@ -53,11 +53,6 @@
   RTC_DCHECK_EQ(0, error);
 }
 
-void ChannelProxy::SetRemoteSSRC(uint32_t ssrc) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetRemoteSSRC(ssrc);
-}
-
 void ChannelProxy::SetMid(const std::string& mid, int extension_id) {
   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   channel_->SetMid(mid, extension_id);
diff --git a/audio/channel_proxy.h b/audio/channel_proxy.h
index 253a196..f82c1fd 100644
--- a/audio/channel_proxy.h
+++ b/audio/channel_proxy.h
@@ -34,7 +34,6 @@
 class RtcpRttStats;
 class RtpPacketSender;
 class RtpPacketReceived;
-class RtpReceiver;
 class RtpRtcp;
 class RtpTransportControllerSendInterface;
 class Transport;
@@ -62,7 +61,6 @@
 
   virtual void SetRTCPStatus(bool enable);
   virtual void SetLocalSSRC(uint32_t ssrc);
-  virtual void SetRemoteSSRC(uint32_t ssrc);
   virtual void SetMid(const std::string& mid, int extension_id);
   virtual void SetRTCP_CNAME(const std::string& c_name);
   virtual void SetNACKStatus(bool enable, int max_packets);
diff --git a/audio/mock_voe_channel_proxy.h b/audio/mock_voe_channel_proxy.h
index ee16195..f6a2637 100644
--- a/audio/mock_voe_channel_proxy.h
+++ b/audio/mock_voe_channel_proxy.h
@@ -37,7 +37,6 @@
       void(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier));
   MOCK_METHOD1(SetRTCPStatus, void(bool enable));
   MOCK_METHOD1(SetLocalSSRC, void(uint32_t ssrc));
-  MOCK_METHOD1(SetRemoteSSRC, void(uint32_t ssrc));
   MOCK_METHOD1(SetRTCP_CNAME, void(const std::string& c_name));
   MOCK_METHOD2(SetNACKStatus, void(bool enable, int max_packets));
   MOCK_METHOD2(SetSendAudioLevelIndicationStatus, void(bool enable, int id));
diff --git a/modules/rtp_rtcp/source/contributing_sources.cc b/modules/rtp_rtcp/source/contributing_sources.cc
index 9b82de5..853706c 100644
--- a/modules/rtp_rtcp/source/contributing_sources.cc
+++ b/modules/rtp_rtcp/source/contributing_sources.cc
@@ -14,15 +14,13 @@
 
 namespace {
 
-// Set by the spec, see
-// https://www.w3.org/TR/webrtc/#dom-rtcrtpreceiver-getcontributingsources
-constexpr int64_t kHistoryMs = 10 * rtc::kNumMillisecsPerSec;
-
 // Allow some stale records to accumulate before cleaning.
 constexpr int64_t kPruningIntervalMs = 15 * rtc::kNumMillisecsPerSec;
 
 }  // namespace
 
+constexpr int64_t ContributingSources::kHistoryMs;
+
 ContributingSources::ContributingSources() = default;
 ContributingSources::~ContributingSources() = default;
 
diff --git a/modules/rtp_rtcp/source/contributing_sources.h b/modules/rtp_rtcp/source/contributing_sources.h
index 64d7f52..1a4a572 100644
--- a/modules/rtp_rtcp/source/contributing_sources.h
+++ b/modules/rtp_rtcp/source/contributing_sources.h
@@ -24,6 +24,10 @@
 
 class ContributingSources {
  public:
+  // Set by the spec, see
+  // https://www.w3.org/TR/webrtc/#dom-rtcrtpreceiver-getcontributingsources
+  static constexpr int64_t kHistoryMs = 10 * rtc::kNumMillisecsPerSec;
+
   ContributingSources();
   ~ContributingSources();