Migrate modules/audio_coding, audio_mixer/ and audio_processing/ to webrtc::Mutex.

Bug: webrtc:11567
Change-Id: I03b78bd2e411e9bcca199f85e4457511826cd17e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176745
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Magnus Flodman <mflodman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31649}
diff --git a/modules/audio_processing/audio_processing_impl.h b/modules/audio_processing/audio_processing_impl.h
index 676e3cd..dfd5f63 100644
--- a/modules/audio_processing/audio_processing_impl.h
+++ b/modules/audio_processing/audio_processing_impl.h
@@ -39,10 +39,10 @@
 #include "modules/audio_processing/rms_level.h"
 #include "modules/audio_processing/transient/transient_suppressor.h"
 #include "modules/audio_processing/voice_detection.h"
-#include "rtc_base/critical_section.h"
 #include "rtc_base/gtest_prod_util.h"
 #include "rtc_base/ignore_wundef.h"
 #include "rtc_base/swap_queue.h"
+#include "rtc_base/synchronization/mutex.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
@@ -101,7 +101,7 @@
   void set_stream_key_pressed(bool key_pressed) override;
   void set_stream_analog_level(int level) override;
   int recommended_stream_analog_level() const
-      RTC_LOCKS_EXCLUDED(crit_capture_) override;
+      RTC_LOCKS_EXCLUDED(mutex_capture_) override;
 
   // Render-side exclusive methods possibly running APM in a
   // multi-threaded manner. Acquire the render lock.
@@ -141,7 +141,7 @@
  protected:
   // Overridden in a mock.
   virtual int InitializeLocked()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_, mutex_capture_);
 
  private:
   // TODO(peah): These friend classes should be removed as soon as the new
@@ -157,7 +157,7 @@
                            BitexactWithDisabledModules);
 
   int recommended_stream_analog_level_locked() const
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   void OverrideSubmoduleCreationForTesting(
       const ApmSubmoduleCreationOverrides& overrides);
@@ -238,7 +238,7 @@
   // Called by render: Holds the render lock when reading the format struct and
   // acquires both locks if reinitialization is required.
   int MaybeInitializeRender(const ProcessingConfig& processing_config)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
   // Called by capture: Holds the capture lock when reading the format struct
   // and acquires both locks if reinitialization is needed.
   int MaybeInitializeCapture(const StreamConfig& input_config,
@@ -247,57 +247,58 @@
   // Method for updating the state keeping track of the active submodules.
   // Returns a bool indicating whether the state has changed.
   bool UpdateActiveSubmoduleStates()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Methods requiring APM running in a single-threaded manner, requiring both
   // the render and capture lock to be acquired.
   int InitializeLocked(const ProcessingConfig& config)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_, mutex_capture_);
   void InitializeResidualEchoDetector()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_, mutex_capture_);
   void InitializeEchoController()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_, mutex_capture_);
 
   // Initializations of capture-only submodules, requiring the capture lock
   // already acquired.
   void InitializeHighPassFilter(bool forced_reset)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializeVoiceDetector() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializeGainController1() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializeVoiceDetector() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializeGainController1() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
   void InitializeTransientSuppressor()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializeGainController2() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializeNoiseSuppressor() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializePreAmplifier() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializePostProcessor() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void InitializeAnalyzer() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializeGainController2() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializeNoiseSuppressor() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializePreAmplifier() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializePostProcessor() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void InitializeAnalyzer() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Initializations of render-only submodules, requiring the render lock
   // already acquired.
-  void InitializePreProcessor() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+  void InitializePreProcessor() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
 
   // Sample rate used for the fullband processing.
   int proc_fullband_sample_rate_hz() const
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Empties and handles the respective RuntimeSetting queues.
   void HandleCaptureRuntimeSettings()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
-  void HandleRenderRuntimeSettings() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
+  void HandleRenderRuntimeSettings()
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
 
-  void EmptyQueuedRenderAudio() RTC_LOCKS_EXCLUDED(crit_capture_);
+  void EmptyQueuedRenderAudio() RTC_LOCKS_EXCLUDED(mutex_capture_);
   void EmptyQueuedRenderAudioLocked()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
   void AllocateRenderQueue()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_, mutex_capture_);
   void QueueBandedRenderAudio(AudioBuffer* audio)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
   void QueueNonbandedRenderAudio(AudioBuffer* audio)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
 
   // Capture-side exclusive methods possibly running APM in a multi-threaded
   // manner that are called with the render lock already acquired.
-  int ProcessCaptureStreamLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+  int ProcessCaptureStreamLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Render-side exclusive methods possibly running APM in a multi-threaded
   // manner that are called with the render lock already acquired.
@@ -305,8 +306,8 @@
   int AnalyzeReverseStreamLocked(const float* const* src,
                                  const StreamConfig& input_config,
                                  const StreamConfig& output_config)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
-  int ProcessRenderStreamLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_render_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
+  int ProcessRenderStreamLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_);
 
   // Collects configuration settings from public and private
   // submodules to be saved as an audioproc::Config message on the
@@ -314,29 +315,30 @@
   // config if it is different from the last saved one; if |forced|,
   // writes the config regardless of the last saved.
   void WriteAecDumpConfigMessage(bool forced)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Notifies attached AecDump of current configuration and capture data.
   void RecordUnprocessedCaptureStream(const float* const* capture_stream)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   void RecordUnprocessedCaptureStream(const int16_t* const data,
                                       const StreamConfig& config)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Notifies attached AecDump of current configuration and
   // processed capture data and issues a capture stream recording
   // request.
   void RecordProcessedCaptureStream(
       const float* const* processed_capture_stream)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   void RecordProcessedCaptureStream(const int16_t* const data,
                                     const StreamConfig& config)
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // Notifies attached AecDump about current state (delay, drift, etc).
-  void RecordAudioProcessingState() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+  void RecordAudioProcessingState()
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_capture_);
 
   // AecDump instance used for optionally logging APM config, input
   // and output to file in the AEC-dump format defined in debug.proto.
