Migrate video/ except video/end_to_end_tests and video/adaptation to webrtc::Mutex.

Also migrates test/ partly.

Bug: webrtc:11567
Change-Id: If5b2eae65c5f297f364b6e3c67f94946a09b4a96
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/178862
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31672}
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 856b73e..15e86e4 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -125,6 +125,7 @@
     "../rtc_base:rtc_base_approved",
     "../rtc_base:rtc_task_queue",
     "../rtc_base:timeutils",
+    "../rtc_base/synchronization:mutex",
     "../rtc_base/task_utils:repeating_task",
     "../system_wrappers",
   ]
@@ -763,6 +764,7 @@
     "../rtc_base:macromagic",
     "../rtc_base:rtc_task_queue",
     "../rtc_base:timeutils",
+    "../rtc_base/synchronization:mutex",
     "../rtc_base/synchronization:sequence_checker",
     "../system_wrappers",
   ]
diff --git a/test/fake_encoder.cc b/test/fake_encoder.cc
index 2959559..84a4afd 100644
--- a/test/fake_encoder.cc
+++ b/test/fake_encoder.cc
@@ -67,19 +67,19 @@
 
 void FakeEncoder::SetMaxBitrate(int max_kbps) {
   RTC_DCHECK_GE(max_kbps, -1);  // max_kbps == -1 disables it.
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   max_target_bitrate_kbps_ = max_kbps;
   SetRatesLocked(current_rate_settings_);
 }
 
 void FakeEncoder::SetQp(int qp) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   qp_ = qp;
 }
 
 int32_t FakeEncoder::InitEncode(const VideoCodec* config,
                                 const Settings& settings) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   config_ = *config;
   current_rate_settings_.bitrate.SetBitrate(0, 0, config_.startBitrate * 1000);
   current_rate_settings_.framerate_fps = config_.maxFramerate;
@@ -100,7 +100,7 @@
   uint32_t counter;
   absl::optional<int> qp;
   {
-    rtc::CritScope cs(&crit_sect_);
+    MutexLock lock(&mutex_);
     max_framerate = config_.maxFramerate;
     num_simulcast_streams = config_.numberOfSimulcastStreams;
     for (int i = 0; i < num_simulcast_streams; ++i) {
@@ -182,7 +182,7 @@
     }
   }
 
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   for (uint8_t i = 0; i < num_simulcast_streams; ++i) {
     if (target_bitrate.GetBitrate(i, 0) > 0) {
       int temporal_id = last_frame_info_.layers.size() > i
@@ -232,7 +232,7 @@
 
 int32_t FakeEncoder::RegisterEncodeCompleteCallback(
     EncodedImageCallback* callback) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   callback_ = callback;
   return 0;
 }
@@ -242,7 +242,7 @@
 }
 
 void FakeEncoder::SetRates(const RateControlParameters& parameters) {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   SetRatesLocked(parameters);
 }
 
@@ -280,7 +280,7 @@
 }
 
 int FakeEncoder::GetConfiguredInputFramerate() const {
-  rtc::CritScope cs(&crit_sect_);
+  MutexLock lock(&mutex_);
   return static_cast<int>(current_rate_settings_.framerate_fps + 0.5);
 }
 
@@ -295,7 +295,7 @@
   const int kIdrFrequency = 10;
   int current_idr_counter;
   {
-    rtc::CritScope cs(&local_crit_sect_);
+    MutexLock lock(&local_mutex_);
     current_idr_counter = idr_counter_;
     ++idr_counter_;
   }
diff --git a/test/fake_encoder.h b/test/fake_encoder.h
index ade0e35..22c7723 100644
--- a/test/fake_encoder.h
+++ b/test/fake_encoder.h
@@ -26,7 +26,7 @@
 #include "api/video_codecs/video_encoder.h"
 #include "modules/include/module_common_types.h"
 #include "modules/video_coding/include/video_codec_interface.h"
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/synchronization/sequence_checker.h"
 #include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/clock.h"
@@ -40,23 +40,23 @@
   virtual ~FakeEncoder() = default;
 
   // Sets max bitrate. Not thread-safe, call before registering the encoder.
