blob: 6d49ef7695cdf81cab9b3c83814cabb15161d282 [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
kwiberg88788ad2016-02-19 07:04:49 -080016#include <memory>
terelius85fa7d52016-03-24 01:51:52 -070017#include <vector>
kwiberg88788ad2016-02-19 07:04:49 -080018
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/constructormagic.h"
21#include "rtc_base/criticalsection.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "rtc_base/thread_annotations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000025
niklase@google.com470e71d2011-07-07 08:21:25 +000026class AudioBuffer;
27
sazabe490b22018-10-03 17:03:13 +020028// The acoustic echo control for mobile (AECM) component is a low complexity
29// robust option intended for use on mobile devices.
Sam Zackrisson8c147b62018-09-28 12:40:47 +020030class EchoControlMobileImpl {
niklase@google.com470e71d2011-07-07 08:21:25 +000031 public:
peah253534d2016-03-15 04:32:28 -070032 EchoControlMobileImpl(rtc::CriticalSection* crit_render,
peahdf3efa82015-11-28 12:35:15 -080033 rtc::CriticalSection* crit_capture);
34
Sam Zackrisson8c147b62018-09-28 12:40:47 +020035 ~EchoControlMobileImpl();
36
37 int Enable(bool enable);
38 bool is_enabled() const;
39
40 // Recommended settings for particular audio routes. In general, the louder
41 // the echo is expected to be, the higher this value should be set. The
42 // preferred setting may vary from device to device.
43 enum RoutingMode {
44 kQuietEarpieceOrHeadset,
45 kEarpiece,
46 kLoudEarpiece,
47 kSpeakerphone,
48 kLoudSpeakerphone
49 };
50
51 // Sets echo control appropriate for the audio routing |mode| on the device.
52 // It can and should be updated during a call if the audio routing changes.
53 int set_routing_mode(RoutingMode mode);
54 RoutingMode routing_mode() const;
55
56 // Comfort noise replaces suppressed background noise to maintain a
57 // consistent signal level.
58 int enable_comfort_noise(bool enable);
59 bool is_comfort_noise_enabled() const;
60
61 // A typical use case is to initialize the component with an echo path from a
62 // previous call. The echo path is retrieved using |GetEchoPath()|, typically
63 // at the end of a call. The data can then be stored for later use as an
64 // initializer before the next call, using |SetEchoPath()|.
65 //
66 // Controlling the echo path this way requires the data |size_bytes| to match
67 // the internal echo path size. This size can be acquired using
68 // |echo_path_size_bytes()|. |SetEchoPath()| causes an entire reset, worth
69 // noting if it is to be called during an ongoing call.
70 //
71 // It is possible that version incompatibilities may result in a stored echo
72 // path of the incorrect size. In this case, the stored path should be
73 // discarded.
74 int SetEchoPath(const void* echo_path, size_t size_bytes);
75 int GetEchoPath(void* echo_path, size_t size_bytes) const;
76
77 // The returned path size is guaranteed not to change for the lifetime of
78 // the application.
79 static size_t echo_path_size_bytes();
niklase@google.com470e71d2011-07-07 08:21:25 +000080
peaha0624602016-10-25 04:45:24 -070081 void ProcessRenderAudio(rtc::ArrayView<const int16_t> packed_render_audio);
peah253534d2016-03-15 04:32:28 -070082 int ProcessCaptureAudio(AudioBuffer* audio, int stream_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
peah253534d2016-03-15 04:32:28 -070084 void Initialize(int sample_rate_hz,
85 size_t num_reverse_channels,
86 size_t num_output_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
peaha0624602016-10-25 04:45:24 -070088 static void PackRenderAudioBuffer(const AudioBuffer* audio,
89 size_t num_output_channels,
90 size_t num_channels,
91 std::vector<int16_t>* packed_buffer);
92
93 static size_t NumCancellersRequired(size_t num_output_channels,
94 size_t num_reverse_channels);
peahfa6228e2015-11-16 16:27:42 -080095
niklase@google.com470e71d2011-07-07 08:21:25 +000096 private:
peahbb9edbd2016-03-10 12:54:25 -080097 class Canceller;
peah253534d2016-03-15 04:32:28 -070098 struct StreamProperties;
peahbb9edbd2016-03-10 12:54:25 -080099
peahbb9edbd2016-03-10 12:54:25 -0800100 int Configure();
peahfa6228e2015-11-16 16:27:42 -0800101
danilchap56359be2017-09-07 07:53:45 -0700102 rtc::CriticalSection* const crit_render_ RTC_ACQUIRED_BEFORE(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800103 rtc::CriticalSection* const crit_capture_;
104
peahbb9edbd2016-03-10 12:54:25 -0800105 bool enabled_ = false;
106
danilchap56359be2017-09-07 07:53:45 -0700107 RoutingMode routing_mode_ RTC_GUARDED_BY(crit_capture_);
108 bool comfort_noise_enabled_ RTC_GUARDED_BY(crit_capture_);
109 unsigned char* external_echo_path_ RTC_GUARDED_BY(crit_render_)
110 RTC_GUARDED_BY(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800111
peahbb9edbd2016-03-10 12:54:25 -0800112 std::vector<std::unique_ptr<Canceller>> cancellers_;
peah253534d2016-03-15 04:32:28 -0700113 std::unique_ptr<StreamProperties> stream_properties_;
114
peahbb9edbd2016-03-10 12:54:25 -0800115 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoControlMobileImpl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116};
117} // namespace webrtc
118
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200119#endif // MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_IMPL_H_