Migrate modules/remote_bitrate_estimator to webrtc::Mutex.

Bug: webrtc:11567
Change-Id: Ib3c8f73459088434a70ee86b044dbbbe14db1777
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/178810
Reviewed-by: Sebastian Jansson <srte@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31652}
diff --git a/modules/remote_bitrate_estimator/BUILD.gn b/modules/remote_bitrate_estimator/BUILD.gn
index f5df205..0b2d2cd 100644
--- a/modules/remote_bitrate_estimator/BUILD.gn
+++ b/modules/remote_bitrate_estimator/BUILD.gn
@@ -56,6 +56,7 @@
     "../../rtc_base:rtc_numerics",
     "../../rtc_base:safe_minmax",
     "../../rtc_base/experiments:field_trial_parser",
+    "../../rtc_base/synchronization:mutex",
     "../../system_wrappers",
     "../../system_wrappers:field_trial",
     "../../system_wrappers:metrics",
diff --git a/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.cc b/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.cc
index b146d00..e8f835c 100644
--- a/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.cc
+++ b/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.cc
@@ -282,7 +282,7 @@
   uint32_t target_bitrate_bps = 0;
   std::vector<uint32_t> ssrcs;
   {
-    rtc::CritScope lock(&crit_);
+    MutexLock lock(&mutex_);
 
     TimeoutStreams(now_ms);
     RTC_DCHECK(inter_arrival_.get());
@@ -391,12 +391,12 @@
 
 void RemoteBitrateEstimatorAbsSendTime::OnRttUpdate(int64_t avg_rtt_ms,
                                                     int64_t max_rtt_ms) {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   remote_rate_.SetRtt(TimeDelta::Millis(avg_rtt_ms));
 }
 
 void RemoteBitrateEstimatorAbsSendTime::RemoveStream(uint32_t ssrc) {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   ssrcs_.erase(ssrc);
 }
 
@@ -409,7 +409,7 @@
   // thread.
   RTC_DCHECK(ssrcs);
   RTC_DCHECK(bitrate_bps);
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   if (!remote_rate_.ValidEstimate()) {
     return false;
   }
@@ -425,7 +425,7 @@
 void RemoteBitrateEstimatorAbsSendTime::SetMinBitrate(int min_bitrate_bps) {
   // Called from both the configuration thread and the network thread. Shouldn't
   // be called from the network thread in the future.
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   remote_rate_.SetMinBitrate(DataRate::BitsPerSec(min_bitrate_bps));
 }
 }  // namespace webrtc
diff --git a/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h b/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h
index 9fd4974..2423363 100644
--- a/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h
+++ b/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h
@@ -28,9 +28,9 @@
 #include "modules/remote_bitrate_estimator/overuse_estimator.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/constructor_magic.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/race_checker.h"
 #include "rtc_base/rate_statistics.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/clock.h"
 
@@ -114,12 +114,12 @@
 
   // Returns true if a probe which changed the estimate was detected.
   ProbeResult ProcessClusters(int64_t now_ms)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
 
   bool IsBitrateImproving(int probe_bitrate_bps) const
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
 
-  void TimeoutStreams(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_);
+  void TimeoutStreams(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(&mutex_);
 
   rtc::RaceChecker network_race_;
   Clock* const clock_;
@@ -138,9 +138,9 @@
   int64_t last_update_ms_;
   bool uma_recorded_;
 
-  rtc::CriticalSection crit_;
-  Ssrcs ssrcs_ RTC_GUARDED_BY(&crit_);
-  AimdRateControl remote_rate_ RTC_GUARDED_BY(&crit_);
+  mutable Mutex mutex_;
+  Ssrcs ssrcs_ RTC_GUARDED_BY(&mutex_);
+  AimdRateControl remote_rate_ RTC_GUARDED_BY(&mutex_);
 
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RemoteBitrateEstimatorAbsSendTime);
 };
