Unify SetReceive and SetSend in media channels

These are conceptually equal and do not need to be separately declared
in each of the Audio/Video interfaces. That in turn allows us to move
UpdateMediaSendRecvState_w to BaseChannel and remove the two separate,
yet identical implementations in the audio/video channel classes.

Bug: none
Change-Id: I560d0fe38a3be0cc16d3bcc43adf8024c9f85f9b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/465920
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47511}
diff --git a/media/base/fake_media_engine.cc b/media/base/fake_media_engine.cc
index 49afc53..074c029 100644
--- a/media/base/fake_media_engine.cc
+++ b/media/base/fake_media_engine.cc
@@ -109,8 +109,8 @@
   return (SetRecvCodecs(params.codecs) &&
           SetRecvRtpHeaderExtensions(params.extensions));
 }
-void FakeVoiceMediaReceiveChannel::SetPlayout(bool playout) {
-  set_playout(playout);
+void FakeVoiceMediaReceiveChannel::SetReceive(bool receive) {
+  set_playout(receive);
 }
 bool FakeVoiceMediaReceiveChannel::HasSource(uint32_t ssrc) const {
   return local_sinks_.find(ssrc) != local_sinks_.end();
@@ -282,8 +282,8 @@
           SetMaxSendBandwidth(params.max_bandwidth_bps) &&
           SetOptions(params.options));
 }
