PeerConnection::SetBitrate now also configures media transport.

(so far SetBitrate did not do anything for media transport)

Bug: webrtc:9719
Change-Id: I48e669341ffe6c9e4697ff9146c314be7796a209
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/127980
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Bjorn Mellem <mellem@webrtc.org>
Commit-Queue: Peter Slatala <psla@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27169}
diff --git a/call/call.cc b/call/call.cc
index bf42ee3..8dcd785 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -244,6 +244,8 @@
   // at least once after that.
   void MediaTransportChange(MediaTransportInterface* media_transport) override;
 
+  void SetClientBitratePreferences(const BitrateSettings& preferences) override;
+
  private:
   DeliveryStatus DeliverRtcp(MediaType media_type,
                              const uint8_t* packet,
@@ -275,6 +277,9 @@
   Clock* const clock_;
   TaskQueueFactory* const task_queue_factory_;
 
+  // Caching the last SetBitrate for media transport.
+  absl::optional<MediaTransportTargetRateConstraints> last_set_bitrate_
+      RTC_GUARDED_BY(&target_observer_crit_);
   const int num_cpu_cores_;
   const std::unique_ptr<ProcessThread> module_process_thread_;
   const std::unique_ptr<CallStats> call_stats_;
@@ -580,6 +585,37 @@
       constraints.min_bitrate =
           DataRate::bps(config_.bitrate_config.min_bitrate_bps);
     }
+
+    // User called ::SetBitrate on peer connection before
+    // media transport was created.
+    if (last_set_bitrate_) {
+      media_transport_->SetTargetBitrateLimits(*last_set_bitrate_);
+    } else {
+      media_transport_->SetTargetBitrateLimits(constraints);
+    }
+  }
+}
+
+void Call::SetClientBitratePreferences(const BitrateSettings& preferences) {
+  GetTransportControllerSend()->SetClientBitratePreferences(preferences);
+  // Can the client code invoke 'SetBitrate' before media transport is created?
+  // It's probably possible :/
+  MediaTransportTargetRateConstraints constraints;
+  if (preferences.start_bitrate_bps.has_value()) {
+    constraints.starting_bitrate =
+        webrtc::DataRate::bps(*preferences.start_bitrate_bps);
+  }
+  if (preferences.max_bitrate_bps.has_value()) {
+    constraints.max_bitrate =
+        webrtc::DataRate::bps(*preferences.max_bitrate_bps);
+  }
+  if (preferences.min_bitrate_bps.has_value()) {
+    constraints.min_bitrate =
+        webrtc::DataRate::bps(*preferences.min_bitrate_bps);
+  }
+  rtc::CritScope lock(&target_observer_crit_);
+  last_set_bitrate_ = constraints;
+  if (media_transport_) {
     media_transport_->SetTargetBitrateLimits(constraints);
   }
 }
diff --git a/call/call.h b/call/call.h
index 3eceb49..90977da 100644
--- a/call/call.h
+++ b/call/call.h
@@ -124,6 +124,9 @@
 
   virtual void OnSentPacket(const rtc::SentPacket& sent_packet) = 0;
 
+  virtual void SetClientBitratePreferences(
+      const BitrateSettings& preferences) = 0;
+
   virtual ~Call() {}
 };
 
diff --git a/call/degraded_call.h b/call/degraded_call.h
index 89eafdb..86b1c9a 100644
--- a/call/degraded_call.h
+++ b/call/degraded_call.h
@@ -133,6 +133,8 @@
   const std::unique_ptr<Call> call_;
 
   void MediaTransportChange(MediaTransportInterface* media_transport) override;
+  void SetClientBitratePreferences(
+      const webrtc::BitrateSettings& preferences) override {}
   const absl::optional<BuiltInNetworkBehaviorConfig> send_config_;
   const std::unique_ptr<ProcessThread> send_process_thread_;
   SimulatedNetwork* send_simulated_network_;
diff --git a/media/engine/fake_webrtc_call.h b/media/engine/fake_webrtc_call.h
index 71a54d6..b20b7ed 100644
--- a/media/engine/fake_webrtc_call.h
+++ b/media/engine/fake_webrtc_call.h
@@ -303,6 +303,9 @@
   void MediaTransportChange(
       webrtc::MediaTransportInterface* media_transport_interface) override;
 
+  void SetClientBitratePreferences(
+      const webrtc::BitrateSettings& preferences) override {}
+
  private:
   webrtc::AudioSendStream* CreateAudioSendStream(
       const webrtc::AudioSendStream::Config& config) override;
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index bcba763..15bea89 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -3611,7 +3611,7 @@
   }
 
   RTC_DCHECK(call_.get());
-  call_->GetTransportControllerSend()->SetClientBitratePreferences(bitrate);
+  call_->SetClientBitratePreferences(bitrate);
 
   return RTCError::OK();
 }