diff --git a/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc b/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc
index db3bbe9..46d8fbc 100644
--- a/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc
+++ b/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc
@@ -95,7 +95,7 @@
   uint32_t rtp_timestamp =
       header.timestamp + header.extension.transmissionTimeOffset;
   int64_t now_ms = clock_->TimeInMilliseconds();
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   SsrcOveruseEstimatorMap::iterator it = overuse_detectors_.find(ssrc);
   if (it == overuse_detectors_.end()) {
     // This is a new SSRC. Adding to map.
@@ -158,7 +158,7 @@
 
 void RemoteBitrateEstimatorSingleStream::Process() {
   {
-    rtc::CritScope cs(&crit_sect_);
+    MutexLock lock(&mutex_);
     UpdateEstimate(clock_->TimeInMilliseconds());
   }
   last_process_time_ = clock_->TimeInMilliseconds();
@@ -168,7 +168,7 @@
   if (last_process_time_ < 0) {
     return 0;
   }
-  rtc::CritScope cs_(&crit_sect_);
+  MutexLock lock_(&mutex_);
   RTC_DCHECK_GT(process_interval_ms_, 0);
   return last_process_time_ + process_interval_ms_ -
          clock_->TimeInMilliseconds();
@@ -217,12 +217,12 @@
 
 void RemoteBitrateEstimatorSingleStream::OnRttUpdate(int64_t avg_rtt_ms,
                                                      int64_t max_rtt_ms) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   GetRemoteRate()->SetRtt(TimeDelta::Millis(avg_rtt_ms));
 }
 
 void RemoteBitrateEstimatorSingleStream::RemoveStream(unsigned int ssrc) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   SsrcOveruseEstimatorMap::iterator it = overuse_detectors_.find(ssrc);
   if (it != overuse_detectors_.end()) {
     delete it->second;
@@ -233,7 +233,7 @@
 bool RemoteBitrateEstimatorSingleStream::LatestEstimate(
     std::vector<uint32_t>* ssrcs,
     uint32_t* bitrate_bps) const {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   assert(bitrate_bps);
   if (!remote_rate_->ValidEstimate()) {
     return false;
@@ -264,7 +264,7 @@
 }
 
 void RemoteBitrateEstimatorSingleStream::SetMinBitrate(int min_bitrate_bps) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   remote_rate_->SetMinBitrate(DataRate::BitsPerSec(min_bitrate_bps));
 }
 
diff --git a/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h b/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h
index a28109c..6da67e5 100644
--- a/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h
+++ b/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h
@@ -22,8 +22,8 @@
 #include "modules/remote_bitrate_estimator/aimd_rate_control.h"
 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
 #include "rtc_base/constructor_magic.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/rate_statistics.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
@@ -54,26 +54,25 @@
   typedef std::map<uint32_t, Detector*> SsrcOveruseEstimatorMap;
 
   // Triggers a new estimate calculation.
-  void UpdateEstimate(int64_t time_now)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
+  void UpdateEstimate(int64_t time_now) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
 
   void GetSsrcs(std::vector<uint32_t>* ssrcs) const
-      RTC_SHARED_LOCKS_REQUIRED(crit_sect_);
+      RTC_SHARED_LOCKS_REQUIRED(mutex_);
 
   // Returns |remote_rate_| if the pointed to object exists,
   // otherwise creates it.
-  AimdRateControl* GetRemoteRate() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
+  AimdRateControl* GetRemoteRate() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
 
   Clock* const clock_;
   const FieldTrialBasedConfig field_trials_;
-  SsrcOveruseEstimatorMap overuse_detectors_ RTC_GUARDED_BY(crit_sect_);
-  RateStatistics incoming_bitrate_ RTC_GUARDED_BY(crit_sect_);
-  uint32_t last_valid_incoming_bitrate_ RTC_GUARDED_BY(crit_sect_);
-  std::unique_ptr<AimdRateControl> remote_rate_ RTC_GUARDED_BY(crit_sect_);
-  RemoteBitrateObserver* const observer_ RTC_GUARDED_BY(crit_sect_);
-  rtc::CriticalSection crit_sect_;
+  SsrcOveruseEstimatorMap overuse_detectors_ RTC_GUARDED_BY(mutex_);
+  RateStatistics incoming_bitrate_ RTC_GUARDED_BY(mutex_);
+  uint32_t last_valid_incoming_bitrate_ RTC_GUARDED_BY(mutex_);
+  std::unique_ptr<AimdRateControl> remote_rate_ RTC_GUARDED_BY(mutex_);
+  RemoteBitrateObserver* const observer_ RTC_GUARDED_BY(mutex_);
+  mutable Mutex mutex_;
   int64_t last_process_time_;
-  int64_t process_interval_ms_ RTC_GUARDED_BY(crit_sect_);
+  int64_t process_interval_ms_ RTC_GUARDED_BY(mutex_);
   bool uma_recorded_;
 
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RemoteBitrateEstimatorSingleStream);
diff --git a/modules/remote_bitrate_estimator/remote_estimator_proxy.cc b/modules/remote_bitrate_estimator/remote_estimator_proxy.cc
index f044721..a9cc170 100644
--- a/modules/remote_bitrate_estimator/remote_estimator_proxy.cc
+++ b/modules/remote_bitrate_estimator/remote_estimator_proxy.cc
@@ -61,7 +61,7 @@
     RTC_LOG(LS_WARNING) << "Arrival time out of bounds: " << arrival_time_ms;
     return;
   }
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   media_ssrc_ = header.ssrc;
   int64_t seq = 0;
 