-void FakeVoiceMediaSendChannel::SetSend(bool send) {
-  set_sending(send);
+bool FakeVoiceMediaSendChannel::SetSend(bool send) {
+  return set_sending(send);
 }
 bool FakeVoiceMediaSendChannel::SetAudioSend(uint32_t ssrc,
                                              bool enable,
diff --git a/media/base/fake_media_engine.h b/media/base/fake_media_engine.h
index 9e02448..832caac 100644
--- a/media/base/fake_media_engine.h
+++ b/media/base/fake_media_engine.h
@@ -512,7 +512,7 @@
   MediaType media_type() const override { return MediaType::AUDIO; }
 
   bool SetReceiverParameters(const AudioReceiverParameters& params) override;
-  void SetPlayout(bool playout) override;
+  void SetReceive(bool receive) override;
 
   bool AddRecvStream(const StreamParams& sp) override;
   bool RemoveRecvStream(uint32_t ssrc) override;
@@ -603,7 +603,7 @@
   MediaType media_type() const override { return MediaType::AUDIO; }
 
   bool SetSenderParameters(const AudioSenderParameter& params) override;
-  void SetSend(bool send) override;
+  bool SetSend(bool send) override;
   bool SetAudioSend(uint32_t ssrc,
                     bool enable,
                     const AudioOptions* options,
diff --git a/media/base/media_channel.h b/media/base/media_channel.h
index 10ab6eb..3a77b05 100644
--- a/media/base/media_channel.h
+++ b/media/base/media_channel.h
@@ -216,6 +216,9 @@
   virtual void SetExtmapAllowMixed(bool extmap_allow_mixed) = 0;
   virtual bool ExtmapAllowMixed() const = 0;
 
+  // Starts or stops transmission (and potentially capture) of local media.
+  virtual bool SetSend(bool send) = 0;
+
   // Set the frame encryptor to use on all outgoing frames. This is optional.
   // This pointers lifetime is managed by the set of RtpSender it is attached
   // to.
@@ -275,6 +278,8 @@
   // ssrc must be the first SSRC of the media stream if the stream uses
   // multiple SSRCs.
   virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
+  // Starts or stops playout or decoding of received media.
+  virtual void SetReceive(bool receive) = 0;
   // Resets any cached StreamParams for an unsignaled RecvStream, and removes
   // any existing unsignaled streams.
   virtual void ResetUnsignaledRecvStream() = 0;
@@ -911,8 +916,7 @@
 class VoiceMediaSendChannelInterface : public MediaSendChannelInterface {
  public:
   virtual bool SetSenderParameters(const AudioSenderParameter& params) = 0;
-  // Starts or stops sending (and potentially capture) of local audio.
-  virtual void SetSend(bool send) = 0;
+
   // Configure stream for sending.
   virtual bool SetAudioSend(uint32_t ssrc,
                             bool enable,
@@ -947,8 +951,7 @@
   // Retrieve the receive parameters for the default receive
   // stream, which is used when SSRCs are not signaled.
   virtual RtpParameters GetDefaultRtpReceiveParameters() const = 0;
-  // Starts or stops playout of received audio.
-  virtual void SetPlayout(bool playout) = 0;
+
   // Set speaker output volume of the specified ssrc.
   virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
   // Set speaker output volume for future unsignaled streams.
@@ -996,8 +999,7 @@
       absl::AnyInvocable<void(EncoderSwitchRequestAction)>;
 
   virtual bool SetSenderParameters(const VideoSenderParameters& params) = 0;
-  // Starts or stops transmission (and potentially capture) of local video.
-  virtual bool SetSend(bool send) = 0;
+
   // Configure stream for sending and register a source.
   // The `ssrc` must correspond to a registered send stream.
   virtual bool SetVideoSend(uint32_t ssrc,
@@ -1031,8 +1033,7 @@
   virtual bool SetReceiverParameters(const VideoReceiverParameters& params) = 0;
   // Get the receive parameters for the incoming stream identified by `ssrc`.
   virtual RtpParameters GetRtpReceiverParameters(uint32_t ssrc) const = 0;
-  // Starts or stops decoding of remote video.
-  virtual void SetReceive(bool receive) = 0;
+
   // Retrieve the receive parameters for the default receive
   // stream, which is used when SSRCs are not signaled.
   virtual RtpParameters GetDefaultRtpReceiveParameters() const = 0;
diff --git a/media/engine/webrtc_voice_engine.cc b/media/engine/webrtc_voice_engine.cc
index 7b1483d..e7151c2 100644
--- a/media/engine/webrtc_voice_engine.cc
+++ b/media/engine/webrtc_voice_engine.cc
@@ -1562,11 +1562,11 @@
   return true;
 }
 
-void WebRtcVoiceSendChannel::SetSend(bool send) {
+bool WebRtcVoiceSendChannel::SetSend(bool send) {
   RTC_DCHECK_RUN_ON(worker_thread_);
   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
   if (send_ == send) {
-    return;
+    return true;
   }
 
   // Apply channel specific options.
@@ -1591,6 +1591,7 @@
   }
 
   send_ = send;
+  return true;
 }
 
 bool WebRtcVoiceSendChannel::SetAudioSend(uint32_t ssrc,
@@ -2356,7 +2357,7 @@
   bool playout_enabled = playout_;
   // Receive codecs can not be changed while playing. So we temporarily
   // pause playout.
-  SetPlayout(false);
+  SetReceive(false);
   RTC_DCHECK(!playout_);
 
   decoder_map_ = std::move(decoder_map);
@@ -2366,7 +2367,7 @@
 
   recv_codecs_ = codecs;
 
-  SetPlayout(playout_enabled);
+  SetReceive(playout_enabled);
   RTC_DCHECK_EQ(playout_, playout_enabled);
 
   return true;
@@ -2411,17 +2412,17 @@
   }
 }
 
-void WebRtcVoiceReceiveChannel::SetPlayout(bool playout) {
-  TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetPlayout");
+void WebRtcVoiceReceiveChannel::SetReceive(bool receive) {
+  TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetReceive");
   RTC_DCHECK_RUN_ON(worker_thread_);
-  if (playout_ == playout) {
+  if (playout_ == receive) {
     return;
   }
 
   for (const auto& kv : recv_streams_) {
-    kv.second->SetPlayout(playout);
+    kv.second->SetPlayout(receive);
   }
-  playout_ = playout;
+  playout_ = receive;
 }
 
 bool WebRtcVoiceReceiveChannel::AddRecvStream(const StreamParams& sp) {
diff --git a/media/engine/webrtc_voice_engine.h b/media/engine/webrtc_voice_engine.h
index cb5ee76..6e60fc2 100644
--- a/media/engine/webrtc_voice_engine.h
+++ b/media/engine/webrtc_voice_engine.h
@@ -223,7 +223,7 @@
   RTCError SetRtpSendParameters(uint32_t ssrc,
                                 const RtpParameters& parameters,
                                 SetParametersCallback callback) override;
-  void SetSend(bool send) override;
+  bool SetSend(bool send) override;
   bool SetAudioSend(uint32_t ssrc,
                     bool enable,
                     const AudioOptions* options,
@@ -354,7 +354,7 @@
   RtpParameters GetRtpReceiverParameters(uint32_t ssrc) const override;
   RtpParameters GetDefaultRtpReceiveParameters() const override;
 
-  void SetPlayout(bool playout) override;
+  void SetReceive(bool receive) override;
   bool AddRecvStream(const StreamParams& sp) override;
   bool RemoveRecvStream(uint32_t ssrc) override;
   void ResetUnsignaledRecvStream() override;
diff --git a/media/engine/webrtc_voice_engine_unittest.cc b/media/engine/webrtc_voice_engine_unittest.cc
index 526a6cb..17b835d 100644
--- a/media/engine/webrtc_voice_engine_unittest.cc
+++ b/media/engine/webrtc_voice_engine_unittest.cc
@@ -1079,7 +1079,7 @@
   parameters.codecs.push_back(kPcmuCodec);
   parameters.codecs.push_back(kCn16000Codec);
   EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
-  receive_channel_->SetPlayout(true);
+  receive_channel_->SetReceive(true);
   EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
 
   // Remapping a payload type to a different codec should fail.
@@ -1096,7 +1096,7 @@
   parameters.codecs.push_back(kPcmuCodec);
   parameters.codecs.push_back(kCn16000Codec);
   EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
-  receive_channel_->SetPlayout(true);
+  receive_channel_->SetReceive(true);
 
   parameters.codecs.push_back(kOpusCodec);
   EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
@@ -2414,9 +2414,9 @@
 TEST_P(WebRtcVoiceEngineTestFake, Playout) {
   EXPECT_TRUE(SetupRecvStream());
   EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
-  receive_channel_->SetPlayout(true);
+  receive_channel_->SetReceive(true);
   EXPECT_TRUE(GetRecvStream(kSsrcX).started());
-  receive_channel_->SetPlayout(false);
+  receive_channel_->SetReceive(false);
   EXPECT_FALSE(GetRecvStream(kSsrcX).started());
 }
 
@@ -2587,7 +2587,7 @@
 
   // Start playout without a receive stream.
   SetSenderParameters(send_parameters_);
-  receive_channel_->SetPlayout(true);
+  receive_channel_->SetReceive(true);
 
   // Adding another stream should enable playout on the new stream only.
   EXPECT_TRUE(AddRecvStream(kSsrcY));
@@ -2607,12 +2607,12 @@
   EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
 
   // Stop playout.
-  receive_channel_->SetPlayout(false);
+  receive_channel_->SetReceive(false);
   EXPECT_FALSE(GetRecvStream(kSsrcY).started());
   EXPECT_FALSE(GetRecvStream(kSsrcZ).started());
 
   // Restart playout and make sure recv streams are played out.
-  receive_channel_->SetPlayout(true);
+  receive_channel_->SetReceive(true);
   EXPECT_TRUE(GetRecvStream(kSsrcY).started());
   EXPECT_TRUE(GetRecvStream(kSsrcZ).started());
 
@@ -3743,7 +3743,7 @@
 // Test that playout is still started after changing parameters
 TEST_P(WebRtcVoiceEngineTestFake, PreservePlayoutWhenRecreateRecvStream) {
   SetupRecvStream();
-  receive_channel_->SetPlayout(true);
+  receive_channel_->SetReceive(true);
   EXPECT_TRUE(GetRecvStream(kSsrcX).started());
 
   // Changing RTP header extensions will recreate the
diff --git a/pc/channel.cc b/pc/channel.cc
index daf42c4..e998e37 100644
--- a/pc/channel.cc
+++ b/pc/channel.cc
@@ -621,6 +621,19 @@
   UpdateMediaSendRecvState_w();
 }
 
+void BaseChannel::UpdateMediaSendRecvState_w() {
+  RTC_DCHECK_DISALLOW_THREAD_BLOCKING_CALLS();
+  bool receive =
+      enabled() && RtpTransceiverDirectionHasRecv(local_content_direction());
+  media_receive_channel()->SetReceive(receive);
+
+  bool send = IsReadyToSendMedia_w();
+  media_send_channel()->SetSend(send);
+
+  RTC_LOG(LS_INFO) << "Changing state, recv=" << receive << " send=" << send
+                   << " for " << ToString();
+}
+
 void BaseChannel::UpdateWritableState_n() {
   TRACE_EVENT0("webrtc", "BaseChannel::UpdateWritableState_n");
   if (rtp_transport_->IsWritable(/*rtcp=*/true) &&
@@ -859,22 +872,7 @@
   DisableMedia_w();
 }
 
-void VoiceChannel::UpdateMediaSendRecvState_w() {
-  RTC_DCHECK_DISALLOW_THREAD_BLOCKING_CALLS();
-  // Render incoming data if we're the active call, and we have the local
-  // content. We receive data on the default channel and multiplexed streams.
-  bool receive =
-      enabled() && RtpTransceiverDirectionHasRecv(local_content_direction());
-  media_receive_channel()->SetPlayout(receive);
 
-  // Send outgoing data if we're the active call, we have the remote content,
-  // and we have had some form of connectivity.
-  bool send = IsReadyToSendMedia_w();
-  media_send_channel()->SetSend(send);
-
-  RTC_LOG(LS_INFO) << "Changing voice state, recv=" << receive
-                   << " send=" << send << " for " << ToString();
-}
 
 RTCError VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
                                          SdpType type) {
@@ -1033,19 +1031,7 @@
   DisableMedia_w();
 }
 
-void VideoChannel::UpdateMediaSendRecvState_w() {
-  RTC_DCHECK_DISALLOW_THREAD_BLOCKING_CALLS();
-  // Send outgoing data if we're the active call, we have the remote content,
-  // and we have had some form of connectivity.
-  bool receive =
-      enabled() && RtpTransceiverDirectionHasRecv(local_content_direction());
-  media_receive_channel()->SetReceive(receive);
 
-  bool send = IsReadyToSendMedia_w();
-  media_send_channel()->SetSend(send);
-  RTC_LOG(LS_INFO) << "Changing video state, recv=" << receive
-                   << " send=" << send << " for " << ToString();
-}
 
 RTCError VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
                                          SdpType type) {
diff --git a/pc/channel.h b/pc/channel.h
index dfd7474..00cd943 100644
--- a/pc/channel.h
+++ b/pc/channel.h
@@ -233,7 +233,7 @@
   // Should be called whenever the conditions for
   // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
   // Updates the send/recv state of the media channel.
-  virtual void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread()) = 0;
+  void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread());
 
   RTCError UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
                                 SdpType type) RTC_RUN_ON(worker_thread());
@@ -389,7 +389,7 @@
 
  private:
   // overrides from BaseChannel
-  void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread()) override;
+
   RTCError SetLocalContent_w(const MediaContentDescription* content,
                              SdpType type) RTC_RUN_ON(worker_thread()) override;
   RTCError SetRemoteContent_w(const MediaContentDescription* content,
@@ -454,7 +454,7 @@
 
  private:
   // overrides from BaseChannel
-  void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread()) override;
+
   RTCError SetLocalContent_w(const MediaContentDescription* content,
                              SdpType type) RTC_RUN_ON(worker_thread()) override;
   RTCError SetRemoteContent_w(const MediaContentDescription* content,
diff --git a/pc/test/mock_voice_media_receive_channel_interface.h b/pc/test/mock_voice_media_receive_channel_interface.h
index 83adde2..8156ddf 100644
--- a/pc/test/mock_voice_media_receive_channel_interface.h
+++ b/pc/test/mock_voice_media_receive_channel_interface.h
@@ -57,7 +57,7 @@
               GetDefaultRtpReceiveParameters,
               (),
               (const, override));
-  MOCK_METHOD(void, SetPlayout, (bool playout), (override));
+  MOCK_METHOD(void, SetReceive, (bool receive), (override));
   MOCK_METHOD(bool,
               SetOutputVolume,
               (uint32_t ssrc, double volume),