echo_canceller3.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include <vector>
  15. #include "api/array_view.h"
  16. #include "api/audio/echo_canceller3_config.h"
  17. #include "api/audio/echo_control.h"
  18. #include "modules/audio_processing/aec3/api_call_jitter_metrics.h"
  19. #include "modules/audio_processing/aec3/block_delay_buffer.h"
  20. #include "modules/audio_processing/aec3/block_framer.h"
  21. #include "modules/audio_processing/aec3/block_processor.h"
  22. #include "modules/audio_processing/aec3/frame_blocker.h"
  23. #include "modules/audio_processing/audio_buffer.h"
  24. #include "modules/audio_processing/logging/apm_data_dumper.h"
  25. #include "rtc_base/checks.h"
  26. #include "rtc_base/race_checker.h"
  27. #include "rtc_base/swap_queue.h"
  28. #include "rtc_base/thread_annotations.h"
  29. namespace webrtc {
  30. // Method for adjusting config parameter dependencies.
  31. // Only to be used externally to AEC3 for testing purposes.
  32. // TODO(webrtc:5298): Move this to a separate file.
  33. EchoCanceller3Config AdjustConfig(const EchoCanceller3Config& config);
  34. // Functor for verifying the invariance of the frames being put into the render
  35. // queue.
  36. class Aec3RenderQueueItemVerifier {
  37. public:
  38. Aec3RenderQueueItemVerifier(size_t num_bands,
  39. size_t num_channels,
  40. size_t frame_length)
  41. : num_bands_(num_bands),
  42. num_channels_(num_channels),
  43. frame_length_(frame_length) {}
  44. bool operator()(const std::vector<std::vector<std::vector<float>>>& v) const {
  45. if (v.size() != num_bands_) {
  46. return false;
  47. }
  48. for (const auto& band : v) {
  49. if (band.size() != num_channels_) {
  50. return false;
  51. }
  52. for (const auto& channel : band) {
  53. if (channel.size() != frame_length_) {
  54. return false;
  55. }
  56. }
  57. }
  58. return true;
  59. }
  60. private:
  61. const size_t num_bands_;
  62. const size_t num_channels_;
  63. const size_t frame_length_;
  64. };
  65. // Main class for the echo canceller3.
  66. // It does 4 things:
  67. // -Receives 10 ms frames of band-split audio.
  68. // -Provides the lower level echo canceller functionality with
  69. // blocks of 64 samples of audio data.
  70. // -Partially handles the jitter in the render and capture API
  71. // call sequence.
  72. //
  73. // The class is supposed to be used in a non-concurrent manner apart from the
  74. // AnalyzeRender call which can be called concurrently with the other methods.
  75. class EchoCanceller3 : public EchoControl {
  76. public:
  77. // Normal c-tor to use.
  78. EchoCanceller3(const EchoCanceller3Config& config,
  79. int sample_rate_hz,
  80. size_t num_render_channels,
  81. size_t num_capture_channels);
  82. // Testing c-tor that is used only for testing purposes.
  83. EchoCanceller3(const EchoCanceller3Config& config,
  84. int sample_rate_hz,
  85. size_t num_render_channels,
  86. size_t num_capture_channels,
  87. std::unique_ptr<BlockProcessor> block_processor);
  88. ~EchoCanceller3() override;
  89. EchoCanceller3(const EchoCanceller3&) = delete;
  90. EchoCanceller3& operator=(const EchoCanceller3&) = delete;
  91. // Analyzes and stores an internal copy of the split-band domain render
  92. // signal.
  93. void AnalyzeRender(AudioBuffer* render) override { AnalyzeRender(*render); }
  94. // Analyzes the full-band domain capture signal to detect signal saturation.
  95. void AnalyzeCapture(AudioBuffer* capture) override {
  96. AnalyzeCapture(*capture);
  97. }
  98. // Processes the split-band domain capture signal in order to remove any echo
  99. // present in the signal.
  100. void ProcessCapture(AudioBuffer* capture, bool level_change) override;
  101. // As above, but also returns the linear filter output.
  102. void ProcessCapture(AudioBuffer* capture,
  103. AudioBuffer* linear_output,
  104. bool level_change) override;
  105. // Collect current metrics from the echo canceller.
  106. Metrics GetMetrics() const override;
  107. // Provides an optional external estimate of the audio buffer delay.
  108. void SetAudioBufferDelay(int delay_ms) override;
  109. bool ActiveProcessing() const override;
  110. // Signals whether an external detector has detected echo leakage from the
  111. // echo canceller.
  112. // Note that in the case echo leakage has been flagged, it should be unflagged
  113. // once it is no longer occurring.
  114. void UpdateEchoLeakageStatus(bool leakage_detected) {
  115. RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
  116. block_processor_->UpdateEchoLeakageStatus(leakage_detected);
  117. }
  118. // Produces a default configuration that is suitable for a certain combination
  119. // of render and capture channels.
  120. static EchoCanceller3Config CreateDefaultConfig(size_t num_render_channels,
  121. size_t num_capture_channels);
  122. private:
  123. class RenderWriter;
  124. // Empties the render SwapQueue.
  125. void EmptyRenderQueue();
  126. // Analyzes and stores an internal copy of the split-band domain render
  127. // signal.
  128. void AnalyzeRender(const AudioBuffer& render);
  129. // Analyzes the full-band domain capture signal to detect signal saturation.
  130. void AnalyzeCapture(const AudioBuffer& capture);
  131. rtc::RaceChecker capture_race_checker_;
  132. rtc::RaceChecker render_race_checker_;
  133. // State that is accessed by the AnalyzeRender call.
  134. std::unique_ptr<RenderWriter> render_writer_
  135. RTC_GUARDED_BY(render_race_checker_);
  136. // State that may be accessed by the capture thread.
  137. static int instance_count_;
  138. std::unique_ptr<ApmDataDumper> data_dumper_;
  139. const EchoCanceller3Config config_;
  140. const int sample_rate_hz_;
  141. const int num_bands_;
  142. const size_t num_render_channels_;
  143. const size_t num_capture_channels_;
  144. std::unique_ptr<BlockFramer> linear_output_framer_
  145. RTC_GUARDED_BY(capture_race_checker_);
  146. BlockFramer output_framer_ RTC_GUARDED_BY(capture_race_checker_);
  147. FrameBlocker capture_blocker_ RTC_GUARDED_BY(capture_race_checker_);
  148. FrameBlocker render_blocker_ RTC_GUARDED_BY(capture_race_checker_);
  149. SwapQueue<std::vector<std::vector<std::vector<float>>>,
  150. Aec3RenderQueueItemVerifier>
  151. render_transfer_queue_;
  152. std::unique_ptr<BlockProcessor> block_processor_
  153. RTC_GUARDED_BY(capture_race_checker_);
  154. std::vector<std::vector<std::vector<float>>> render_queue_output_frame_
  155. RTC_GUARDED_BY(capture_race_checker_);
  156. bool saturated_microphone_signal_ RTC_GUARDED_BY(capture_race_checker_) =
  157. false;
  158. std::vector<std::vector<std::vector<float>>> render_block_
  159. RTC_GUARDED_BY(capture_race_checker_);
  160. std::unique_ptr<std::vector<std::vector<std::vector<float>>>>
  161. linear_output_block_ RTC_GUARDED_BY(capture_race_checker_);
  162. std::vector<std::vector<std::vector<float>>> capture_block_
  163. RTC_GUARDED_BY(capture_race_checker_);
  164. std::vector<std::vector<rtc::ArrayView<float>>> render_sub_frame_view_
  165. RTC_GUARDED_BY(capture_race_checker_);
  166. std::vector<std::vector<rtc::ArrayView<float>>> linear_output_sub_frame_view_
  167. RTC_GUARDED_BY(capture_race_checker_);
  168. std::vector<std::vector<rtc::ArrayView<float>>> capture_sub_frame_view_
  169. RTC_GUARDED_BY(capture_race_checker_);
  170. std::unique_ptr<BlockDelayBuffer> block_delay_buffer_
  171. RTC_GUARDED_BY(capture_race_checker_);
  172. ApiCallJitterMetrics api_call_metrics_ RTC_GUARDED_BY(capture_race_checker_);
  173. };
  174. } // namespace webrtc
  175. #endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_