@@ -134,7 +134,7 @@
 }
 
 int64_t RemoteEstimatorProxy::TimeUntilNextProcess() {
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   if (!send_periodic_feedback_) {
     // Wait a day until next process.
     return 24 * 60 * 60 * 1000;
@@ -147,7 +147,7 @@
 }
 
 void RemoteEstimatorProxy::Process() {
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   if (!send_periodic_feedback_) {
     return;
   }
@@ -169,7 +169,7 @@
       kTwccReportSize * 8.0 * 1000.0 / send_config_.min_interval->ms();
 
   // Let TWCC reports occupy 5% of total bandwidth.
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   send_interval_ms_ = static_cast<int>(
       0.5 + kTwccReportSize * 8.0 * 1000.0 /
                 rtc::SafeClamp(send_config_.bandwidth_fraction * bitrate_bps,
@@ -178,7 +178,7 @@
 
 void RemoteEstimatorProxy::SetSendPeriodicFeedback(
     bool send_periodic_feedback) {
-  rtc::CritScope cs(&lock_);
+  MutexLock lock(&lock_);
   send_periodic_feedback_ = send_periodic_feedback;
 }
 
diff --git a/modules/remote_bitrate_estimator/remote_estimator_proxy.h b/modules/remote_bitrate_estimator/remote_estimator_proxy.h
index e11eb1f..a4adefc 100644
--- a/modules/remote_bitrate_estimator/remote_estimator_proxy.h
+++ b/modules/remote_bitrate_estimator/remote_estimator_proxy.h
@@ -17,9 +17,9 @@
 #include "api/transport/network_control.h"
 #include "api/transport/webrtc_key_value_config.h"
 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/experiments/field_trial_parser.h"
 #include "rtc_base/numerics/sequence_number_util.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace webrtc {
 
@@ -92,7 +92,7 @@
   const TransportWideFeedbackConfig send_config_;
   int64_t last_process_time_ms_;
 
-  rtc::CriticalSection lock_;
+  Mutex lock_;
   //  |network_state_estimator_| may be null.
   NetworkStateEstimator* const network_state_estimator_
       RTC_PT_GUARDED_BY(&lock_);
diff --git a/modules/remote_bitrate_estimator/test/bwe_test_logging.cc b/modules/remote_bitrate_estimator/test/bwe_test_logging.cc
index cf44fa0..f99576f 100644
--- a/modules/remote_bitrate_estimator/test/bwe_test_logging.cc
+++ b/modules/remote_bitrate_estimator/test/bwe_test_logging.cc
@@ -61,27 +61,27 @@
 }
 
 void Logging::SetGlobalContext(uint32_t name) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   thread_map_[rtc::CurrentThreadId()].global_state.tag = ToString(name);
 }
 
 void Logging::SetGlobalContext(const std::string& name) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   thread_map_[rtc::CurrentThreadId()].global_state.tag = name;
 }
 
 void Logging::SetGlobalContext(const char* name) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   thread_map_[rtc::CurrentThreadId()].global_state.tag = name;
 }
 
 void Logging::SetGlobalEnable(bool enabled) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   thread_map_[rtc::CurrentThreadId()].global_state.enabled = enabled;
 }
 
 void Logging::Log(const char format[], ...) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -118,7 +118,7 @@
                    double value,
                    uint32_t ssrc,
                    const std::string& alg_name) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -132,7 +132,7 @@
                       const std::string& name,
                       double value,
                       int flow_id) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -145,7 +145,7 @@
                               const std::string& name,
                               double value,
                               int flow_id) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -161,7 +161,7 @@
                            double yhigh,
                            const std::string& error_title,
                            int flow_id) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -180,7 +180,7 @@
                                 double ymax,
                                 const std::string& limit_title,
                                 int flow_id) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -195,7 +195,7 @@
                         const std::string& title,
                         const std::string& y_label,
                         int num_flows) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   const State& state = it->second.stack.top();
@@ -229,7 +229,7 @@
 void Logging::PushState(const std::string& append_to_tag,
                         int64_t timestamp_ms,
                         bool enabled) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   State new_state(append_to_tag, timestamp_ms, enabled);
   ThreadState* thread_state = &thread_map_[rtc::CurrentThreadId()];
   std::stack<State>* stack = &thread_state->stack;
@@ -242,7 +242,7 @@
 }
 
 void Logging::PopState() {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
   RTC_DCHECK(it != thread_map_.end());
   std::stack<State>* stack = &it->second.stack;
diff --git a/modules/remote_bitrate_estimator/test/bwe_test_logging.h b/modules/remote_bitrate_estimator/test/bwe_test_logging.h
index a399d0b..5a30da8 100644
--- a/modules/remote_bitrate_estimator/test/bwe_test_logging.h
+++ b/modules/remote_bitrate_estimator/test/bwe_test_logging.h
@@ -129,7 +129,7 @@
 #include <string>
 
 #include "rtc_base/constructor_magic.h"
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 
 #define BWE_TEST_LOGGING_GLOBAL_CONTEXT(name)                             \
   do {                                                                    \
@@ -345,7 +345,7 @@
                  bool enabled);
   void PopState();
 
-  rtc::CriticalSection crit_sect_;
+  Mutex mutex_;
   ThreadMap thread_map_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(Logging);