blob: a5b66c8311b80447a91d557a749ffe702727abbb [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_IMPL_H_
12#define MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/include/audio_processing.h"
18#include "modules/audio_processing/render_queue_item_verifier.h"
19#include "rtc_base/constructormagic.h"
20#include "rtc_base/criticalsection.h"
21#include "rtc_base/swap_queue.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025class AudioBuffer;
26
sazabe490b22018-10-03 17:03:13 +020027// The acoustic echo control for mobile (AECM) component is a low complexity
28// robust option intended for use on mobile devices.
Sam Zackrisson8c147b62018-09-28 12:40:47 +020029class EchoControlMobileImpl {
niklase@google.com470e71d2011-07-07 08:21:25 +000030 public:
peah253534d2016-03-15 04:32:28 -070031 EchoControlMobileImpl(rtc::CriticalSection* crit_render,
peahdf3efa82015-11-28 12:35:15 -080032 rtc::CriticalSection* crit_capture);
33
Sam Zackrisson8c147b62018-09-28 12:40:47 +020034 ~EchoControlMobileImpl();
35
36 int Enable(bool enable);
37 bool is_enabled() const;
38
39 // Recommended settings for particular audio routes. In general, the louder
40 // the echo is expected to be, the higher this value should be set. The
41 // preferred setting may vary from device to device.
42 enum RoutingMode {
43 kQuietEarpieceOrHeadset,
44 kEarpiece,
45 kLoudEarpiece,
46 kSpeakerphone,
47 kLoudSpeakerphone
48 };
49
50 // Sets echo control appropriate for the audio routing |mode| on the device.
51 // It can and should be updated during a call if the audio routing changes.
52 int set_routing_mode(RoutingMode mode);
53 RoutingMode routing_mode() const;
54
55 // Comfort noise replaces suppressed background noise to maintain a
56 // consistent signal level.
57 int enable_comfort_noise(bool enable);
58 bool is_comfort_noise_enabled() const;
59
60 // A typical use case is to initialize the component with an echo path from a
61 // previous call. The echo path is retrieved using |GetEchoPath()|, typically
62 // at the end of a call. The data can then be stored for later use as an
63 // initializer before the next call, using |SetEchoPath()|.
64 //
65 // Controlling the echo path this way requires the data |size_bytes| to match
66 // the internal echo path size. This size can be acquired using
67 // |echo_path_size_bytes()|. |SetEchoPath()| causes an entire reset, worth
68 // noting if it is to be called during an ongoing call.
69 //
70 // It is possible that version incompatibilities may result in a stored echo
71 // path of the incorrect size. In this case, the stored path should be
72 // discarded.
73 int SetEchoPath(const void* echo_path, size_t size_bytes);
74 int GetEchoPath(void* echo_path, size_t size_bytes) const;
75
76 // The returned path size is guaranteed not to change for the lifetime of
77 // the application.
78 static size_t echo_path_size_bytes();
niklase@google.com470e71d2011-07-07 08:21:25 +000079
peaha0624602016-10-25 04:45:24 -070080 void ProcessRenderAudio(rtc::ArrayView<const int16_t> packed_render_audio);
peah253534d2016-03-15 04:32:28 -070081 int ProcessCaptureAudio(AudioBuffer* audio, int stream_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
peah253534d2016-03-15 04:32:28 -070083 void Initialize(int sample_rate_hz,
84 size_t num_reverse_channels,
85 size_t num_output_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
peaha0624602016-10-25 04:45:24 -070087 static void PackRenderAudioBuffer(const AudioBuffer* audio,
88 size_t num_output_channels,
89 size_t num_channels,
90 std::vector<int16_t>* packed_buffer);
91
92 static size_t NumCancellersRequired(size_t num_output_channels,
93 size_t num_reverse_channels);
peahfa6228e2015-11-16 16:27:42 -080094
niklase@google.com470e71d2011-07-07 08:21:25 +000095 private:
peahbb9edbd2016-03-10 12:54:25 -080096 class Canceller;
peah253534d2016-03-15 04:32:28 -070097 struct StreamProperties;
peahbb9edbd2016-03-10 12:54:25 -080098
peahbb9edbd2016-03-10 12:54:25 -080099 int Configure();
peahfa6228e2015-11-16 16:27:42 -0800100
danilchap56359be2017-09-07 07:53:45 -0700101 rtc::CriticalSection* const crit_render_ RTC_ACQUIRED_BEFORE(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800102 rtc::CriticalSection* const crit_capture_;
103
peahbb9edbd2016-03-10 12:54:25 -0800104 bool enabled_ = false;
105
danilchap56359be2017-09-07 07:53:45 -0700106 RoutingMode routing_mode_ RTC_GUARDED_BY(crit_capture_);
107 bool comfort_noise_enabled_ RTC_GUARDED_BY(crit_capture_);
108 unsigned char* external_echo_path_ RTC_GUARDED_BY(crit_render_)
109 RTC_GUARDED_BY(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800110
peahbb9edbd2016-03-10 12:54:25 -0800111 std::vector<std::unique_ptr<Canceller>> cancellers_;
peah253534d2016-03-15 04:32:28 -0700112 std::unique_ptr<StreamProperties> stream_properties_;
113
peahbb9edbd2016-03-10 12:54:25 -0800114 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoControlMobileImpl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000115};
116} // namespace webrtc
117
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200118#endif // MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_IMPL_H_