-  void SetMaxBitrate(int max_kbps) RTC_LOCKS_EXCLUDED(crit_sect_);
-  void SetQp(int qp) RTC_LOCKS_EXCLUDED(crit_sect_);
+  void SetMaxBitrate(int max_kbps) RTC_LOCKS_EXCLUDED(mutex_);
+  void SetQp(int qp) RTC_LOCKS_EXCLUDED(mutex_);
 
   void SetFecControllerOverride(
       FecControllerOverride* fec_controller_override) override;
 
   int32_t InitEncode(const VideoCodec* config, const Settings& settings)
-      RTC_LOCKS_EXCLUDED(crit_sect_) override;
+      RTC_LOCKS_EXCLUDED(mutex_) override;
   int32_t Encode(const VideoFrame& input_image,
                  const std::vector<VideoFrameType>* frame_types)
-      RTC_LOCKS_EXCLUDED(crit_sect_) override;
+      RTC_LOCKS_EXCLUDED(mutex_) override;
   int32_t RegisterEncodeCompleteCallback(EncodedImageCallback* callback)
-      RTC_LOCKS_EXCLUDED(crit_sect_) override;
+      RTC_LOCKS_EXCLUDED(mutex_) override;
   int32_t Release() override;
   void SetRates(const RateControlParameters& parameters)
-      RTC_LOCKS_EXCLUDED(crit_sect_) override;
-  int GetConfiguredInputFramerate() const RTC_LOCKS_EXCLUDED(crit_sect_);
+      RTC_LOCKS_EXCLUDED(mutex_) override;
+  int GetConfiguredInputFramerate() const RTC_LOCKS_EXCLUDED(mutex_);
   EncoderInfo GetEncoderInfo() const override;
 
   static const char* kImplementationName;
@@ -81,7 +81,7 @@
                       uint8_t num_simulcast_streams,
                       const VideoBitrateAllocation& target_bitrate,
                       SimulcastStream simulcast_streams[kMaxSimulcastStreams],
-                      int framerate) RTC_LOCKS_EXCLUDED(crit_sect_);
+                      int framerate) RTC_LOCKS_EXCLUDED(mutex_);
 
   // Called before the frame is passed to callback_->OnEncodedImage, to let
   // subclasses fill out codec_specific, possibly modify encodedImage.
@@ -91,20 +91,20 @@
       CodecSpecificInfo* codec_specific);
 
   void SetRatesLocked(const RateControlParameters& parameters)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
 
-  FrameInfo last_frame_info_ RTC_GUARDED_BY(crit_sect_);
+  FrameInfo last_frame_info_ RTC_GUARDED_BY(mutex_);
   Clock* const clock_;
 
-  VideoCodec config_ RTC_GUARDED_BY(crit_sect_);
-  EncodedImageCallback* callback_ RTC_GUARDED_BY(crit_sect_);
-  RateControlParameters current_rate_settings_ RTC_GUARDED_BY(crit_sect_);
-  int max_target_bitrate_kbps_ RTC_GUARDED_BY(crit_sect_);
-  bool pending_keyframe_ RTC_GUARDED_BY(crit_sect_);
-  uint32_t counter_ RTC_GUARDED_BY(crit_sect_);
-  rtc::CriticalSection crit_sect_;
+  VideoCodec config_ RTC_GUARDED_BY(mutex_);
+  EncodedImageCallback* callback_ RTC_GUARDED_BY(mutex_);
+  RateControlParameters current_rate_settings_ RTC_GUARDED_BY(mutex_);
+  int max_target_bitrate_kbps_ RTC_GUARDED_BY(mutex_);
+  bool pending_keyframe_ RTC_GUARDED_BY(mutex_);
+  uint32_t counter_ RTC_GUARDED_BY(mutex_);
+  mutable Mutex mutex_;
   bool used_layers_[kMaxSimulcastStreams];
