1 /*
2  *  Copyright (c) 2012 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 
11 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_IMPL_H_
12 #define WEBRTC_MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_IMPL_H_
13 
14 #include "webrtc/base/criticalsection.h"
15 #include "webrtc/base/scoped_ptr.h"
16 #include "webrtc/common_audio/swap_queue.h"
17 #include "webrtc/modules/audio_processing/include/audio_processing.h"
18 #include "webrtc/modules/audio_processing/processing_component.h"
19 
20 namespace webrtc {
21 
22 class AudioBuffer;
23 
24 class EchoCancellationImpl : public EchoCancellation,
25                              public ProcessingComponent {
26  public:
27   EchoCancellationImpl(const AudioProcessing* apm,
28                        rtc::CriticalSection* crit_render,
29                        rtc::CriticalSection* crit_capture);
30   virtual ~EchoCancellationImpl();
31 
32   int ProcessRenderAudio(const AudioBuffer* audio);
33   int ProcessCaptureAudio(AudioBuffer* audio);
34 
35   // EchoCancellation implementation.
36   bool is_enabled() const override;
37   int stream_drift_samples() const override;
38   SuppressionLevel suppression_level() const override;
39   bool is_drift_compensation_enabled() const override;
40 
41   // ProcessingComponent implementation.
42   int Initialize() override;
43   void SetExtraOptions(const Config& config) override;
44   bool is_delay_agnostic_enabled() const;
45   bool is_extended_filter_enabled() const;
46 
47   // Reads render side data that has been queued on the render call.
48   // Called holding the capture lock.
49   void ReadQueuedRenderData();
50 
51  private:
52   // EchoCancellation implementation.
53   int Enable(bool enable) override;
54   int enable_drift_compensation(bool enable) override;
55   void set_stream_drift_samples(int drift) override;
56   int set_suppression_level(SuppressionLevel level) override;
57   int enable_metrics(bool enable) override;
58   bool are_metrics_enabled() const override;
59   bool stream_has_echo() const override;
60   int GetMetrics(Metrics* metrics) override;
61   int enable_delay_logging(bool enable) override;
62   bool is_delay_logging_enabled() const override;
63   int GetDelayMetrics(int* median, int* std) override;
64   int GetDelayMetrics(int* median,
65                       int* std,
66                       float* fraction_poor_delays) override;
67 
68   struct AecCore* aec_core() const override;
69 
70   // ProcessingComponent implementation.
71   void* CreateHandle() const override;
72   int InitializeHandle(void* handle) const override;
73   int ConfigureHandle(void* handle) const override;
74   void DestroyHandle(void* handle) const override;
75   size_t num_handles_required() const override;
76   int GetHandleError(void* handle) const override;
77 
78   void AllocateRenderQueue();
79 
80   // Not guarded as its public API is thread safe.
81   const AudioProcessing* apm_;
82 
83   rtc::CriticalSection* const crit_render_ ACQUIRED_BEFORE(crit_capture_);
84   rtc::CriticalSection* const crit_capture_;
85 
86   bool drift_compensation_enabled_ GUARDED_BY(crit_capture_);
87   bool metrics_enabled_ GUARDED_BY(crit_capture_);
88   SuppressionLevel suppression_level_ GUARDED_BY(crit_capture_);
89   int stream_drift_samples_ GUARDED_BY(crit_capture_);
90   bool was_stream_drift_set_ GUARDED_BY(crit_capture_);
91   bool stream_has_echo_ GUARDED_BY(crit_capture_);
92   bool delay_logging_enabled_ GUARDED_BY(crit_capture_);
93   bool extended_filter_enabled_ GUARDED_BY(crit_capture_);
94   bool delay_agnostic_enabled_ GUARDED_BY(crit_capture_);
95 
96   size_t render_queue_element_max_size_ GUARDED_BY(crit_render_)
97       GUARDED_BY(crit_capture_);
98   std::vector<float> render_queue_buffer_ GUARDED_BY(crit_render_);
99   std::vector<float> capture_queue_buffer_ GUARDED_BY(crit_capture_);
100 
101   // Lock protection not needed.
102   rtc::scoped_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>>
103       render_signal_queue_;
104 };
105 
106 }  // namespace webrtc
107 
108 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_IMPL_H_
109