@@ -344,18 +346,18 @@
 
   // Hold the last config written with AecDump for avoiding writing
   // the same config twice.
-  InternalAPMConfig apm_config_for_aec_dump_ RTC_GUARDED_BY(crit_capture_);
+  InternalAPMConfig apm_config_for_aec_dump_ RTC_GUARDED_BY(mutex_capture_);
 
   // Critical sections.
-  rtc::CriticalSection crit_render_ RTC_ACQUIRED_BEFORE(crit_capture_);
-  rtc::CriticalSection crit_capture_;
+  mutable Mutex mutex_render_ RTC_ACQUIRED_BEFORE(mutex_capture_);
+  mutable Mutex mutex_capture_;
 
   // Struct containing the Config specifying the behavior of APM.
   AudioProcessing::Config config_;
 
   // Overrides for testing the exclusion of some submodules from the build.
   ApmSubmoduleCreationOverrides submodule_creation_overrides_
-      RTC_GUARDED_BY(crit_capture_);
+      RTC_GUARDED_BY(mutex_capture_);
 
   // Class containing information about what submodules are active.
   SubmoduleStates submodule_states_;
@@ -444,7 +446,7 @@
       const float* keyboard_data = nullptr;
     } keyboard_info;
     int cached_stream_analog_level_ = 0;
-  } capture_ RTC_GUARDED_BY(crit_capture_);
+  } capture_ RTC_GUARDED_BY(mutex_capture_);
 
   struct ApmCaptureNonLockedState {
     ApmCaptureNonLockedState()
@@ -465,7 +467,7 @@
     ~ApmRenderState();
     std::unique_ptr<AudioConverter> render_converter;
     std::unique_ptr<AudioBuffer> render_audio;
-  } render_ RTC_GUARDED_BY(crit_render_);
+  } render_ RTC_GUARDED_BY(mutex_render_);
 
   // Class for statistics reporting. The class is thread-safe and no lock is
   // needed when accessing it.
@@ -481,27 +483,28 @@
     void UpdateStatistics(const AudioProcessingStats& new_stats);
 
    private:
-    rtc::CriticalSection crit_stats_;
-    AudioProcessingStats cached_stats_ RTC_GUARDED_BY(crit_stats_);
+    Mutex mutex_stats_;
+    AudioProcessingStats cached_stats_ RTC_GUARDED_BY(mutex_stats_);
     SwapQueue<AudioProcessingStats> stats_message_queue_;
   } stats_reporter_;
 
-  std::vector<int16_t> aecm_render_queue_buffer_ RTC_GUARDED_BY(crit_render_);
-  std::vector<int16_t> aecm_capture_queue_buffer_ RTC_GUARDED_BY(crit_capture_);
+  std::vector<int16_t> aecm_render_queue_buffer_ RTC_GUARDED_BY(mutex_render_);
+  std::vector<int16_t> aecm_capture_queue_buffer_
+      RTC_GUARDED_BY(mutex_capture_);
 
-  size_t agc_render_queue_element_max_size_ RTC_GUARDED_BY(crit_render_)
-      RTC_GUARDED_BY(crit_capture_) = 0;
-  std::vector<int16_t> agc_render_queue_buffer_ RTC_GUARDED_BY(crit_render_);
-  std::vector<int16_t> agc_capture_queue_buffer_ RTC_GUARDED_BY(crit_capture_);
+  size_t agc_render_queue_element_max_size_ RTC_GUARDED_BY(mutex_render_)
+      RTC_GUARDED_BY(mutex_capture_) = 0;
+  std::vector<int16_t> agc_render_queue_buffer_ RTC_GUARDED_BY(mutex_render_);
+  std::vector<int16_t> agc_capture_queue_buffer_ RTC_GUARDED_BY(mutex_capture_);
 
-  size_t red_render_queue_element_max_size_ RTC_GUARDED_BY(crit_render_)
-      RTC_GUARDED_BY(crit_capture_) = 0;
-  std::vector<float> red_render_queue_buffer_ RTC_GUARDED_BY(crit_render_);
-  std::vector<float> red_capture_queue_buffer_ RTC_GUARDED_BY(crit_capture_);
+  size_t red_render_queue_element_max_size_ RTC_GUARDED_BY(mutex_render_)
+      RTC_GUARDED_BY(mutex_capture_) = 0;
+  std::vector<float> red_render_queue_buffer_ RTC_GUARDED_BY(mutex_render_);
+  std::vector<float> red_capture_queue_buffer_ RTC_GUARDED_BY(mutex_capture_);
 
-  RmsLevel capture_input_rms_ RTC_GUARDED_BY(crit_capture_);
-  RmsLevel capture_output_rms_ RTC_GUARDED_BY(crit_capture_);
-  int capture_rms_interval_counter_ RTC_GUARDED_BY(crit_capture_) = 0;
+  RmsLevel capture_input_rms_ RTC_GUARDED_BY(mutex_capture_);
+  RmsLevel capture_output_rms_ RTC_GUARDED_BY(mutex_capture_);
+  int capture_rms_interval_counter_ RTC_GUARDED_BY(mutex_capture_) = 0;
 
   // Lock protection not needed.
   std::unique_ptr<