-  absl::optional<int> qp_ RTC_GUARDED_BY(crit_sect_);
+  absl::optional<int> qp_ RTC_GUARDED_BY(mutex_);
 
   // Current byte debt to be payed over a number of frames.
   // The debt is acquired by keyframes overshooting the bitrate target.
@@ -121,8 +121,8 @@
       EncodedImage* encoded_image,
       CodecSpecificInfo* codec_specific) override;
 
-  int idr_counter_ RTC_GUARDED_BY(local_crit_sect_);
-  rtc::CriticalSection local_crit_sect_;
+  int idr_counter_ RTC_GUARDED_BY(local_mutex_);
+  Mutex local_mutex_;
 };
 
 class DelayedEncoder : public test::FakeEncoder {
diff --git a/test/frame_forwarder.cc b/test/frame_forwarder.cc
index d8ec4b5..e89f753 100644
--- a/test/frame_forwarder.cc
+++ b/test/frame_forwarder.cc
@@ -18,14 +18,14 @@
 FrameForwarder::~FrameForwarder() {}
 
 void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   if (sink_)
     sink_->OnFrame(video_frame);
 }
 
 void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
                                      const rtc::VideoSinkWants& wants) {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   AddOrUpdateSinkLocked(sink, wants);
 }
 
@@ -38,13 +38,13 @@
 }
 
 void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   RTC_DCHECK_EQ(sink, sink_);
   sink_ = nullptr;
 }
 
 rtc::VideoSinkWants FrameForwarder::sink_wants() const {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   return sink_wants_;
 }
 
@@ -53,7 +53,7 @@
 }
 
 bool FrameForwarder::has_sinks() const {
-  rtc::CritScope lock(&crit_);
+  MutexLock lock(&mutex_);
   return sink_ != nullptr;
 }
 
diff --git a/test/frame_forwarder.h b/test/frame_forwarder.h
index d391160..bbf11cc 100644
--- a/test/frame_forwarder.h
+++ b/test/frame_forwarder.h
@@ -12,7 +12,7 @@
 
 #include "api/video/video_frame.h"
 #include "api/video/video_source_interface.h"
-#include "rtc_base/critical_section.h"
+#include "rtc_base/synchronization/mutex.h"
 
 namespace webrtc {
 namespace test {
@@ -27,25 +27,25 @@
   ~FrameForwarder() override;
   // Forwards |video_frame| to the registered |sink_|.
   virtual void IncomingCapturedFrame(const VideoFrame& video_frame)
-      RTC_LOCKS_EXCLUDED(crit_);
-  rtc::VideoSinkWants sink_wants() const RTC_LOCKS_EXCLUDED(crit_);
-  bool has_sinks() const RTC_LOCKS_EXCLUDED(crit_);
+      RTC_LOCKS_EXCLUDED(mutex_);
+  rtc::VideoSinkWants sink_wants() const RTC_LOCKS_EXCLUDED(mutex_);
+  bool has_sinks() const RTC_LOCKS_EXCLUDED(mutex_);
 
  protected:
   rtc::VideoSinkWants sink_wants_locked() const
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
   void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
                        const rtc::VideoSinkWants& wants)
-      RTC_LOCKS_EXCLUDED(crit_) override;
+      RTC_LOCKS_EXCLUDED(mutex_) override;
   void AddOrUpdateSinkLocked(rtc::VideoSinkInterface<VideoFrame>* sink,
                              const rtc::VideoSinkWants& wants)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
   void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink)
-      RTC_LOCKS_EXCLUDED(crit_) override;
+      RTC_LOCKS_EXCLUDED(mutex_) override;
 
-  rtc::CriticalSection crit_;
-  rtc::VideoSinkInterface<VideoFrame>* sink_ RTC_GUARDED_BY(crit_);
-  rtc::VideoSinkWants sink_wants_ RTC_GUARDED_BY(crit_);
+  mutable Mutex mutex_;
+  rtc::VideoSinkInterface<VideoFrame>* sink_ RTC_GUARDED_BY(mutex_);
+  rtc::VideoSinkWants sink_wants_ RTC_GUARDED_BY(mutex_);
 };
 
 }  // namespace test