blob: b58ba596ea85665a28cf300f79897987d6af8123 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +000011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_IMPL_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
kwiberg88788ad2016-02-19 07:04:49 -080014#include <memory>
terelius85fa7d52016-03-24 01:51:52 -070015#include <vector>
kwiberg88788ad2016-02-19 07:04:49 -080016
peahb624d8c2016-03-05 03:01:14 -080017#include "webrtc/base/constructormagic.h"
peahdf3efa82015-11-28 12:35:15 -080018#include "webrtc/base/criticalsection.h"
terelius85fa7d52016-03-24 01:51:52 -070019#include "webrtc/base/swap_queue.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000020#include "webrtc/modules/audio_processing/include/audio_processing.h"
peah737f4b82016-03-10 23:05:28 -080021#include "webrtc/modules/audio_processing/render_queue_item_verifier.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025class AudioBuffer;
26
peahb624d8c2016-03-05 03:01:14 -080027class EchoCancellationImpl : public EchoCancellation {
niklase@google.com470e71d2011-07-07 08:21:25 +000028 public:
peahb58a1582016-03-15 09:34:24 -070029 EchoCancellationImpl(rtc::CriticalSection* crit_render,
peahdf3efa82015-11-28 12:35:15 -080030 rtc::CriticalSection* crit_capture);
niklase@google.com470e71d2011-07-07 08:21:25 +000031 virtual ~EchoCancellationImpl();
32
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000033 int ProcessRenderAudio(const AudioBuffer* audio);
peahb58a1582016-03-15 09:34:24 -070034 int ProcessCaptureAudio(AudioBuffer* audio, int stream_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000035
36 // EchoCancellation implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000037 bool is_enabled() const override;
38 int stream_drift_samples() const override;
Minyue13b96ba2015-10-03 00:39:14 +020039 SuppressionLevel suppression_level() const override;
40 bool is_drift_compensation_enabled() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000041
peahb58a1582016-03-15 09:34:24 -070042 void Initialize(int sample_rate_hz,
43 size_t num_reverse_channels_,
44 size_t num_output_channels_,
45 size_t num_proc_channels_);
peahb624d8c2016-03-05 03:01:14 -080046 void SetExtraOptions(const Config& config);
Minyue13b96ba2015-10-03 00:39:14 +020047 bool is_delay_agnostic_enabled() const;
48 bool is_extended_filter_enabled() const;
peah6ebc4d32016-03-07 16:59:39 -080049 bool is_aec3_enabled() const;
peah7789fe72016-04-15 01:19:44 -070050 std::string GetExperimentsDescription();
Minyue13b96ba2015-10-03 00:39:14 +020051
peahdc2242d2016-04-06 09:30:58 -070052 // Checks whether the module is enabled. Must only be
53 // called from the render side of APM as otherwise
54 // deadlocks may occur.
55 bool is_enabled_render_side_query() const;
56
peahfa6228e2015-11-16 16:27:42 -080057 // Reads render side data that has been queued on the render call.
peahdf3efa82015-11-28 12:35:15 -080058 // Called holding the capture lock.
peahfa6228e2015-11-16 16:27:42 -080059 void ReadQueuedRenderData();
60
peah20028c42016-03-04 11:50:54 -080061 // Returns the system delay of the first AEC component.
62 int GetSystemDelayInSamples() const;
63
niklase@google.com470e71d2011-07-07 08:21:25 +000064 private:
peahb624d8c2016-03-05 03:01:14 -080065 class Canceller;
peahb58a1582016-03-15 09:34:24 -070066 struct StreamProperties;
peahb624d8c2016-03-05 03:01:14 -080067
niklase@google.com470e71d2011-07-07 08:21:25 +000068 // EchoCancellation implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000069 int Enable(bool enable) override;
70 int enable_drift_compensation(bool enable) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 void set_stream_drift_samples(int drift) override;
72 int set_suppression_level(SuppressionLevel level) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073 int enable_metrics(bool enable) override;
74 bool are_metrics_enabled() const override;
75 bool stream_has_echo() const override;
76 int GetMetrics(Metrics* metrics) override;
77 int enable_delay_logging(bool enable) override;
78 bool is_delay_logging_enabled() const override;
79 int GetDelayMetrics(int* median, int* std) override;
80 int GetDelayMetrics(int* median,
81 int* std,
82 float* fraction_poor_delays) override;
peahdf3efa82015-11-28 12:35:15 -080083
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000084 struct AecCore* aec_core() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
peahb58a1582016-03-15 09:34:24 -070086 size_t NumCancellersRequired() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000087
peahfa6228e2015-11-16 16:27:42 -080088 void AllocateRenderQueue();
peahb624d8c2016-03-05 03:01:14 -080089 int Configure();
peahfa6228e2015-11-16 16:27:42 -080090
peahdf3efa82015-11-28 12:35:15 -080091 rtc::CriticalSection* const crit_render_ ACQUIRED_BEFORE(crit_capture_);
92 rtc::CriticalSection* const crit_capture_;
peahfa6228e2015-11-16 16:27:42 -080093
peahb624d8c2016-03-05 03:01:14 -080094 bool enabled_ = false;
peahdf3efa82015-11-28 12:35:15 -080095 bool drift_compensation_enabled_ GUARDED_BY(crit_capture_);
96 bool metrics_enabled_ GUARDED_BY(crit_capture_);
97 SuppressionLevel suppression_level_ GUARDED_BY(crit_capture_);
98 int stream_drift_samples_ GUARDED_BY(crit_capture_);
99 bool was_stream_drift_set_ GUARDED_BY(crit_capture_);
100 bool stream_has_echo_ GUARDED_BY(crit_capture_);
101 bool delay_logging_enabled_ GUARDED_BY(crit_capture_);
102 bool extended_filter_enabled_ GUARDED_BY(crit_capture_);
103 bool delay_agnostic_enabled_ GUARDED_BY(crit_capture_);
peah6ebc4d32016-03-07 16:59:39 -0800104 bool aec3_enabled_ GUARDED_BY(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800105
106 size_t render_queue_element_max_size_ GUARDED_BY(crit_render_)
107 GUARDED_BY(crit_capture_);
108 std::vector<float> render_queue_buffer_ GUARDED_BY(crit_render_);
109 std::vector<float> capture_queue_buffer_ GUARDED_BY(crit_capture_);
110
111 // Lock protection not needed.
kwiberg88788ad2016-02-19 07:04:49 -0800112 std::unique_ptr<SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>>
peahfa6228e2015-11-16 16:27:42 -0800113 render_signal_queue_;
peahb624d8c2016-03-05 03:01:14 -0800114
115 std::vector<std::unique_ptr<Canceller>> cancellers_;
peahb58a1582016-03-15 09:34:24 -0700116 std::unique_ptr<StreamProperties> stream_properties_;
117
peahb624d8c2016-03-05 03:01:14 -0800118 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCancellationImpl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119};
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000120
niklase@google.com470e71d2011-07-07 08:21:25 +0000121} // namespace webrtc
122
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +0000123#endif // WEBRTC_MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_IMPL_H_