blob: 3341ec56b6fffea58348941f7a0ab786714c9c25 [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
Sam Zackrisson8c147b62018-09-28 12:40:47 +020027class EchoControlMobileImpl {
niklase@google.com470e71d2011-07-07 08:21:25 +000028 public:
peah253534d2016-03-15 04:32:28 -070029 EchoControlMobileImpl(rtc::CriticalSection* crit_render,
peahdf3efa82015-11-28 12:35:15 -080030 rtc::CriticalSection* crit_capture);
31
Sam Zackrisson8c147b62018-09-28 12:40:47 +020032 ~EchoControlMobileImpl();
33
34 int Enable(bool enable);
35 bool is_enabled() const;
36
37 // Recommended settings for particular audio routes. In general, the louder
38 // the echo is expected to be, the higher this value should be set. The
39 // preferred setting may vary from device to device.
40 enum RoutingMode {
41 kQuietEarpieceOrHeadset,
42 kEarpiece,
43 kLoudEarpiece,
44 kSpeakerphone,
45 kLoudSpeakerphone
46 };
47
48 // Sets echo control appropriate for the audio routing |mode| on the device.
49 // It can and should be updated during a call if the audio routing changes.
50 int set_routing_mode(RoutingMode mode);
51 RoutingMode routing_mode() const;
52
53 // Comfort noise replaces suppressed background noise to maintain a
54 // consistent signal level.
55 int enable_comfort_noise(bool enable);
56 bool is_comfort_noise_enabled() const;
57
58 // A typical use case is to initialize the component with an echo path from a
59 // previous call. The echo path is retrieved using |GetEchoPath()|, typically
60 // at the end of a call. The data can then be stored for later use as an
61 // initializer before the next call, using |SetEchoPath()|.
62 //
63 // Controlling the echo path this way requires the data |size_bytes| to match
64 // the internal echo path size. This size can be acquired using
65 // |echo_path_size_bytes()|. |SetEchoPath()| causes an entire reset, worth
66 // noting if it is to be called during an ongoing call.
67 //
68 // It is possible that version incompatibilities may result in a stored echo
69 // path of the incorrect size. In this case, the stored path should be
70 // discarded.
71 int SetEchoPath(const void* echo_path, size_t size_bytes);
72 int GetEchoPath(void* echo_path, size_t size_bytes) const;
73
74 // The returned path size is guaranteed not to change for the lifetime of
75 // the application.
76 static size_t echo_path_size_bytes();
niklase@google.com470e71d2011-07-07 08:21:25 +000077
peaha0624602016-10-25 04:45:24 -070078 void ProcessRenderAudio(rtc::ArrayView<const int16_t> packed_render_audio);
peah253534d2016-03-15 04:32:28 -070079 int ProcessCaptureAudio(AudioBuffer* audio, int stream_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
peah253534d2016-03-15 04:32:28 -070081 void Initialize(int sample_rate_hz,
82 size_t num_reverse_channels,
83 size_t num_output_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
peaha0624602016-10-25 04:45:24 -070085 static void PackRenderAudioBuffer(const AudioBuffer* audio,
86 size_t num_output_channels,
87 size_t num_channels,
88 std::vector<int16_t>* packed_buffer);
89
90 static size_t NumCancellersRequired(size_t num_output_channels,
91 size_t num_reverse_channels);
peahfa6228e2015-11-16 16:27:42 -080092
niklase@google.com470e71d2011-07-07 08:21:25 +000093 private:
peahbb9edbd2016-03-10 12:54:25 -080094 class Canceller;
peah253534d2016-03-15 04:32:28 -070095 struct StreamProperties;
peahbb9edbd2016-03-10 12:54:25 -080096
peahbb9edbd2016-03-10 12:54:25 -080097 int Configure();
peahfa6228e2015-11-16 16:27:42 -080098
danilchap56359be2017-09-07 07:53:45 -070099 rtc::CriticalSection* const crit_render_ RTC_ACQUIRED_BEFORE(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800100 rtc::CriticalSection* const crit_capture_;
101
peahbb9edbd2016-03-10 12:54:25 -0800102 bool enabled_ = false;
103
danilchap56359be2017-09-07 07:53:45 -0700104 RoutingMode routing_mode_ RTC_GUARDED_BY(crit_capture_);
105 bool comfort_noise_enabled_ RTC_GUARDED_BY(crit_capture_);
106 unsigned char* external_echo_path_ RTC_GUARDED_BY(crit_render_)
107 RTC_GUARDED_BY(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -0800108
peahbb9edbd2016-03-10 12:54:25 -0800109 std::vector<std::unique_ptr<Canceller>> cancellers_;
peah253534d2016-03-15 04:32:28 -0700110 std::unique_ptr<StreamProperties> stream_properties_;
111
peahbb9edbd2016-03-10 12:54:25 -0800112 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoControlMobileImpl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113};
114} // namespace webrtc
115
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200116#endif // MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_IMPL_H_